xref: /linux/drivers/net/wireless/ath/ath12k/core.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5  */
6 
7 #include <linux/export.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/remoteproc.h>
11 #include <linux/firmware.h>
12 #include <linux/of.h>
13 #include <linux/of_graph.h>
14 #include "ahb.h"
15 #include "core.h"
16 #include "dp_tx.h"
17 #include "dp_rx.h"
18 #include "debug.h"
19 #include "debugfs.h"
20 #include "fw.h"
21 #include "hif.h"
22 #include "pci.h"
23 #include "wow.h"
24 #include "dp_cmn.h"
25 #include "peer.h"
26 #include "qmi.h"
27 
28 unsigned int ath12k_debug_mask;
29 module_param_named(debug_mask, ath12k_debug_mask, uint, 0644);
30 MODULE_PARM_DESC(debug_mask, "Debugging mask");
31 EXPORT_SYMBOL(ath12k_debug_mask);
32 
33 bool ath12k_ftm_mode;
34 module_param_named(ftm_mode, ath12k_ftm_mode, bool, 0444);
35 MODULE_PARM_DESC(ftm_mode, "Boots up in factory test mode");
36 EXPORT_SYMBOL(ath12k_ftm_mode);
37 
38 /* protected with ath12k_hw_group_mutex */
39 static struct list_head ath12k_hw_group_list = LIST_HEAD_INIT(ath12k_hw_group_list);
40 
41 static DEFINE_MUTEX(ath12k_hw_group_mutex);
42 
43 static const struct
44 ath12k_mem_profile_based_param ath12k_mem_profile_based_param[] = {
45 [ATH12K_QMI_MEMORY_MODE_DEFAULT] = {
46 		.num_vdevs = 17,
47 		.max_client_single = 512,
48 		.max_client_dbs = 128,
49 		.max_client_dbs_sbs = 128,
50 		.dp_params = {
51 			.tx_comp_ring_size = 32768,
52 			.rxdma_monitor_buf_ring_size = 4096,
53 			.rxdma_monitor_dst_ring_size = 8192,
54 			.num_pool_tx_desc = 32768,
55 			.rx_desc_count = 12288,
56 			.rx_release_ring_size = 16384,
57 		},
58 	},
59 [ATH12K_QMI_MEMORY_MODE_LOW_512_M] = {
60 		.num_vdevs = 9,
61 		.max_client_single = 128,
62 		.max_client_dbs = 64,
63 		.max_client_dbs_sbs = 64,
64 		.dp_params = {
65 			.tx_comp_ring_size = 16384,
66 			.rxdma_monitor_buf_ring_size = 256,
67 			.rxdma_monitor_dst_ring_size = 512,
68 			.num_pool_tx_desc = 16384,
69 			.rx_desc_count = 6144,
70 			.rx_release_ring_size = 8192,
71 		},
72 	},
73 };
74 
ath12k_core_rfkill_config(struct ath12k_base * ab)75 static int ath12k_core_rfkill_config(struct ath12k_base *ab)
76 {
77 	struct ath12k *ar;
78 	int ret = 0, i;
79 
80 	if (!(ab->target_caps.sys_cap_info & WMI_SYS_CAP_INFO_RFKILL))
81 		return 0;
82 
83 	if (ath12k_acpi_get_disable_rfkill(ab))
84 		return 0;
85 
86 	for (i = 0; i < ab->num_radios; i++) {
87 		ar = ab->pdevs[i].ar;
88 
89 		ret = ath12k_mac_rfkill_config(ar);
90 		if (ret && ret != -EOPNOTSUPP) {
91 			ath12k_warn(ab, "failed to configure rfkill: %d", ret);
92 			return ret;
93 		}
94 	}
95 
96 	return ret;
97 }
98 
99 /* Check if we need to continue with suspend/resume operation.
100  * Return:
101  *	a negative value: error happens and don't continue.
102  *	0:  no error but don't continue.
103  *	positive value: no error and do continue.
104  */
ath12k_core_continue_suspend_resume(struct ath12k_base * ab)105 static int ath12k_core_continue_suspend_resume(struct ath12k_base *ab)
106 {
107 	struct ath12k *ar;
108 
109 	if (!ab->hw_params->supports_suspend)
110 		return -EOPNOTSUPP;
111 
112 	/* so far single_pdev_only chips have supports_suspend as true
113 	 * so pass 0 as a dummy pdev_id here.
114 	 */
115 	ar = ab->pdevs[0].ar;
116 	if (!ar || !ar->ah || ar->ah->state != ATH12K_HW_STATE_OFF)
117 		return 0;
118 
119 	return 1;
120 }
121 
ath12k_core_suspend(struct ath12k_base * ab)122 int ath12k_core_suspend(struct ath12k_base *ab)
123 {
124 	struct ath12k *ar;
125 	int ret, i;
126 
127 	ret = ath12k_core_continue_suspend_resume(ab);
128 	if (ret <= 0)
129 		return ret;
130 
131 	for (i = 0; i < ab->num_radios; i++) {
132 		ar = ab->pdevs[i].ar;
133 		if (!ar)
134 			continue;
135 
136 		wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
137 
138 		ret = ath12k_mac_wait_tx_complete(ar);
139 		if (ret) {
140 			wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
141 			ath12k_warn(ab, "failed to wait tx complete: %d\n", ret);
142 			return ret;
143 		}
144 
145 		wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
146 	}
147 
148 	/* PM framework skips suspend_late/resume_early callbacks
149 	 * if other devices report errors in their suspend callbacks.
150 	 * However ath12k_core_resume() would still be called because
151 	 * here we return success thus kernel put us on dpm_suspended_list.
152 	 * Since we won't go through a power down/up cycle, there is
153 	 * no chance to call complete(&ab->restart_completed) in
154 	 * ath12k_core_restart(), making ath12k_core_resume() timeout.
155 	 * So call it here to avoid this issue. This also works in case
156 	 * no error happens thus suspend_late/resume_early get called,
157 	 * because it will be reinitialized in ath12k_core_resume_early().
158 	 */
159 	complete(&ab->restart_completed);
160 
161 	return 0;
162 }
163 EXPORT_SYMBOL(ath12k_core_suspend);
164 
ath12k_core_suspend_late(struct ath12k_base * ab)165 int ath12k_core_suspend_late(struct ath12k_base *ab)
166 {
167 	int ret;
168 
169 	ret = ath12k_core_continue_suspend_resume(ab);
170 	if (ret <= 0)
171 		return ret;
172 
173 	ath12k_acpi_stop(ab);
174 
175 	ath12k_hif_irq_disable(ab);
176 	ath12k_hif_ce_irq_disable(ab);
177 
178 	ath12k_hif_power_down(ab, true);
179 
180 	return 0;
181 }
182 EXPORT_SYMBOL(ath12k_core_suspend_late);
183 
ath12k_core_resume_early(struct ath12k_base * ab)184 int ath12k_core_resume_early(struct ath12k_base *ab)
185 {
186 	int ret;
187 
188 	ret = ath12k_core_continue_suspend_resume(ab);
189 	if (ret <= 0)
190 		return ret;
191 
192 	reinit_completion(&ab->restart_completed);
193 	ret = ath12k_hif_power_up(ab);
194 	if (ret)
195 		ath12k_warn(ab, "failed to power up hif during resume: %d\n", ret);
196 
197 	return ret;
198 }
199 EXPORT_SYMBOL(ath12k_core_resume_early);
200 
ath12k_core_resume(struct ath12k_base * ab)201 int ath12k_core_resume(struct ath12k_base *ab)
202 {
203 	long time_left;
204 	int ret;
205 
206 	ret = ath12k_core_continue_suspend_resume(ab);
207 	if (ret <= 0)
208 		return ret;
209 
210 	time_left = wait_for_completion_timeout(&ab->restart_completed,
211 						ATH12K_RESET_TIMEOUT_HZ);
212 	if (time_left == 0) {
213 		ath12k_warn(ab, "timeout while waiting for restart complete");
214 		return -ETIMEDOUT;
215 	}
216 
217 	return 0;
218 }
219 EXPORT_SYMBOL(ath12k_core_resume);
220 
__ath12k_core_create_board_name(struct ath12k_base * ab,char * name,size_t name_len,bool with_variant,bool bus_type_mode,bool with_default)221 static int __ath12k_core_create_board_name(struct ath12k_base *ab, char *name,
222 					   size_t name_len, bool with_variant,
223 					   bool bus_type_mode, bool with_default)
224 {
225 	/* strlen(',variant=') + strlen(ab->qmi.target.bdf_ext) */
226 	char variant[9 + ATH12K_QMI_BDF_EXT_STR_LENGTH] = {};
227 
228 	if (with_variant && ab->qmi.target.bdf_ext[0] != '\0')
229 		scnprintf(variant, sizeof(variant), ",variant=%s",
230 			  ab->qmi.target.bdf_ext);
231 
232 	switch (ab->id.bdf_search) {
233 	case ATH12K_BDF_SEARCH_BUS_AND_BOARD:
234 		if (bus_type_mode)
235 			scnprintf(name, name_len,
236 				  "bus=%s",
237 				  ath12k_bus_str(ab->hif.bus));
238 		else
239 			scnprintf(name, name_len,
240 				  "bus=%s,vendor=%04x,device=%04x,subsystem-vendor=%04x,subsystem-device=%04x,qmi-chip-id=%d,qmi-board-id=%d%s",
241 				  ath12k_bus_str(ab->hif.bus),
242 				  ab->id.vendor, ab->id.device,
243 				  ab->id.subsystem_vendor,
244 				  ab->id.subsystem_device,
245 				  ab->qmi.target.chip_id,
246 				  ab->qmi.target.board_id,
247 				  variant);
248 		break;
249 	default:
250 		scnprintf(name, name_len,
251 			  "bus=%s,qmi-chip-id=%d,qmi-board-id=%d%s",
252 			  ath12k_bus_str(ab->hif.bus),
253 			  ab->qmi.target.chip_id,
254 			  with_default ?
255 			  ATH12K_BOARD_ID_DEFAULT : ab->qmi.target.board_id,
256 			  variant);
257 		break;
258 	}
259 
260 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot using board name '%s'\n", name);
261 
262 	return 0;
263 }
264 
ath12k_core_create_board_name(struct ath12k_base * ab,char * name,size_t name_len)265 static int ath12k_core_create_board_name(struct ath12k_base *ab, char *name,
266 					 size_t name_len)
267 {
268 	return __ath12k_core_create_board_name(ab, name, name_len, true, false, false);
269 }
270 
ath12k_core_create_fallback_board_name(struct ath12k_base * ab,char * name,size_t name_len)271 static int ath12k_core_create_fallback_board_name(struct ath12k_base *ab, char *name,
272 						  size_t name_len)
273 {
274 	return __ath12k_core_create_board_name(ab, name, name_len, false, false, true);
275 }
276 
ath12k_core_create_bus_type_board_name(struct ath12k_base * ab,char * name,size_t name_len)277 static int ath12k_core_create_bus_type_board_name(struct ath12k_base *ab, char *name,
278 						  size_t name_len)
279 {
280 	return __ath12k_core_create_board_name(ab, name, name_len, false, true, true);
281 }
282 
ath12k_core_firmware_request(struct ath12k_base * ab,const char * file)283 const struct firmware *ath12k_core_firmware_request(struct ath12k_base *ab,
284 						    const char *file)
285 {
286 	const struct firmware *fw;
287 	char path[100];
288 	int ret;
289 
290 	if (!file)
291 		return ERR_PTR(-ENOENT);
292 
293 	ath12k_core_create_firmware_path(ab, file, path, sizeof(path));
294 
295 	ret = firmware_request_nowarn(&fw, path, ab->dev);
296 	if (ret)
297 		return ERR_PTR(ret);
298 
299 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot firmware request %s size %zu\n",
300 		   path, fw->size);
301 
302 	return fw;
303 }
304 
ath12k_core_free_bdf(struct ath12k_base * ab,struct ath12k_board_data * bd)305 void ath12k_core_free_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
306 {
307 	if (!IS_ERR(bd->fw))
308 		release_firmware(bd->fw);
309 
310 	memset(bd, 0, sizeof(*bd));
311 }
312 
ath12k_core_parse_bd_ie_board(struct ath12k_base * ab,struct ath12k_board_data * bd,const void * buf,size_t buf_len,const char * boardname,int ie_id,int name_id,int data_id)313 static int ath12k_core_parse_bd_ie_board(struct ath12k_base *ab,
314 					 struct ath12k_board_data *bd,
315 					 const void *buf, size_t buf_len,
316 					 const char *boardname,
317 					 int ie_id,
318 					 int name_id,
319 					 int data_id)
320 {
321 	const struct ath12k_fw_ie *hdr;
322 	bool name_match_found;
323 	int ret, board_ie_id;
324 	size_t board_ie_len;
325 	const void *board_ie_data;
326 
327 	name_match_found = false;
328 
329 	/* go through ATH12K_BD_IE_BOARD_/ATH12K_BD_IE_REGDB_ elements */
330 	while (buf_len > sizeof(struct ath12k_fw_ie)) {
331 		hdr = buf;
332 		board_ie_id = le32_to_cpu(hdr->id);
333 		board_ie_len = le32_to_cpu(hdr->len);
334 		board_ie_data = hdr->data;
335 
336 		buf_len -= sizeof(*hdr);
337 		buf += sizeof(*hdr);
338 
339 		if (buf_len < ALIGN(board_ie_len, 4)) {
340 			ath12k_err(ab, "invalid %s length: %zu < %zu\n",
341 				   ath12k_bd_ie_type_str(ie_id),
342 				   buf_len, ALIGN(board_ie_len, 4));
343 			ret = -EINVAL;
344 			goto out;
345 		}
346 
347 		if (board_ie_id == name_id) {
348 			ath12k_dbg_dump(ab, ATH12K_DBG_BOOT, "board name", "",
349 					board_ie_data, board_ie_len);
350 
351 			if (board_ie_len != strlen(boardname))
352 				goto next;
353 
354 			ret = memcmp(board_ie_data, boardname, strlen(boardname));
355 			if (ret)
356 				goto next;
357 
358 			name_match_found = true;
359 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
360 				   "boot found match %s for name '%s'",
361 				   ath12k_bd_ie_type_str(ie_id),
362 				   boardname);
363 		} else if (board_ie_id == data_id) {
364 			if (!name_match_found)
365 				/* no match found */
366 				goto next;
367 
368 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
369 				   "boot found %s for '%s'",
370 				   ath12k_bd_ie_type_str(ie_id),
371 				   boardname);
372 
373 			bd->data = board_ie_data;
374 			bd->len = board_ie_len;
375 
376 			ret = 0;
377 			goto out;
378 		} else {
379 			ath12k_warn(ab, "unknown %s id found: %d\n",
380 				    ath12k_bd_ie_type_str(ie_id),
381 				    board_ie_id);
382 		}
383 next:
384 		/* jump over the padding */
385 		board_ie_len = ALIGN(board_ie_len, 4);
386 
387 		buf_len -= board_ie_len;
388 		buf += board_ie_len;
389 	}
390 
391 	/* no match found */
392 	ret = -ENOENT;
393 
394 out:
395 	return ret;
396 }
397 
ath12k_core_fetch_board_data_api_n(struct ath12k_base * ab,struct ath12k_board_data * bd,const char * boardname,int ie_id_match,int name_id,int data_id)398 static int ath12k_core_fetch_board_data_api_n(struct ath12k_base *ab,
399 					      struct ath12k_board_data *bd,
400 					      const char *boardname,
401 					      int ie_id_match,
402 					      int name_id,
403 					      int data_id)
404 {
405 	size_t len, magic_len;
406 	const u8 *data;
407 	char *filename, filepath[100];
408 	size_t ie_len;
409 	struct ath12k_fw_ie *hdr;
410 	int ret, ie_id;
411 
412 	filename = ATH12K_BOARD_API2_FILE;
413 
414 	if (!bd->fw)
415 		bd->fw = ath12k_core_firmware_request(ab, filename);
416 
417 	if (IS_ERR(bd->fw))
418 		return PTR_ERR(bd->fw);
419 
420 	data = bd->fw->data;
421 	len = bd->fw->size;
422 
423 	ath12k_core_create_firmware_path(ab, filename,
424 					 filepath, sizeof(filepath));
425 
426 	/* magic has extra null byte padded */
427 	magic_len = strlen(ATH12K_BOARD_MAGIC) + 1;
428 	if (len < magic_len) {
429 		ath12k_err(ab, "failed to find magic value in %s, file too short: %zu\n",
430 			   filepath, len);
431 		ret = -EINVAL;
432 		goto err;
433 	}
434 
435 	if (memcmp(data, ATH12K_BOARD_MAGIC, magic_len)) {
436 		ath12k_err(ab, "found invalid board magic\n");
437 		ret = -EINVAL;
438 		goto err;
439 	}
440 
441 	/* magic is padded to 4 bytes */
442 	magic_len = ALIGN(magic_len, 4);
443 	if (len < magic_len) {
444 		ath12k_err(ab, "failed: %s too small to contain board data, len: %zu\n",
445 			   filepath, len);
446 		ret = -EINVAL;
447 		goto err;
448 	}
449 
450 	data += magic_len;
451 	len -= magic_len;
452 
453 	while (len > sizeof(struct ath12k_fw_ie)) {
454 		hdr = (struct ath12k_fw_ie *)data;
455 		ie_id = le32_to_cpu(hdr->id);
456 		ie_len = le32_to_cpu(hdr->len);
457 
458 		len -= sizeof(*hdr);
459 		data = hdr->data;
460 
461 		if (len < ALIGN(ie_len, 4)) {
462 			ath12k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
463 				   ie_id, ie_len, len);
464 			ret = -EINVAL;
465 			goto err;
466 		}
467 
468 		if (ie_id == ie_id_match) {
469 			ret = ath12k_core_parse_bd_ie_board(ab, bd, data,
470 							    ie_len,
471 							    boardname,
472 							    ie_id_match,
473 							    name_id,
474 							    data_id);
475 			if (ret == -ENOENT)
476 				/* no match found, continue */
477 				goto next;
478 			else if (ret)
479 				/* there was an error, bail out */
480 				goto err;
481 			/* either found or error, so stop searching */
482 			goto out;
483 		}
484 next:
485 		/* jump over the padding */
486 		ie_len = ALIGN(ie_len, 4);
487 
488 		len -= ie_len;
489 		data += ie_len;
490 	}
491 
492 out:
493 	if (!bd->data || !bd->len) {
494 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
495 			   "failed to fetch %s for %s from %s\n",
496 			   ath12k_bd_ie_type_str(ie_id_match),
497 			   boardname, filepath);
498 		ret = -ENODATA;
499 		goto err;
500 	}
501 
502 	return 0;
503 
504 err:
505 	ath12k_core_free_bdf(ab, bd);
506 	return ret;
507 }
508 
ath12k_core_fetch_board_data_api_1(struct ath12k_base * ab,struct ath12k_board_data * bd,char * filename)509 int ath12k_core_fetch_board_data_api_1(struct ath12k_base *ab,
510 				       struct ath12k_board_data *bd,
511 				       char *filename)
512 {
513 	bd->fw = ath12k_core_firmware_request(ab, filename);
514 	if (IS_ERR(bd->fw))
515 		return PTR_ERR(bd->fw);
516 
517 	bd->data = bd->fw->data;
518 	bd->len = bd->fw->size;
519 
520 	return 0;
521 }
522 
523 #define BOARD_NAME_SIZE 200
ath12k_core_fetch_bdf(struct ath12k_base * ab,struct ath12k_board_data * bd)524 int ath12k_core_fetch_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
525 {
526 	char boardname[BOARD_NAME_SIZE], fallback_boardname[BOARD_NAME_SIZE];
527 	char *filename, filepath[100];
528 	int bd_api;
529 	int ret;
530 
531 	filename = ATH12K_BOARD_API2_FILE;
532 
533 	ret = ath12k_core_create_board_name(ab, boardname, sizeof(boardname));
534 	if (ret) {
535 		ath12k_err(ab, "failed to create board name: %d", ret);
536 		return ret;
537 	}
538 
539 	bd_api = 2;
540 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname,
541 						 ATH12K_BD_IE_BOARD,
542 						 ATH12K_BD_IE_BOARD_NAME,
543 						 ATH12K_BD_IE_BOARD_DATA);
544 	if (!ret)
545 		goto success;
546 
547 	ret = ath12k_core_create_fallback_board_name(ab, fallback_boardname,
548 						     sizeof(fallback_boardname));
549 	if (ret) {
550 		ath12k_err(ab, "failed to create fallback board name: %d", ret);
551 		return ret;
552 	}
553 
554 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, fallback_boardname,
555 						 ATH12K_BD_IE_BOARD,
556 						 ATH12K_BD_IE_BOARD_NAME,
557 						 ATH12K_BD_IE_BOARD_DATA);
558 	if (!ret)
559 		goto success;
560 
561 	bd_api = 1;
562 	ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE);
563 	if (ret) {
564 		ath12k_core_create_firmware_path(ab, filename,
565 						 filepath, sizeof(filepath));
566 		ath12k_err(ab, "failed to fetch board data for %s from %s\n",
567 			   boardname, filepath);
568 		if (memcmp(boardname, fallback_boardname, strlen(boardname)))
569 			ath12k_err(ab, "failed to fetch board data for %s from %s\n",
570 				   fallback_boardname, filepath);
571 
572 		ath12k_err(ab, "failed to fetch board.bin from %s\n",
573 			   ab->hw_params->fw.dir);
574 		return ret;
575 	}
576 
577 success:
578 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", bd_api);
579 	return 0;
580 }
581 
ath12k_core_fetch_regdb(struct ath12k_base * ab,struct ath12k_board_data * bd)582 int ath12k_core_fetch_regdb(struct ath12k_base *ab, struct ath12k_board_data *bd)
583 {
584 	char boardname[BOARD_NAME_SIZE], default_boardname[BOARD_NAME_SIZE];
585 	int ret;
586 
587 	ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
588 	if (ret) {
589 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
590 			   "failed to create board name for regdb: %d", ret);
591 		goto exit;
592 	}
593 
594 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname,
595 						 ATH12K_BD_IE_REGDB,
596 						 ATH12K_BD_IE_REGDB_NAME,
597 						 ATH12K_BD_IE_REGDB_DATA);
598 	if (!ret)
599 		goto exit;
600 
601 	ret = ath12k_core_create_bus_type_board_name(ab, default_boardname,
602 						     BOARD_NAME_SIZE);
603 	if (ret) {
604 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
605 			   "failed to create default board name for regdb: %d", ret);
606 		goto exit;
607 	}
608 
609 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, default_boardname,
610 						 ATH12K_BD_IE_REGDB,
611 						 ATH12K_BD_IE_REGDB_NAME,
612 						 ATH12K_BD_IE_REGDB_DATA);
613 	if (!ret)
614 		goto exit;
615 
616 	ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_REGDB_FILE_NAME);
617 	if (ret)
618 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "failed to fetch %s from %s\n",
619 			   ATH12K_REGDB_FILE_NAME, ab->hw_params->fw.dir);
620 
621 exit:
622 	if (!ret)
623 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "fetched regdb\n");
624 
625 	return ret;
626 }
627 
ath12k_core_get_max_station_per_radio(struct ath12k_base * ab)628 u32 ath12k_core_get_max_station_per_radio(struct ath12k_base *ab)
629 {
630 	if (ab->num_radios == 2)
631 		return TARGET_NUM_STATIONS(ab, DBS);
632 	if (ab->num_radios == 3)
633 		return TARGET_NUM_STATIONS(ab, DBS_SBS);
634 	return TARGET_NUM_STATIONS(ab, SINGLE);
635 }
636 
ath12k_core_get_max_peers_per_radio(struct ath12k_base * ab)637 u32 ath12k_core_get_max_peers_per_radio(struct ath12k_base *ab)
638 {
639 	return ath12k_core_get_max_station_per_radio(ab) + TARGET_NUM_VDEVS(ab);
640 }
641 EXPORT_SYMBOL(ath12k_core_get_max_peers_per_radio);
642 
643 static inline
ath12k_core_to_group_ref_get(struct ath12k_base * ab)644 void ath12k_core_to_group_ref_get(struct ath12k_base *ab)
645 {
646 	struct ath12k_hw_group *ag = ab->ag;
647 
648 	lockdep_assert_held(&ag->mutex);
649 
650 	if (ab->hw_group_ref) {
651 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "core already attached to group %d\n",
652 			   ag->id);
653 		return;
654 	}
655 
656 	ab->hw_group_ref = true;
657 	ag->num_started++;
658 
659 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "core attached to group %d, num_started %d\n",
660 		   ag->id, ag->num_started);
661 }
662 
663 static inline
ath12k_core_to_group_ref_put(struct ath12k_base * ab)664 void ath12k_core_to_group_ref_put(struct ath12k_base *ab)
665 {
666 	struct ath12k_hw_group *ag = ab->ag;
667 
668 	lockdep_assert_held(&ag->mutex);
669 
670 	if (!ab->hw_group_ref) {
671 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "core already de-attached from group %d\n",
672 			   ag->id);
673 		return;
674 	}
675 
676 	ab->hw_group_ref = false;
677 	ag->num_started--;
678 
679 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "core de-attached from group %d, num_started %d\n",
680 		   ag->id, ag->num_started);
681 }
682 
ath12k_core_stop(struct ath12k_base * ab)683 static void ath12k_core_stop(struct ath12k_base *ab)
684 {
685 	ath12k_link_sta_rhash_tbl_destroy(ab);
686 
687 	ath12k_core_to_group_ref_put(ab);
688 
689 	if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags)) {
690 		ath12k_dp_reoq_lut_addr_reset(ath12k_ab_to_dp(ab));
691 		ath12k_qmi_firmware_stop(ab);
692 	}
693 
694 	ath12k_acpi_stop(ab);
695 
696 	ath12k_dp_rx_pdev_reo_cleanup(ab);
697 	ath12k_hif_stop(ab);
698 	ath12k_wmi_detach(ab);
699 	ath12k_dp_cmn_device_deinit(ath12k_ab_to_dp(ab));
700 
701 	/* De-Init of components as needed */
702 }
703 
ath12k_core_check_cc_code_bdfext(const struct dmi_header * hdr,void * data)704 static void ath12k_core_check_cc_code_bdfext(const struct dmi_header *hdr, void *data)
705 {
706 	struct ath12k_base *ab = data;
707 	const char *magic = ATH12K_SMBIOS_BDF_EXT_MAGIC;
708 	struct ath12k_smbios_bdf *smbios = (struct ath12k_smbios_bdf *)hdr;
709 	ssize_t copied;
710 	size_t len;
711 	int i;
712 
713 	if (ab->qmi.target.bdf_ext[0] != '\0')
714 		return;
715 
716 	if (hdr->type != ATH12K_SMBIOS_BDF_EXT_TYPE)
717 		return;
718 
719 	if (hdr->length != ATH12K_SMBIOS_BDF_EXT_LENGTH) {
720 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
721 			   "wrong smbios bdf ext type length (%d).\n",
722 			   hdr->length);
723 		return;
724 	}
725 
726 	spin_lock_bh(&ab->base_lock);
727 
728 	switch (smbios->country_code_flag) {
729 	case ATH12K_SMBIOS_CC_ISO:
730 		ab->new_alpha2[0] = u16_get_bits(smbios->cc_code >> 8, 0xff);
731 		ab->new_alpha2[1] = u16_get_bits(smbios->cc_code, 0xff);
732 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot smbios cc_code %c%c\n",
733 			   ab->new_alpha2[0], ab->new_alpha2[1]);
734 		break;
735 	case ATH12K_SMBIOS_CC_WW:
736 		ab->new_alpha2[0] = '0';
737 		ab->new_alpha2[1] = '0';
738 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot smbios worldwide regdomain\n");
739 		break;
740 	default:
741 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot ignore smbios country code setting %d\n",
742 			   smbios->country_code_flag);
743 		break;
744 	}
745 
746 	spin_unlock_bh(&ab->base_lock);
747 
748 	if (!smbios->bdf_enabled) {
749 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "bdf variant name not found.\n");
750 		return;
751 	}
752 
753 	/* Only one string exists (per spec) */
754 	if (memcmp(smbios->bdf_ext, magic, strlen(magic)) != 0) {
755 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
756 			   "bdf variant magic does not match.\n");
757 		return;
758 	}
759 
760 	len = min_t(size_t,
761 		    strlen(smbios->bdf_ext), sizeof(ab->qmi.target.bdf_ext));
762 	for (i = 0; i < len; i++) {
763 		if (!isascii(smbios->bdf_ext[i]) || !isprint(smbios->bdf_ext[i])) {
764 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
765 				   "bdf variant name contains non ascii chars.\n");
766 			return;
767 		}
768 	}
769 
770 	/* Copy extension name without magic prefix */
771 	copied = strscpy(ab->qmi.target.bdf_ext, smbios->bdf_ext + strlen(magic),
772 			 sizeof(ab->qmi.target.bdf_ext));
773 	if (copied < 0) {
774 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
775 			   "bdf variant string is longer than the buffer can accommodate\n");
776 		return;
777 	}
778 
779 	ath12k_dbg(ab, ATH12K_DBG_BOOT,
780 		   "found and validated bdf variant smbios_type 0x%x bdf %s\n",
781 		   ATH12K_SMBIOS_BDF_EXT_TYPE, ab->qmi.target.bdf_ext);
782 }
783 
ath12k_core_check_smbios(struct ath12k_base * ab)784 int ath12k_core_check_smbios(struct ath12k_base *ab)
785 {
786 	ab->qmi.target.bdf_ext[0] = '\0';
787 	dmi_walk(ath12k_core_check_cc_code_bdfext, ab);
788 
789 	if (ab->qmi.target.bdf_ext[0] == '\0')
790 		return -ENODATA;
791 
792 	return 0;
793 }
794 
ath12k_core_soc_create(struct ath12k_base * ab)795 static int ath12k_core_soc_create(struct ath12k_base *ab)
796 {
797 	int ret;
798 
799 	if (ath12k_ftm_mode) {
800 		ab->fw_mode = ATH12K_QMI_FIRMWARE_MODE_FTM;
801 		ath12k_info(ab, "Booting in ftm mode\n");
802 	}
803 
804 	ret = ath12k_qmi_init_service(ab);
805 	if (ret) {
806 		ath12k_err(ab, "failed to initialize qmi :%d\n", ret);
807 		return ret;
808 	}
809 
810 	ath12k_debugfs_soc_create(ab);
811 
812 	ret = ath12k_hif_power_up(ab);
813 	if (ret) {
814 		ath12k_err(ab, "failed to power up :%d\n", ret);
815 		goto err_qmi_deinit;
816 	}
817 
818 	return 0;
819 
820 err_qmi_deinit:
821 	ath12k_debugfs_soc_destroy(ab);
822 	ath12k_qmi_deinit_service(ab);
823 	return ret;
824 }
825 
ath12k_core_soc_destroy(struct ath12k_base * ab)826 static void ath12k_core_soc_destroy(struct ath12k_base *ab)
827 {
828 	ath12k_hif_power_down(ab, false);
829 	ath12k_reg_free(ab);
830 	ath12k_debugfs_soc_destroy(ab);
831 	ath12k_qmi_deinit_service(ab);
832 }
833 
ath12k_core_pdev_create(struct ath12k_base * ab)834 static int ath12k_core_pdev_create(struct ath12k_base *ab)
835 {
836 	int ret;
837 
838 	ret = ath12k_dp_pdev_alloc(ab);
839 	if (ret) {
840 		ath12k_err(ab, "failed to attach DP pdev: %d\n", ret);
841 		return ret;
842 	}
843 
844 	ret = ath12k_thermal_register(ab);
845 	if (ret) {
846 		ath12k_err(ab, "could not register thermal device: %d\n", ret);
847 		goto err_dp_pdev_free;
848 	}
849 
850 	ath12k_debugfs_pdev_create(ab);
851 
852 	return 0;
853 
854 err_dp_pdev_free:
855 	ath12k_dp_pdev_free(ab);
856 	return ret;
857 }
858 
ath12k_core_pdev_destroy(struct ath12k_base * ab)859 static void ath12k_core_pdev_destroy(struct ath12k_base *ab)
860 {
861 	ath12k_thermal_unregister(ab);
862 	ath12k_dp_pdev_free(ab);
863 }
864 
ath12k_core_start(struct ath12k_base * ab)865 static int ath12k_core_start(struct ath12k_base *ab)
866 {
867 	int ret;
868 
869 	lockdep_assert_held(&ab->core_lock);
870 
871 	ret = ath12k_wmi_attach(ab);
872 	if (ret) {
873 		ath12k_err(ab, "failed to attach wmi: %d\n", ret);
874 		return ret;
875 	}
876 
877 	ret = ath12k_htc_init(ab);
878 	if (ret) {
879 		ath12k_err(ab, "failed to init htc: %d\n", ret);
880 		goto err_wmi_detach;
881 	}
882 
883 	ret = ath12k_hif_start(ab);
884 	if (ret) {
885 		ath12k_err(ab, "failed to start HIF: %d\n", ret);
886 		goto err_wmi_detach;
887 	}
888 
889 	ret = ath12k_htc_wait_target(&ab->htc);
890 	if (ret) {
891 		ath12k_err(ab, "failed to connect to HTC: %d\n", ret);
892 		goto err_hif_stop;
893 	}
894 
895 	ret = ath12k_dp_htt_connect(ath12k_ab_to_dp(ab));
896 	if (ret) {
897 		ath12k_err(ab, "failed to connect to HTT: %d\n", ret);
898 		goto err_hif_stop;
899 	}
900 
901 	ret = ath12k_wmi_connect(ab);
902 	if (ret) {
903 		ath12k_err(ab, "failed to connect wmi: %d\n", ret);
904 		goto err_hif_stop;
905 	}
906 
907 	ret = ath12k_htc_start(&ab->htc);
908 	if (ret) {
909 		ath12k_err(ab, "failed to start HTC: %d\n", ret);
910 		goto err_hif_stop;
911 	}
912 
913 	ret = ath12k_wmi_wait_for_service_ready(ab);
914 	if (ret) {
915 		ath12k_err(ab, "failed to receive wmi service ready event: %d\n",
916 			   ret);
917 		goto err_hif_stop;
918 	}
919 
920 	ath12k_hal_cc_config(ab);
921 
922 	ret = ath12k_dp_rx_pdev_reo_setup(ab);
923 	if (ret) {
924 		ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
925 		goto err_hif_stop;
926 	}
927 
928 	ret = ath12k_wmi_cmd_init(ab);
929 	if (ret) {
930 		ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret);
931 		goto err_reo_cleanup;
932 	}
933 
934 	ret = ath12k_wmi_wait_for_unified_ready(ab);
935 	if (ret) {
936 		ath12k_err(ab, "failed to receive wmi unified ready event: %d\n",
937 			   ret);
938 		goto err_reo_cleanup;
939 	}
940 
941 	/* put hardware to DBS mode */
942 	if (ab->hw_params->single_pdev_only) {
943 		ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS);
944 		if (ret) {
945 			ath12k_err(ab, "failed to send dbs mode: %d\n", ret);
946 			goto err_reo_cleanup;
947 		}
948 	}
949 
950 	ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab);
951 	if (ret) {
952 		ath12k_err(ab, "failed to send htt version request message: %d\n",
953 			   ret);
954 		goto err_reo_cleanup;
955 	}
956 
957 	ath12k_acpi_set_dsm_func(ab);
958 
959 	/* Indicate the core start in the appropriate group */
960 	ath12k_core_to_group_ref_get(ab);
961 
962 	ret = ath12k_link_sta_rhash_tbl_init(ab);
963 	if (ret) {
964 		ath12k_warn(ab, "failed to init peer addr rhash table %d\n", ret);
965 		goto err_reo_cleanup;
966 	}
967 
968 	return 0;
969 
970 err_reo_cleanup:
971 	ath12k_dp_rx_pdev_reo_cleanup(ab);
972 err_hif_stop:
973 	ath12k_hif_stop(ab);
974 err_wmi_detach:
975 	ath12k_wmi_detach(ab);
976 	return ret;
977 }
978 
ath12k_core_device_cleanup(struct ath12k_base * ab)979 static void ath12k_core_device_cleanup(struct ath12k_base *ab)
980 {
981 	mutex_lock(&ab->core_lock);
982 
983 	ath12k_hif_irq_disable(ab);
984 	ath12k_core_pdev_destroy(ab);
985 
986 	mutex_unlock(&ab->core_lock);
987 }
988 
ath12k_core_device_setup(struct ath12k_base * ab)989 static int ath12k_core_device_setup(struct ath12k_base *ab)
990 {
991 	int ret;
992 
993 	guard(mutex)(&ab->core_lock);
994 
995 	ret = ath12k_core_pdev_create(ab);
996 	if (ret) {
997 		ath12k_err(ab, "failed to create pdev core %d\n", ret);
998 		return ret;
999 	}
1000 
1001 	ath12k_hif_irq_enable(ab);
1002 
1003 	ret = ath12k_core_rfkill_config(ab);
1004 	if (ret && ret != -EOPNOTSUPP)
1005 		return ret;
1006 
1007 	return 0;
1008 }
1009 
ath12k_core_hw_group_stop(struct ath12k_hw_group * ag)1010 static void ath12k_core_hw_group_stop(struct ath12k_hw_group *ag)
1011 {
1012 	struct ath12k_base *ab;
1013 	int i;
1014 
1015 	lockdep_assert_held(&ag->mutex);
1016 
1017 	clear_bit(ATH12K_GROUP_FLAG_REGISTERED, &ag->flags);
1018 
1019 	for (i = ag->num_devices - 1; i >= 0; i--) {
1020 		ab = ag->ab[i];
1021 		if (!ab)
1022 			continue;
1023 
1024 		clear_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags);
1025 
1026 		ath12k_core_device_cleanup(ab);
1027 	}
1028 
1029 	/* Unregister MAC (drops wiphys) only after per-device cleanup */
1030 	ath12k_mac_unregister(ag);
1031 
1032 	/* Teardown MLO state after MAC unregister for symmetry */
1033 	ath12k_mac_mlo_teardown(ag);
1034 
1035 	ath12k_mac_destroy(ag);
1036 }
1037 
ath12k_get_num_partner_link(struct ath12k * ar)1038 u8 ath12k_get_num_partner_link(struct ath12k *ar)
1039 {
1040 	struct ath12k_base *partner_ab, *ab = ar->ab;
1041 	struct ath12k_hw_group *ag = ab->ag;
1042 	struct ath12k_pdev *pdev;
1043 	u8 num_link = 0;
1044 	int i, j;
1045 
1046 	lockdep_assert_held(&ag->mutex);
1047 
1048 	for (i = 0; i < ag->num_devices; i++) {
1049 		partner_ab = ag->ab[i];
1050 
1051 		for (j = 0; j < partner_ab->num_radios; j++) {
1052 			pdev = &partner_ab->pdevs[j];
1053 
1054 			/* Avoid the self link */
1055 			if (ar == pdev->ar)
1056 				continue;
1057 
1058 			num_link++;
1059 		}
1060 	}
1061 
1062 	return num_link;
1063 }
1064 
__ath12k_mac_mlo_ready(struct ath12k * ar)1065 static int __ath12k_mac_mlo_ready(struct ath12k *ar)
1066 {
1067 	u8 num_link = ath12k_get_num_partner_link(ar);
1068 	int ret;
1069 
1070 	if (num_link == 0)
1071 		return 0;
1072 
1073 	ret = ath12k_wmi_mlo_ready(ar);
1074 	if (ret) {
1075 		ath12k_err(ar->ab, "MLO ready failed for pdev %d: %d\n",
1076 			   ar->pdev_idx, ret);
1077 		return ret;
1078 	}
1079 
1080 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mlo ready done for pdev %d\n",
1081 		   ar->pdev_idx);
1082 
1083 	return 0;
1084 }
1085 
ath12k_mac_mlo_ready(struct ath12k_hw_group * ag)1086 int ath12k_mac_mlo_ready(struct ath12k_hw_group *ag)
1087 {
1088 	struct ath12k_hw *ah;
1089 	struct ath12k *ar;
1090 	int ret;
1091 	int i, j;
1092 
1093 	for (i = 0; i < ag->num_hw; i++) {
1094 		ah = ag->ah[i];
1095 		if (!ah)
1096 			continue;
1097 
1098 		for_each_ar(ah, ar, j) {
1099 			ar = &ah->radio[j];
1100 			ret = __ath12k_mac_mlo_ready(ar);
1101 			if (ret)
1102 				return ret;
1103 		}
1104 	}
1105 
1106 	return 0;
1107 }
1108 
ath12k_core_mlo_setup(struct ath12k_hw_group * ag)1109 static int ath12k_core_mlo_setup(struct ath12k_hw_group *ag)
1110 {
1111 	int ret, i;
1112 
1113 	if (!ag->mlo_capable)
1114 		return 0;
1115 
1116 	ret = ath12k_mac_mlo_setup(ag);
1117 	if (ret)
1118 		return ret;
1119 
1120 	for (i = 0; i < ag->num_devices; i++)
1121 		ath12k_dp_partner_cc_init(ag->ab[i]);
1122 
1123 	ret = ath12k_mac_mlo_ready(ag);
1124 	if (ret)
1125 		goto err_mlo_teardown;
1126 
1127 	return 0;
1128 
1129 err_mlo_teardown:
1130 	ath12k_mac_mlo_teardown(ag);
1131 
1132 	return ret;
1133 }
1134 
ath12k_core_hw_group_start(struct ath12k_hw_group * ag)1135 static int ath12k_core_hw_group_start(struct ath12k_hw_group *ag)
1136 {
1137 	struct ath12k_base *ab;
1138 	int ret, i;
1139 
1140 	lockdep_assert_held(&ag->mutex);
1141 
1142 	if (test_bit(ATH12K_GROUP_FLAG_REGISTERED, &ag->flags)) {
1143 		ret = ath12k_core_mlo_setup(ag);
1144 		if (WARN_ON(ret)) {
1145 			ath12k_mac_unregister(ag);
1146 			goto err_mac_destroy;
1147 		}
1148 		goto core_pdev_create;
1149 	}
1150 
1151 	ret = ath12k_mac_allocate(ag);
1152 	if (WARN_ON(ret))
1153 		return ret;
1154 
1155 	ret = ath12k_core_mlo_setup(ag);
1156 	if (WARN_ON(ret))
1157 		goto err_mac_destroy;
1158 
1159 	ret = ath12k_mac_register(ag);
1160 	if (WARN_ON(ret))
1161 		goto err_mlo_teardown;
1162 
1163 	set_bit(ATH12K_GROUP_FLAG_REGISTERED, &ag->flags);
1164 
1165 core_pdev_create:
1166 	for (i = 0; i < ag->num_devices; i++) {
1167 		ab = ag->ab[i];
1168 		if (!ab)
1169 			continue;
1170 
1171 		set_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags);
1172 
1173 		ret = ath12k_core_device_setup(ab);
1174 		if (ret)
1175 			goto err;
1176 	}
1177 
1178 	return 0;
1179 
1180 err:
1181 	ath12k_core_hw_group_stop(ag);
1182 	return ret;
1183 
1184 err_mlo_teardown:
1185 	ath12k_mac_mlo_teardown(ag);
1186 
1187 err_mac_destroy:
1188 	ath12k_mac_destroy(ag);
1189 
1190 	return ret;
1191 }
1192 
ath12k_core_start_firmware(struct ath12k_base * ab,enum ath12k_qmi_firmware_mode mode)1193 static int ath12k_core_start_firmware(struct ath12k_base *ab,
1194 				      enum ath12k_qmi_firmware_mode mode)
1195 {
1196 	int ret;
1197 
1198 	ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3,
1199 				    &ab->qmi.ce_cfg.shadow_reg_v3_len);
1200 
1201 	ret = ath12k_qmi_firmware_start(ab, mode);
1202 	if (ret) {
1203 		ath12k_err(ab, "failed to send firmware start: %d\n", ret);
1204 		return ret;
1205 	}
1206 
1207 	return ret;
1208 }
1209 
1210 static inline
ath12k_core_hw_group_start_ready(struct ath12k_hw_group * ag)1211 bool ath12k_core_hw_group_start_ready(struct ath12k_hw_group *ag)
1212 {
1213 	lockdep_assert_held(&ag->mutex);
1214 
1215 	return (ag->num_started == ag->num_devices);
1216 }
1217 
ath12k_fw_stats_pdevs_free(struct list_head * head)1218 static void ath12k_fw_stats_pdevs_free(struct list_head *head)
1219 {
1220 	struct ath12k_fw_stats_pdev *i, *tmp;
1221 
1222 	list_for_each_entry_safe(i, tmp, head, list) {
1223 		list_del(&i->list);
1224 		kfree(i);
1225 	}
1226 }
1227 
ath12k_fw_stats_bcn_free(struct list_head * head)1228 void ath12k_fw_stats_bcn_free(struct list_head *head)
1229 {
1230 	struct ath12k_fw_stats_bcn *i, *tmp;
1231 
1232 	list_for_each_entry_safe(i, tmp, head, list) {
1233 		list_del(&i->list);
1234 		kfree(i);
1235 	}
1236 }
1237 
ath12k_fw_stats_vdevs_free(struct list_head * head)1238 static void ath12k_fw_stats_vdevs_free(struct list_head *head)
1239 {
1240 	struct ath12k_fw_stats_vdev *i, *tmp;
1241 
1242 	list_for_each_entry_safe(i, tmp, head, list) {
1243 		list_del(&i->list);
1244 		kfree(i);
1245 	}
1246 }
1247 
ath12k_fw_stats_init(struct ath12k * ar)1248 void ath12k_fw_stats_init(struct ath12k *ar)
1249 {
1250 	INIT_LIST_HEAD(&ar->fw_stats.vdevs);
1251 	INIT_LIST_HEAD(&ar->fw_stats.pdevs);
1252 	INIT_LIST_HEAD(&ar->fw_stats.bcn);
1253 	init_completion(&ar->fw_stats_complete);
1254 	init_completion(&ar->fw_stats_done);
1255 }
1256 
ath12k_fw_stats_free(struct ath12k_fw_stats * stats)1257 void ath12k_fw_stats_free(struct ath12k_fw_stats *stats)
1258 {
1259 	ath12k_fw_stats_pdevs_free(&stats->pdevs);
1260 	ath12k_fw_stats_vdevs_free(&stats->vdevs);
1261 	ath12k_fw_stats_bcn_free(&stats->bcn);
1262 }
1263 
ath12k_fw_stats_reset(struct ath12k * ar)1264 void ath12k_fw_stats_reset(struct ath12k *ar)
1265 {
1266 	spin_lock_bh(&ar->data_lock);
1267 	ath12k_fw_stats_free(&ar->fw_stats);
1268 	ar->fw_stats.num_vdev_recvd = 0;
1269 	spin_unlock_bh(&ar->data_lock);
1270 }
1271 
ath12k_core_trigger_partner(struct ath12k_base * ab)1272 static void ath12k_core_trigger_partner(struct ath12k_base *ab)
1273 {
1274 	struct ath12k_hw_group *ag = ab->ag;
1275 	struct ath12k_base *partner_ab;
1276 	bool found = false;
1277 	int i;
1278 
1279 	for (i = 0; i < ag->num_devices; i++) {
1280 		partner_ab = ag->ab[i];
1281 		if (!partner_ab)
1282 			continue;
1283 
1284 		if (found)
1285 			ath12k_qmi_trigger_host_cap(partner_ab);
1286 
1287 		found = (partner_ab == ab);
1288 	}
1289 }
1290 
ath12k_core_qmi_firmware_ready(struct ath12k_base * ab)1291 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab)
1292 {
1293 	struct ath12k_hw_group *ag = ath12k_ab_to_ag(ab);
1294 	int ret, i;
1295 
1296 	ret = ath12k_core_start_firmware(ab, ab->fw_mode);
1297 	if (ret) {
1298 		ath12k_err(ab, "failed to start firmware: %d\n", ret);
1299 		return ret;
1300 	}
1301 
1302 	ret = ath12k_ce_init_pipes(ab);
1303 	if (ret) {
1304 		ath12k_err(ab, "failed to initialize CE: %d\n", ret);
1305 		goto err_firmware_stop;
1306 	}
1307 
1308 	ret = ath12k_dp_cmn_device_init(ath12k_ab_to_dp(ab));
1309 	if (ret) {
1310 		ath12k_err(ab, "failed to init DP: %d\n", ret);
1311 		goto err_firmware_stop;
1312 	}
1313 
1314 	mutex_lock(&ag->mutex);
1315 	mutex_lock(&ab->core_lock);
1316 
1317 	ret = ath12k_core_start(ab);
1318 	if (ret) {
1319 		ath12k_err(ab, "failed to start core: %d\n", ret);
1320 		goto err_deinit;
1321 	}
1322 
1323 	mutex_unlock(&ab->core_lock);
1324 
1325 	if (ath12k_core_hw_group_start_ready(ag)) {
1326 		ret = ath12k_core_hw_group_start(ag);
1327 		if (ret) {
1328 			ath12k_warn(ab, "unable to start hw group\n");
1329 			goto err_core_stop;
1330 		}
1331 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "group %d started\n", ag->id);
1332 	} else {
1333 		ath12k_core_trigger_partner(ab);
1334 	}
1335 
1336 	mutex_unlock(&ag->mutex);
1337 
1338 	return 0;
1339 
1340 err_core_stop:
1341 	for (i = ag->num_devices - 1; i >= 0; i--) {
1342 		ab = ag->ab[i];
1343 		if (!ab)
1344 			continue;
1345 
1346 		mutex_lock(&ab->core_lock);
1347 		ath12k_core_stop(ab);
1348 		mutex_unlock(&ab->core_lock);
1349 	}
1350 	mutex_unlock(&ag->mutex);
1351 	goto exit;
1352 
1353 err_deinit:
1354 	ath12k_dp_reoq_lut_addr_reset(ath12k_ab_to_dp(ab));
1355 	ath12k_dp_cmn_device_deinit(ath12k_ab_to_dp(ab));
1356 	mutex_unlock(&ab->core_lock);
1357 	mutex_unlock(&ag->mutex);
1358 
1359 err_firmware_stop:
1360 	ath12k_qmi_firmware_stop(ab);
1361 
1362 exit:
1363 	return ret;
1364 }
1365 
ath12k_core_reconfigure_on_crash(struct ath12k_base * ab)1366 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab)
1367 {
1368 	int ret, total_vdev;
1369 
1370 	mutex_lock(&ab->core_lock);
1371 	ath12k_link_sta_rhash_tbl_destroy(ab);
1372 	ath12k_thermal_unregister(ab);
1373 	ath12k_dp_pdev_free(ab);
1374 	ath12k_ce_cleanup_pipes(ab);
1375 	ath12k_wmi_detach(ab);
1376 	ath12k_dp_rx_pdev_reo_cleanup(ab);
1377 	mutex_unlock(&ab->core_lock);
1378 
1379 	ath12k_dp_cmn_device_deinit(ath12k_ab_to_dp(ab));
1380 	ath12k_hal_srng_deinit(ab);
1381 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
1382 	ab->free_vdev_map = (1LL << total_vdev) - 1;
1383 
1384 	ret = ath12k_hal_srng_init(ab);
1385 	if (ret)
1386 		return ret;
1387 
1388 	clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
1389 
1390 	ret = ath12k_core_qmi_firmware_ready(ab);
1391 	if (ret)
1392 		goto err_hal_srng_deinit;
1393 
1394 	clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags);
1395 
1396 	return 0;
1397 
1398 err_hal_srng_deinit:
1399 	ath12k_hal_srng_deinit(ab);
1400 	return ret;
1401 }
1402 
ath12k_rfkill_work(struct work_struct * work)1403 static void ath12k_rfkill_work(struct work_struct *work)
1404 {
1405 	struct ath12k_base *ab = container_of(work, struct ath12k_base, rfkill_work);
1406 	struct ath12k_hw_group *ag = ab->ag;
1407 	struct ath12k *ar;
1408 	struct ath12k_hw *ah;
1409 	struct ieee80211_hw *hw;
1410 	bool rfkill_radio_on;
1411 	int i, j;
1412 
1413 	spin_lock_bh(&ab->base_lock);
1414 	rfkill_radio_on = ab->rfkill_radio_on;
1415 	spin_unlock_bh(&ab->base_lock);
1416 
1417 	for (i = 0; i < ag->num_hw; i++) {
1418 		ah = ath12k_ag_to_ah(ag, i);
1419 		if (!ah)
1420 			continue;
1421 
1422 		for (j = 0; j < ah->num_radio; j++) {
1423 			ar = &ah->radio[j];
1424 			if (!ar)
1425 				continue;
1426 
1427 			ath12k_mac_rfkill_enable_radio(ar, rfkill_radio_on);
1428 		}
1429 
1430 		hw = ah->hw;
1431 		wiphy_rfkill_set_hw_state(hw->wiphy, !rfkill_radio_on);
1432 	}
1433 }
1434 
ath12k_core_halt(struct ath12k * ar)1435 void ath12k_core_halt(struct ath12k *ar)
1436 {
1437 	struct list_head *pos, *n;
1438 	struct ath12k_base *ab = ar->ab;
1439 
1440 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1441 
1442 	ar->num_created_vdevs = 0;
1443 	ar->allocated_vdev_map = 0;
1444 
1445 	ath12k_mac_scan_finish(ar);
1446 	ath12k_mac_peer_cleanup_all(ar);
1447 	cancel_delayed_work_sync(&ar->scan.timeout);
1448 	cancel_work_sync(&ar->regd_update_work);
1449 	cancel_work_sync(&ar->regd_channel_update_work);
1450 	cancel_work_sync(&ab->rfkill_work);
1451 	cancel_work_sync(&ab->update_11d_work);
1452 
1453 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
1454 	synchronize_rcu();
1455 
1456 	spin_lock_bh(&ar->data_lock);
1457 	list_for_each_safe(pos, n, &ar->arvifs)
1458 		list_del_init(pos);
1459 	spin_unlock_bh(&ar->data_lock);
1460 
1461 	idr_init(&ar->txmgmt_idr);
1462 }
1463 
ath12k_core_pre_reconfigure_recovery(struct ath12k_base * ab)1464 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab)
1465 {
1466 	struct ath12k_hw_group *ag = ab->ag;
1467 	struct ath12k *ar;
1468 	struct ath12k_hw *ah;
1469 	int i, j;
1470 
1471 	spin_lock_bh(&ab->base_lock);
1472 	ab->stats.fw_crash_counter++;
1473 	spin_unlock_bh(&ab->base_lock);
1474 
1475 	if (ab->is_reset)
1476 		set_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
1477 
1478 	for (i = 0; i < ag->num_hw; i++) {
1479 		ah = ath12k_ag_to_ah(ag, i);
1480 		if (!ah || ah->state == ATH12K_HW_STATE_OFF ||
1481 		    ah->state == ATH12K_HW_STATE_TM)
1482 			continue;
1483 
1484 		wiphy_lock(ah->hw->wiphy);
1485 
1486 		/* If queue 0 is stopped, it is safe to assume that all
1487 		 * other queues are stopped by driver via
1488 		 * ieee80211_stop_queues() below. This means, there is
1489 		 * no need to stop it again and hence continue
1490 		 */
1491 		if (ieee80211_queue_stopped(ah->hw, 0)) {
1492 			wiphy_unlock(ah->hw->wiphy);
1493 			continue;
1494 		}
1495 
1496 		ieee80211_stop_queues(ah->hw);
1497 
1498 		for (j = 0; j < ah->num_radio; j++) {
1499 			ar = &ah->radio[j];
1500 
1501 			ath12k_mac_drain_tx(ar);
1502 			ar->state_11d = ATH12K_11D_IDLE;
1503 			complete(&ar->completed_11d_scan);
1504 			complete(&ar->scan.started);
1505 			complete_all(&ar->scan.completed);
1506 			complete(&ar->scan.on_channel);
1507 			complete(&ar->peer_assoc_done);
1508 			ath12k_peer_delete_wait_flush(ar);
1509 			complete(&ar->install_key_done);
1510 			complete(&ar->vdev_setup_done);
1511 			complete(&ar->vdev_delete_done);
1512 			complete(&ar->bss_survey_done);
1513 			complete_all(&ar->regd_update_completed);
1514 			complete_all(&ar->thermal.wmi_sync);
1515 
1516 			wake_up(&ar->dp.tx_empty_waitq);
1517 			idr_for_each(&ar->txmgmt_idr,
1518 				     ath12k_mac_tx_mgmt_pending_free, ar);
1519 			idr_destroy(&ar->txmgmt_idr);
1520 			wake_up(&ar->txmgmt_empty_waitq);
1521 
1522 			ar->monitor_vdev_id = -1;
1523 			ar->monitor_vdev_created = false;
1524 			ar->monitor_started = false;
1525 		}
1526 
1527 		wiphy_unlock(ah->hw->wiphy);
1528 
1529 		complete(&ah->peer_ml_id_done);
1530 	}
1531 
1532 	wake_up(&ab->wmi_ab.tx_credits_wq);
1533 	wake_up(&ab->peer_mapping_wq);
1534 }
1535 
ath12k_update_11d(struct work_struct * work)1536 static void ath12k_update_11d(struct work_struct *work)
1537 {
1538 	struct ath12k_base *ab = container_of(work, struct ath12k_base, update_11d_work);
1539 	struct ath12k *ar;
1540 	struct ath12k_pdev *pdev;
1541 	struct wmi_set_current_country_arg arg = {};
1542 	int ret, i;
1543 
1544 	spin_lock_bh(&ab->base_lock);
1545 	memcpy(&arg.alpha2, &ab->new_alpha2, 2);
1546 	spin_unlock_bh(&ab->base_lock);
1547 
1548 	ath12k_dbg(ab, ATH12K_DBG_WMI, "update 11d new cc %c%c\n",
1549 		   arg.alpha2[0], arg.alpha2[1]);
1550 
1551 	for (i = 0; i < ab->num_radios; i++) {
1552 		pdev = &ab->pdevs[i];
1553 		ar = pdev->ar;
1554 
1555 		memcpy(&ar->alpha2, &arg.alpha2, 2);
1556 
1557 		reinit_completion(&ar->regd_update_completed);
1558 
1559 		ret = ath12k_wmi_send_set_current_country_cmd(ar, &arg);
1560 		if (ret)
1561 			ath12k_warn(ar->ab,
1562 				    "pdev id %d failed set current country code: %d\n",
1563 				    i, ret);
1564 	}
1565 }
1566 
ath12k_core_post_reconfigure_recovery(struct ath12k_base * ab)1567 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab)
1568 {
1569 	struct ath12k_hw_group *ag = ab->ag;
1570 	struct ath12k_hw *ah;
1571 	struct ath12k *ar;
1572 	int i, j;
1573 
1574 	for (i = 0; i < ag->num_hw; i++) {
1575 		ah = ath12k_ag_to_ah(ag, i);
1576 		if (!ah || ah->state == ATH12K_HW_STATE_OFF)
1577 			continue;
1578 
1579 		wiphy_lock(ah->hw->wiphy);
1580 		mutex_lock(&ah->hw_mutex);
1581 
1582 		switch (ah->state) {
1583 		case ATH12K_HW_STATE_ON:
1584 			ah->state = ATH12K_HW_STATE_RESTARTING;
1585 
1586 			for (j = 0; j < ah->num_radio; j++) {
1587 				ar = &ah->radio[j];
1588 				ath12k_core_halt(ar);
1589 			}
1590 
1591 			ath12k_mac_dp_peer_cleanup(ah);
1592 			break;
1593 		case ATH12K_HW_STATE_OFF:
1594 			ath12k_warn(ab,
1595 				    "cannot restart hw %d that hasn't been started\n",
1596 				    i);
1597 			break;
1598 		case ATH12K_HW_STATE_RESTARTING:
1599 			break;
1600 		case ATH12K_HW_STATE_RESTARTED:
1601 			ah->state = ATH12K_HW_STATE_WEDGED;
1602 			fallthrough;
1603 		case ATH12K_HW_STATE_WEDGED:
1604 			ath12k_warn(ab,
1605 				    "device is wedged, will not restart hw %d\n", i);
1606 			break;
1607 		case ATH12K_HW_STATE_TM:
1608 			ath12k_warn(ab, "fw mode reset done radio %d\n", i);
1609 			break;
1610 		}
1611 
1612 		mutex_unlock(&ah->hw_mutex);
1613 		wiphy_unlock(ah->hw->wiphy);
1614 	}
1615 
1616 	complete(&ab->driver_recovery);
1617 }
1618 
ath12k_core_restart(struct work_struct * work)1619 static void ath12k_core_restart(struct work_struct *work)
1620 {
1621 	struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work);
1622 	struct ath12k_hw_group *ag = ab->ag;
1623 	struct ath12k_hw *ah;
1624 	int ret, i;
1625 
1626 	ret = ath12k_core_reconfigure_on_crash(ab);
1627 	if (ret) {
1628 		ath12k_err(ab, "failed to reconfigure driver on crash recovery\n");
1629 		return;
1630 	}
1631 
1632 	if (ab->is_reset) {
1633 		if (!test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags)) {
1634 			atomic_dec(&ab->reset_count);
1635 			complete(&ab->reset_complete);
1636 			ab->is_reset = false;
1637 			atomic_set(&ab->fail_cont_count, 0);
1638 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
1639 		}
1640 
1641 		mutex_lock(&ag->mutex);
1642 
1643 		if (!ath12k_core_hw_group_start_ready(ag)) {
1644 			mutex_unlock(&ag->mutex);
1645 			goto exit_restart;
1646 		}
1647 
1648 		for (i = 0; i < ag->num_hw; i++) {
1649 			ah = ath12k_ag_to_ah(ag, i);
1650 			ieee80211_restart_hw(ah->hw);
1651 		}
1652 
1653 		mutex_unlock(&ag->mutex);
1654 	}
1655 
1656 exit_restart:
1657 	complete(&ab->restart_completed);
1658 }
1659 
ath12k_core_reset(struct work_struct * work)1660 static void ath12k_core_reset(struct work_struct *work)
1661 {
1662 	struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work);
1663 	struct ath12k_hw_group *ag = ab->ag;
1664 	int reset_count, fail_cont_count, i;
1665 	long time_left;
1666 
1667 	if (!(test_bit(ATH12K_FLAG_QMI_FW_READY_COMPLETE, &ab->dev_flags))) {
1668 		ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags);
1669 		return;
1670 	}
1671 
1672 	/* Sometimes the recovery will fail and then the next all recovery fail,
1673 	 * this is to avoid infinite recovery since it can not recovery success
1674 	 */
1675 	fail_cont_count = atomic_read(&ab->fail_cont_count);
1676 
1677 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL)
1678 		return;
1679 
1680 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST &&
1681 	    time_before(jiffies, ab->reset_fail_timeout))
1682 		return;
1683 
1684 	reset_count = atomic_inc_return(&ab->reset_count);
1685 
1686 	if (reset_count > 1) {
1687 		/* Sometimes it happened another reset worker before the previous one
1688 		 * completed, then the second reset worker will destroy the previous one,
1689 		 * thus below is to avoid that.
1690 		 */
1691 		ath12k_warn(ab, "already resetting count %d\n", reset_count);
1692 
1693 		reinit_completion(&ab->reset_complete);
1694 		time_left = wait_for_completion_timeout(&ab->reset_complete,
1695 							ATH12K_RESET_TIMEOUT_HZ);
1696 		if (time_left) {
1697 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n");
1698 			atomic_dec(&ab->reset_count);
1699 			return;
1700 		}
1701 
1702 		ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ;
1703 		/* Record the continuous recovery fail count when recovery failed*/
1704 		fail_cont_count = atomic_inc_return(&ab->fail_cont_count);
1705 	}
1706 
1707 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n");
1708 
1709 	ab->is_reset = true;
1710 	atomic_set(&ab->recovery_count, 0);
1711 
1712 	ath12k_coredump_collect(ab);
1713 	ath12k_core_pre_reconfigure_recovery(ab);
1714 
1715 	ath12k_core_post_reconfigure_recovery(ab);
1716 
1717 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n");
1718 
1719 	ath12k_hif_irq_disable(ab);
1720 	ath12k_hif_ce_irq_disable(ab);
1721 
1722 	ath12k_hif_power_down(ab, false);
1723 
1724 	/* prepare for power up */
1725 	ab->qmi.num_radios = U8_MAX;
1726 
1727 	mutex_lock(&ag->mutex);
1728 	ath12k_core_to_group_ref_put(ab);
1729 
1730 	if (ag->num_started > 0) {
1731 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
1732 			   "waiting for %d partner device(s) to reset\n",
1733 			   ag->num_started);
1734 		mutex_unlock(&ag->mutex);
1735 		return;
1736 	}
1737 
1738 	/* Prepare MLO global memory region for power up */
1739 	ath12k_qmi_reset_mlo_mem(ag);
1740 
1741 	for (i = 0; i < ag->num_devices; i++) {
1742 		ab = ag->ab[i];
1743 		if (!ab)
1744 			continue;
1745 
1746 		ath12k_hif_power_up(ab);
1747 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n");
1748 	}
1749 
1750 	mutex_unlock(&ag->mutex);
1751 }
1752 
ath12k_core_get_memory_mode(struct ath12k_base * ab)1753 enum ath12k_qmi_mem_mode ath12k_core_get_memory_mode(struct ath12k_base *ab)
1754 {
1755 	unsigned long total_ram;
1756 	struct sysinfo si;
1757 
1758 	si_meminfo(&si);
1759 	total_ram = si.totalram * si.mem_unit;
1760 
1761 	if (total_ram < SZ_512M)
1762 		return ATH12K_QMI_MEMORY_MODE_LOW_512_M;
1763 
1764 	return ATH12K_QMI_MEMORY_MODE_DEFAULT;
1765 }
1766 EXPORT_SYMBOL(ath12k_core_get_memory_mode);
1767 
ath12k_core_pre_init(struct ath12k_base * ab)1768 int ath12k_core_pre_init(struct ath12k_base *ab)
1769 {
1770 	const struct ath12k_mem_profile_based_param *param;
1771 
1772 	param = &ath12k_mem_profile_based_param[ab->target_mem_mode];
1773 	ab->profile_param = param;
1774 	ath12k_fw_map(ab);
1775 
1776 	return 0;
1777 }
1778 
ath12k_core_panic_handler(struct notifier_block * nb,unsigned long action,void * data)1779 static int ath12k_core_panic_handler(struct notifier_block *nb,
1780 				     unsigned long action, void *data)
1781 {
1782 	struct ath12k_base *ab = container_of(nb, struct ath12k_base,
1783 					      panic_nb);
1784 
1785 	return ath12k_hif_panic_handler(ab);
1786 }
1787 
ath12k_core_panic_notifier_register(struct ath12k_base * ab)1788 static int ath12k_core_panic_notifier_register(struct ath12k_base *ab)
1789 {
1790 	ab->panic_nb.notifier_call = ath12k_core_panic_handler;
1791 
1792 	return atomic_notifier_chain_register(&panic_notifier_list,
1793 					      &ab->panic_nb);
1794 }
1795 
ath12k_core_panic_notifier_unregister(struct ath12k_base * ab)1796 static void ath12k_core_panic_notifier_unregister(struct ath12k_base *ab)
1797 {
1798 	atomic_notifier_chain_unregister(&panic_notifier_list,
1799 					 &ab->panic_nb);
1800 }
1801 
1802 static inline
ath12k_core_hw_group_create_ready(struct ath12k_hw_group * ag)1803 bool ath12k_core_hw_group_create_ready(struct ath12k_hw_group *ag)
1804 {
1805 	lockdep_assert_held(&ag->mutex);
1806 
1807 	return (ag->num_probed == ag->num_devices);
1808 }
1809 
ath12k_core_hw_group_alloc(struct ath12k_base * ab)1810 static struct ath12k_hw_group *ath12k_core_hw_group_alloc(struct ath12k_base *ab)
1811 {
1812 	struct ath12k_hw_group *ag;
1813 	int count = 0;
1814 
1815 	lockdep_assert_held(&ath12k_hw_group_mutex);
1816 
1817 	list_for_each_entry(ag, &ath12k_hw_group_list, list)
1818 		count++;
1819 
1820 	ag = kzalloc_obj(*ag);
1821 	if (!ag)
1822 		return NULL;
1823 
1824 	ag->id = count;
1825 	list_add(&ag->list, &ath12k_hw_group_list);
1826 	mutex_init(&ag->mutex);
1827 	ag->mlo_capable = false;
1828 
1829 	return ag;
1830 }
1831 
ath12k_core_free_wsi_info(struct ath12k_hw_group * ag)1832 static void ath12k_core_free_wsi_info(struct ath12k_hw_group *ag)
1833 {
1834 	int i;
1835 
1836 	for (i = 0; i < ag->num_devices; i++) {
1837 		of_node_put(ag->wsi_node[i]);
1838 		ag->wsi_node[i] = NULL;
1839 	}
1840 	ag->num_devices = 0;
1841 }
1842 
ath12k_core_hw_group_free(struct ath12k_hw_group * ag)1843 static void ath12k_core_hw_group_free(struct ath12k_hw_group *ag)
1844 {
1845 	mutex_lock(&ath12k_hw_group_mutex);
1846 
1847 	ath12k_core_free_wsi_info(ag);
1848 	list_del(&ag->list);
1849 	kfree(ag);
1850 
1851 	mutex_unlock(&ath12k_hw_group_mutex);
1852 }
1853 
ath12k_core_hw_group_find_by_dt(struct ath12k_base * ab)1854 static struct ath12k_hw_group *ath12k_core_hw_group_find_by_dt(struct ath12k_base *ab)
1855 {
1856 	struct ath12k_hw_group *ag;
1857 	int i;
1858 
1859 	if (!ab->dev->of_node)
1860 		return NULL;
1861 
1862 	list_for_each_entry(ag, &ath12k_hw_group_list, list)
1863 		for (i = 0; i < ag->num_devices; i++)
1864 			if (ag->wsi_node[i] == ab->dev->of_node)
1865 				return ag;
1866 
1867 	return NULL;
1868 }
1869 
ath12k_core_get_wsi_info(struct ath12k_hw_group * ag,struct ath12k_base * ab)1870 static int ath12k_core_get_wsi_info(struct ath12k_hw_group *ag,
1871 				    struct ath12k_base *ab)
1872 {
1873 	struct device_node *next_wsi_dev;
1874 	int device_count = 0, ret = 0;
1875 	struct device_node *wsi_dev;
1876 
1877 	wsi_dev = of_node_get(ab->dev->of_node);
1878 	if (!wsi_dev)
1879 		return -ENODEV;
1880 
1881 	do {
1882 		if (device_count >= ATH12K_MAX_DEVICES) {
1883 			ath12k_warn(ab, "device count in DT %d is more than limit %d\n",
1884 				    device_count, ATH12K_MAX_DEVICES);
1885 			ret = -EINVAL;
1886 			break;
1887 		}
1888 
1889 		ag->wsi_node[device_count++] = of_node_get(wsi_dev);
1890 
1891 		struct device_node *tx_endpoint __free(device_node) =
1892 					of_graph_get_endpoint_by_regs(wsi_dev, 0, -1);
1893 		if (!tx_endpoint) {
1894 			ret = -ENODEV;
1895 			break;
1896 		}
1897 
1898 		struct device_node *next_rx_endpoint __free(device_node) =
1899 					of_graph_get_remote_endpoint(tx_endpoint);
1900 		if (!next_rx_endpoint) {
1901 			ret = -ENODEV;
1902 			break;
1903 		}
1904 
1905 		next_wsi_dev = of_graph_get_port_parent(next_rx_endpoint);
1906 		if (!next_wsi_dev) {
1907 			ret = -ENODEV;
1908 			break;
1909 		}
1910 
1911 		of_node_put(wsi_dev);
1912 		wsi_dev = next_wsi_dev;
1913 	} while (ab->dev->of_node != wsi_dev);
1914 
1915 	if (ret) {
1916 		while (--device_count >= 0) {
1917 			of_node_put(ag->wsi_node[device_count]);
1918 			ag->wsi_node[device_count] = NULL;
1919 		}
1920 
1921 		of_node_put(wsi_dev);
1922 		return ret;
1923 	}
1924 
1925 	of_node_put(wsi_dev);
1926 	ag->num_devices = device_count;
1927 
1928 	return 0;
1929 }
1930 
ath12k_core_get_wsi_index(struct ath12k_hw_group * ag,struct ath12k_base * ab)1931 static int ath12k_core_get_wsi_index(struct ath12k_hw_group *ag,
1932 				     struct ath12k_base *ab)
1933 {
1934 	int i, wsi_controller_index = -1, node_index = -1;
1935 	bool control;
1936 
1937 	for (i = 0; i < ag->num_devices; i++) {
1938 		control = of_property_read_bool(ag->wsi_node[i], "qcom,wsi-controller");
1939 		if (control)
1940 			wsi_controller_index = i;
1941 
1942 		if (ag->wsi_node[i] == ab->dev->of_node)
1943 			node_index = i;
1944 	}
1945 
1946 	if (wsi_controller_index == -1) {
1947 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "wsi controller is not defined in dt");
1948 		return -EINVAL;
1949 	}
1950 
1951 	if (node_index == -1) {
1952 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "unable to get WSI node index");
1953 		return -EINVAL;
1954 	}
1955 
1956 	ab->wsi_info.index = (ag->num_devices + node_index - wsi_controller_index) %
1957 		ag->num_devices;
1958 
1959 	return 0;
1960 }
1961 
ath12k_core_hw_group_assign(struct ath12k_base * ab)1962 static struct ath12k_hw_group *ath12k_core_hw_group_assign(struct ath12k_base *ab)
1963 {
1964 	struct ath12k_wsi_info *wsi = &ab->wsi_info;
1965 	struct ath12k_hw_group *ag;
1966 
1967 	lockdep_assert_held(&ath12k_hw_group_mutex);
1968 
1969 	if (ath12k_ftm_mode)
1970 		goto invalid_group;
1971 
1972 	/* The grouping of multiple devices will be done based on device tree file.
1973 	 * The platforms that do not have any valid group information would have
1974 	 * each device to be part of its own invalid group.
1975 	 *
1976 	 * We use group id ATH12K_INVALID_GROUP_ID for single device group
1977 	 * which didn't have dt entry or wrong dt entry, there could be many
1978 	 * groups with same group id, i.e ATH12K_INVALID_GROUP_ID. So
1979 	 * default group id of ATH12K_INVALID_GROUP_ID combined with
1980 	 * num devices in ath12k_hw_group determines if the group is
1981 	 * multi device or single device group
1982 	 */
1983 
1984 	ag = ath12k_core_hw_group_find_by_dt(ab);
1985 	if (!ag) {
1986 		ag = ath12k_core_hw_group_alloc(ab);
1987 		if (!ag) {
1988 			ath12k_warn(ab, "unable to create new hw group\n");
1989 			return NULL;
1990 		}
1991 
1992 		if (ath12k_core_get_wsi_info(ag, ab) ||
1993 		    ath12k_core_get_wsi_index(ag, ab)) {
1994 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
1995 				   "unable to get wsi info from dt, grouping single device");
1996 			ath12k_core_free_wsi_info(ag);
1997 			ag->id = ATH12K_INVALID_GROUP_ID;
1998 			ag->num_devices = 1;
1999 			wsi->index = 0;
2000 		}
2001 
2002 		goto exit;
2003 	} else if (test_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags)) {
2004 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "group id %d in unregister state\n",
2005 			   ag->id);
2006 		goto invalid_group;
2007 	} else {
2008 		if (ath12k_core_get_wsi_index(ag, ab))
2009 			goto invalid_group;
2010 		goto exit;
2011 	}
2012 
2013 invalid_group:
2014 	ag = ath12k_core_hw_group_alloc(ab);
2015 	if (!ag) {
2016 		ath12k_warn(ab, "unable to create new hw group\n");
2017 		return NULL;
2018 	}
2019 
2020 	ag->id = ATH12K_INVALID_GROUP_ID;
2021 	ag->num_devices = 1;
2022 	wsi->index = 0;
2023 
2024 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "single device added to hardware group\n");
2025 
2026 exit:
2027 	if (ag->num_probed >= ag->num_devices) {
2028 		ath12k_warn(ab, "unable to add new device to group, max limit reached\n");
2029 		goto invalid_group;
2030 	}
2031 
2032 	ab->device_id = ag->num_probed++;
2033 	ag->ab[ab->device_id] = ab;
2034 	ab->ag = ag;
2035 
2036 	ath12k_dp_cmn_hw_group_assign(ath12k_ab_to_dp(ab), ag);
2037 
2038 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "wsi group-id %d num-devices %d index %d",
2039 		   ag->id, ag->num_devices, wsi->index);
2040 
2041 	return ag;
2042 }
2043 
ath12k_core_hw_group_unassign(struct ath12k_base * ab)2044 void ath12k_core_hw_group_unassign(struct ath12k_base *ab)
2045 {
2046 	struct ath12k_hw_group *ag = ath12k_ab_to_ag(ab);
2047 	u8 device_id = ab->device_id;
2048 	int num_probed;
2049 
2050 	if (!ag)
2051 		return;
2052 
2053 	mutex_lock(&ag->mutex);
2054 
2055 	if (WARN_ON(device_id >= ag->num_devices)) {
2056 		mutex_unlock(&ag->mutex);
2057 		return;
2058 	}
2059 
2060 	if (WARN_ON(ag->ab[device_id] != ab)) {
2061 		mutex_unlock(&ag->mutex);
2062 		return;
2063 	}
2064 
2065 	ath12k_dp_cmn_hw_group_unassign(ath12k_ab_to_dp(ab), ag);
2066 
2067 	ag->ab[device_id] = NULL;
2068 	ab->ag = NULL;
2069 	ab->device_id = ATH12K_INVALID_DEVICE_ID;
2070 
2071 	if (ag->num_probed)
2072 		ag->num_probed--;
2073 
2074 	num_probed = ag->num_probed;
2075 
2076 	mutex_unlock(&ag->mutex);
2077 
2078 	if (!num_probed)
2079 		ath12k_core_hw_group_free(ag);
2080 }
2081 
ath12k_core_hw_group_destroy(struct ath12k_hw_group * ag)2082 static void ath12k_core_hw_group_destroy(struct ath12k_hw_group *ag)
2083 {
2084 	struct ath12k_base *ab;
2085 	int i;
2086 
2087 	if (WARN_ON(!ag))
2088 		return;
2089 
2090 	for (i = 0; i < ag->num_devices; i++) {
2091 		ab = ag->ab[i];
2092 		if (!ab)
2093 			continue;
2094 
2095 		ath12k_core_soc_destroy(ab);
2096 	}
2097 }
2098 
ath12k_core_hw_group_cleanup(struct ath12k_hw_group * ag)2099 void ath12k_core_hw_group_cleanup(struct ath12k_hw_group *ag)
2100 {
2101 	struct ath12k_base *ab;
2102 	int i;
2103 
2104 	if (!ag)
2105 		return;
2106 
2107 	mutex_lock(&ag->mutex);
2108 
2109 	if (test_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags)) {
2110 		mutex_unlock(&ag->mutex);
2111 		return;
2112 	}
2113 
2114 	set_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags);
2115 
2116 	ath12k_core_hw_group_stop(ag);
2117 
2118 	for (i = 0; i < ag->num_devices; i++) {
2119 		ab = ag->ab[i];
2120 		if (!ab)
2121 			continue;
2122 
2123 		mutex_lock(&ab->core_lock);
2124 		ath12k_core_stop(ab);
2125 		mutex_unlock(&ab->core_lock);
2126 	}
2127 
2128 	mutex_unlock(&ag->mutex);
2129 }
2130 
ath12k_core_hw_group_create(struct ath12k_hw_group * ag)2131 static int ath12k_core_hw_group_create(struct ath12k_hw_group *ag)
2132 {
2133 	struct ath12k_base *ab;
2134 	int i, ret;
2135 
2136 	lockdep_assert_held(&ag->mutex);
2137 
2138 	for (i = 0; i < ag->num_devices; i++) {
2139 		ab = ag->ab[i];
2140 		if (!ab)
2141 			continue;
2142 
2143 		mutex_lock(&ab->core_lock);
2144 
2145 		ret = ath12k_core_soc_create(ab);
2146 		if (ret) {
2147 			mutex_unlock(&ab->core_lock);
2148 			ath12k_err(ab, "failed to create soc %d core: %d\n", i, ret);
2149 			goto destroy;
2150 		}
2151 
2152 		mutex_unlock(&ab->core_lock);
2153 	}
2154 
2155 	return 0;
2156 
2157 destroy:
2158 	for (i--; i >= 0; i--) {
2159 		ab = ag->ab[i];
2160 		if (!ab)
2161 			continue;
2162 
2163 		mutex_lock(&ab->core_lock);
2164 		ath12k_core_soc_destroy(ab);
2165 		mutex_unlock(&ab->core_lock);
2166 	}
2167 
2168 	return ret;
2169 }
2170 
ath12k_core_hw_group_set_mlo_capable(struct ath12k_hw_group * ag)2171 void ath12k_core_hw_group_set_mlo_capable(struct ath12k_hw_group *ag)
2172 {
2173 	struct ath12k_base *ab;
2174 	int i;
2175 
2176 	if (ath12k_ftm_mode)
2177 		return;
2178 
2179 	lockdep_assert_held(&ag->mutex);
2180 
2181 	if (ag->num_devices == 1) {
2182 		ab = ag->ab[0];
2183 		/* QCN9274 firmware uses firmware IE for MLO advertisement */
2184 		if (ab->fw.fw_features_valid) {
2185 			ag->mlo_capable =
2186 				ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MLO);
2187 			return;
2188 		}
2189 
2190 		/* while WCN7850 firmware uses QMI single_chip_mlo_support bit */
2191 		ag->mlo_capable = ab->single_chip_mlo_support;
2192 		return;
2193 	}
2194 
2195 	ag->mlo_capable = true;
2196 
2197 	for (i = 0; i < ag->num_devices; i++) {
2198 		ab = ag->ab[i];
2199 		if (!ab)
2200 			continue;
2201 
2202 		/* even if 1 device's firmware feature indicates MLO
2203 		 * unsupported, make MLO unsupported for the whole group
2204 		 */
2205 		if (!ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MLO)) {
2206 			ag->mlo_capable = false;
2207 			return;
2208 		}
2209 	}
2210 }
2211 
ath12k_core_init(struct ath12k_base * ab)2212 int ath12k_core_init(struct ath12k_base *ab)
2213 {
2214 	struct ath12k_hw_group *ag;
2215 	int ret;
2216 
2217 	ret = ath12k_core_panic_notifier_register(ab);
2218 	if (ret)
2219 		ath12k_warn(ab, "failed to register panic handler: %d\n", ret);
2220 
2221 	mutex_lock(&ath12k_hw_group_mutex);
2222 
2223 	ag = ath12k_core_hw_group_assign(ab);
2224 	if (!ag) {
2225 		mutex_unlock(&ath12k_hw_group_mutex);
2226 		ath12k_warn(ab, "unable to get hw group\n");
2227 		ret = -ENODEV;
2228 		goto err_unregister_notifier;
2229 	}
2230 
2231 	mutex_unlock(&ath12k_hw_group_mutex);
2232 
2233 	mutex_lock(&ag->mutex);
2234 
2235 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "num devices %d num probed %d\n",
2236 		   ag->num_devices, ag->num_probed);
2237 
2238 	if (ath12k_core_hw_group_create_ready(ag)) {
2239 		ret = ath12k_core_hw_group_create(ag);
2240 		if (ret) {
2241 			mutex_unlock(&ag->mutex);
2242 			ath12k_warn(ab, "unable to create hw group\n");
2243 			goto err_unassign_hw_group;
2244 		}
2245 	}
2246 
2247 	mutex_unlock(&ag->mutex);
2248 
2249 	return 0;
2250 
2251 err_unassign_hw_group:
2252 	ath12k_core_hw_group_unassign(ab);
2253 err_unregister_notifier:
2254 	ath12k_core_panic_notifier_unregister(ab);
2255 
2256 	return ret;
2257 }
2258 
ath12k_core_deinit(struct ath12k_base * ab)2259 void ath12k_core_deinit(struct ath12k_base *ab)
2260 {
2261 	ath12k_core_hw_group_destroy(ab->ag);
2262 	ath12k_core_hw_group_unassign(ab);
2263 	ath12k_core_panic_notifier_unregister(ab);
2264 }
2265 
ath12k_core_free(struct ath12k_base * ab)2266 void ath12k_core_free(struct ath12k_base *ab)
2267 {
2268 	timer_delete_sync(&ab->rx_replenish_retry);
2269 	ath12k_wmi_free();
2270 	destroy_workqueue(ab->workqueue_aux);
2271 	destroy_workqueue(ab->workqueue);
2272 	kfree(ab);
2273 }
2274 
ath12k_core_alloc(struct device * dev,size_t priv_size,enum ath12k_bus bus)2275 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
2276 				      enum ath12k_bus bus)
2277 {
2278 	struct ath12k_base *ab;
2279 
2280 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
2281 	if (!ab)
2282 		return NULL;
2283 
2284 	init_completion(&ab->driver_recovery);
2285 
2286 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
2287 	if (!ab->workqueue)
2288 		goto err_sc_free;
2289 
2290 	ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq");
2291 	if (!ab->workqueue_aux)
2292 		goto err_free_wq;
2293 
2294 	if (ath12k_wmi_alloc() < 0)
2295 		goto err_free_wq_aux;
2296 
2297 	mutex_init(&ab->core_lock);
2298 	spin_lock_init(&ab->base_lock);
2299 	init_completion(&ab->reset_complete);
2300 
2301 	init_waitqueue_head(&ab->peer_mapping_wq);
2302 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
2303 	INIT_WORK(&ab->restart_work, ath12k_core_restart);
2304 	INIT_WORK(&ab->reset_work, ath12k_core_reset);
2305 	INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work);
2306 	INIT_WORK(&ab->dump_work, ath12k_coredump_upload);
2307 	INIT_WORK(&ab->update_11d_work, ath12k_update_11d);
2308 
2309 	timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0);
2310 	init_completion(&ab->htc_suspend);
2311 	init_completion(&ab->restart_completed);
2312 	init_completion(&ab->wow.wakeup_completed);
2313 
2314 	ab->dev = dev;
2315 	ab->hif.bus = bus;
2316 	ab->qmi.num_radios = U8_MAX;
2317 	ab->single_chip_mlo_support = false;
2318 
2319 	/* Device index used to identify the devices in a group.
2320 	 *
2321 	 * In Intra-device MLO, only one device present in a group,
2322 	 * so it is always zero.
2323 	 *
2324 	 * In Inter-device MLO, Multiple device present in a group,
2325 	 * expect non-zero value.
2326 	 */
2327 	ab->device_id = 0;
2328 
2329 	return ab;
2330 
2331 err_free_wq_aux:
2332 	destroy_workqueue(ab->workqueue_aux);
2333 err_free_wq:
2334 	destroy_workqueue(ab->workqueue);
2335 err_sc_free:
2336 	kfree(ab);
2337 	return NULL;
2338 }
2339 
2340 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies WLAN devices");
2341 MODULE_LICENSE("Dual BSD/GPL");
2342