1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/remoteproc.h>
10 #include <linux/firmware.h>
11 #include <linux/of.h>
12 #include "core.h"
13 #include "dp_tx.h"
14 #include "dp_rx.h"
15 #include "debug.h"
16 #include "hif.h"
17 
18 unsigned int ath12k_debug_mask;
19 module_param_named(debug_mask, ath12k_debug_mask, uint, 0644);
20 MODULE_PARM_DESC(debug_mask, "Debugging mask");
21 
ath12k_core_suspend(struct ath12k_base * ab)22 int ath12k_core_suspend(struct ath12k_base *ab)
23 {
24 	int ret;
25 
26 	if (!ab->hw_params->supports_suspend)
27 		return -EOPNOTSUPP;
28 
29 	/* TODO: there can frames in queues so for now add delay as a hack.
30 	 * Need to implement to handle and remove this delay.
31 	 */
32 	msleep(500);
33 
34 	ret = ath12k_dp_rx_pktlog_stop(ab, true);
35 	if (ret) {
36 		ath12k_warn(ab, "failed to stop dp rx (and timer) pktlog during suspend: %d\n",
37 			    ret);
38 		return ret;
39 	}
40 
41 	ret = ath12k_dp_rx_pktlog_stop(ab, false);
42 	if (ret) {
43 		ath12k_warn(ab, "failed to stop dp rx pktlog during suspend: %d\n",
44 			    ret);
45 		return ret;
46 	}
47 
48 	ath12k_hif_irq_disable(ab);
49 	ath12k_hif_ce_irq_disable(ab);
50 
51 	ret = ath12k_hif_suspend(ab);
52 	if (ret) {
53 		ath12k_warn(ab, "failed to suspend hif: %d\n", ret);
54 		return ret;
55 	}
56 
57 	return 0;
58 }
59 
ath12k_core_resume(struct ath12k_base * ab)60 int ath12k_core_resume(struct ath12k_base *ab)
61 {
62 	int ret;
63 
64 	if (!ab->hw_params->supports_suspend)
65 		return -EOPNOTSUPP;
66 
67 	ret = ath12k_hif_resume(ab);
68 	if (ret) {
69 		ath12k_warn(ab, "failed to resume hif during resume: %d\n", ret);
70 		return ret;
71 	}
72 
73 	ath12k_hif_ce_irq_enable(ab);
74 	ath12k_hif_irq_enable(ab);
75 
76 	ret = ath12k_dp_rx_pktlog_start(ab);
77 	if (ret) {
78 		ath12k_warn(ab, "failed to start rx pktlog during resume: %d\n",
79 			    ret);
80 		return ret;
81 	}
82 
83 	return 0;
84 }
85 
ath12k_core_create_board_name(struct ath12k_base * ab,char * name,size_t name_len)86 static int ath12k_core_create_board_name(struct ath12k_base *ab, char *name,
87 					 size_t name_len)
88 {
89 	/* strlen(',variant=') + strlen(ab->qmi.target.bdf_ext) */
90 	char variant[9 + ATH12K_QMI_BDF_EXT_STR_LENGTH] = { 0 };
91 
92 	if (ab->qmi.target.bdf_ext[0] != '\0')
93 		scnprintf(variant, sizeof(variant), ",variant=%s",
94 			  ab->qmi.target.bdf_ext);
95 
96 	scnprintf(name, name_len,
97 		  "bus=%s,qmi-chip-id=%d,qmi-board-id=%d%s",
98 		  ath12k_bus_str(ab->hif.bus),
99 		  ab->qmi.target.chip_id,
100 		  ab->qmi.target.board_id, variant);
101 
102 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot using board name '%s'\n", name);
103 
104 	return 0;
105 }
106 
ath12k_core_firmware_request(struct ath12k_base * ab,const char * file)107 const struct firmware *ath12k_core_firmware_request(struct ath12k_base *ab,
108 						    const char *file)
109 {
110 	const struct firmware *fw;
111 	char path[100];
112 	int ret;
113 
114 	if (!file)
115 		return ERR_PTR(-ENOENT);
116 
117 	ath12k_core_create_firmware_path(ab, file, path, sizeof(path));
118 
119 	ret = firmware_request_nowarn(&fw, path, ab->dev);
120 	if (ret)
121 		return ERR_PTR(ret);
122 
123 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot firmware request %s size %zu\n",
124 		   path, fw->size);
125 
126 	return fw;
127 }
128 
ath12k_core_free_bdf(struct ath12k_base * ab,struct ath12k_board_data * bd)129 void ath12k_core_free_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
130 {
131 	if (!IS_ERR(bd->fw))
132 		release_firmware(bd->fw);
133 
134 	memset(bd, 0, sizeof(*bd));
135 }
136 
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 bd_ie_type)137 static int ath12k_core_parse_bd_ie_board(struct ath12k_base *ab,
138 					 struct ath12k_board_data *bd,
139 					 const void *buf, size_t buf_len,
140 					 const char *boardname,
141 					 int bd_ie_type)
142 {
143 	const struct ath12k_fw_ie *hdr;
144 	bool name_match_found;
145 	int ret, board_ie_id;
146 	size_t board_ie_len;
147 	const void *board_ie_data;
148 
149 	name_match_found = false;
150 
151 	/* go through ATH12K_BD_IE_BOARD_ elements */
152 	while (buf_len > sizeof(struct ath12k_fw_ie)) {
153 		hdr = buf;
154 		board_ie_id = le32_to_cpu(hdr->id);
155 		board_ie_len = le32_to_cpu(hdr->len);
156 		board_ie_data = hdr->data;
157 
158 		buf_len -= sizeof(*hdr);
159 		buf += sizeof(*hdr);
160 
161 		if (buf_len < ALIGN(board_ie_len, 4)) {
162 			ath12k_err(ab, "invalid ATH12K_BD_IE_BOARD length: %zu < %zu\n",
163 				   buf_len, ALIGN(board_ie_len, 4));
164 			ret = -EINVAL;
165 			goto out;
166 		}
167 
168 		switch (board_ie_id) {
169 		case ATH12K_BD_IE_BOARD_NAME:
170 			ath12k_dbg_dump(ab, ATH12K_DBG_BOOT, "board name", "",
171 					board_ie_data, board_ie_len);
172 
173 			if (board_ie_len != strlen(boardname))
174 				break;
175 
176 			ret = memcmp(board_ie_data, boardname, strlen(boardname));
177 			if (ret)
178 				break;
179 
180 			name_match_found = true;
181 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
182 				   "boot found match for name '%s'",
183 				   boardname);
184 			break;
185 		case ATH12K_BD_IE_BOARD_DATA:
186 			if (!name_match_found)
187 				/* no match found */
188 				break;
189 
190 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
191 				   "boot found board data for '%s'", boardname);
192 
193 			bd->data = board_ie_data;
194 			bd->len = board_ie_len;
195 
196 			ret = 0;
197 			goto out;
198 		default:
199 			ath12k_warn(ab, "unknown ATH12K_BD_IE_BOARD found: %d\n",
200 				    board_ie_id);
201 			break;
202 		}
203 
204 		/* jump over the padding */
205 		board_ie_len = ALIGN(board_ie_len, 4);
206 
207 		buf_len -= board_ie_len;
208 		buf += board_ie_len;
209 	}
210 
211 	/* no match found */
212 	ret = -ENOENT;
213 
214 out:
215 	return ret;
216 }
217 
ath12k_core_fetch_board_data_api_n(struct ath12k_base * ab,struct ath12k_board_data * bd,const char * boardname)218 static int ath12k_core_fetch_board_data_api_n(struct ath12k_base *ab,
219 					      struct ath12k_board_data *bd,
220 					      const char *boardname)
221 {
222 	size_t len, magic_len;
223 	const u8 *data;
224 	char *filename, filepath[100];
225 	size_t ie_len;
226 	struct ath12k_fw_ie *hdr;
227 	int ret, ie_id;
228 
229 	filename = ATH12K_BOARD_API2_FILE;
230 
231 	if (!bd->fw)
232 		bd->fw = ath12k_core_firmware_request(ab, filename);
233 
234 	if (IS_ERR(bd->fw))
235 		return PTR_ERR(bd->fw);
236 
237 	data = bd->fw->data;
238 	len = bd->fw->size;
239 
240 	ath12k_core_create_firmware_path(ab, filename,
241 					 filepath, sizeof(filepath));
242 
243 	/* magic has extra null byte padded */
244 	magic_len = strlen(ATH12K_BOARD_MAGIC) + 1;
245 	if (len < magic_len) {
246 		ath12k_err(ab, "failed to find magic value in %s, file too short: %zu\n",
247 			   filepath, len);
248 		ret = -EINVAL;
249 		goto err;
250 	}
251 
252 	if (memcmp(data, ATH12K_BOARD_MAGIC, magic_len)) {
253 		ath12k_err(ab, "found invalid board magic\n");
254 		ret = -EINVAL;
255 		goto err;
256 	}
257 
258 	/* magic is padded to 4 bytes */
259 	magic_len = ALIGN(magic_len, 4);
260 	if (len < magic_len) {
261 		ath12k_err(ab, "failed: %s too small to contain board data, len: %zu\n",
262 			   filepath, len);
263 		ret = -EINVAL;
264 		goto err;
265 	}
266 
267 	data += magic_len;
268 	len -= magic_len;
269 
270 	while (len > sizeof(struct ath12k_fw_ie)) {
271 		hdr = (struct ath12k_fw_ie *)data;
272 		ie_id = le32_to_cpu(hdr->id);
273 		ie_len = le32_to_cpu(hdr->len);
274 
275 		len -= sizeof(*hdr);
276 		data = hdr->data;
277 
278 		if (len < ALIGN(ie_len, 4)) {
279 			ath12k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
280 				   ie_id, ie_len, len);
281 			ret = -EINVAL;
282 			goto err;
283 		}
284 
285 		switch (ie_id) {
286 		case ATH12K_BD_IE_BOARD:
287 			ret = ath12k_core_parse_bd_ie_board(ab, bd, data,
288 							    ie_len,
289 							    boardname,
290 							    ATH12K_BD_IE_BOARD);
291 			if (ret == -ENOENT)
292 				/* no match found, continue */
293 				break;
294 			else if (ret)
295 				/* there was an error, bail out */
296 				goto err;
297 			/* either found or error, so stop searching */
298 			goto out;
299 		}
300 
301 		/* jump over the padding */
302 		ie_len = ALIGN(ie_len, 4);
303 
304 		len -= ie_len;
305 		data += ie_len;
306 	}
307 
308 out:
309 	if (!bd->data || !bd->len) {
310 		ath12k_err(ab,
311 			   "failed to fetch board data for %s from %s\n",
312 			   boardname, filepath);
313 		ret = -ENODATA;
314 		goto err;
315 	}
316 
317 	return 0;
318 
319 err:
320 	ath12k_core_free_bdf(ab, bd);
321 	return ret;
322 }
323 
ath12k_core_fetch_board_data_api_1(struct ath12k_base * ab,struct ath12k_board_data * bd,char * filename)324 int ath12k_core_fetch_board_data_api_1(struct ath12k_base *ab,
325 				       struct ath12k_board_data *bd,
326 				       char *filename)
327 {
328 	bd->fw = ath12k_core_firmware_request(ab, filename);
329 	if (IS_ERR(bd->fw))
330 		return PTR_ERR(bd->fw);
331 
332 	bd->data = bd->fw->data;
333 	bd->len = bd->fw->size;
334 
335 	return 0;
336 }
337 
338 #define BOARD_NAME_SIZE 100
ath12k_core_fetch_bdf(struct ath12k_base * ab,struct ath12k_board_data * bd)339 int ath12k_core_fetch_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
340 {
341 	char boardname[BOARD_NAME_SIZE];
342 	int ret;
343 
344 	ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
345 	if (ret) {
346 		ath12k_err(ab, "failed to create board name: %d", ret);
347 		return ret;
348 	}
349 
350 	ab->bd_api = 2;
351 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname);
352 	if (!ret)
353 		goto success;
354 
355 	ab->bd_api = 1;
356 	ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE);
357 	if (ret) {
358 		ath12k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n",
359 			   ab->hw_params->fw.dir);
360 		return ret;
361 	}
362 
363 success:
364 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", ab->bd_api);
365 	return 0;
366 }
367 
ath12k_core_stop(struct ath12k_base * ab)368 static void ath12k_core_stop(struct ath12k_base *ab)
369 {
370 	if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags))
371 		ath12k_qmi_firmware_stop(ab);
372 
373 	ath12k_hif_stop(ab);
374 	ath12k_wmi_detach(ab);
375 	ath12k_dp_rx_pdev_reo_cleanup(ab);
376 
377 	/* De-Init of components as needed */
378 }
379 
ath12k_core_soc_create(struct ath12k_base * ab)380 static int ath12k_core_soc_create(struct ath12k_base *ab)
381 {
382 	int ret;
383 
384 	ret = ath12k_qmi_init_service(ab);
385 	if (ret) {
386 		ath12k_err(ab, "failed to initialize qmi :%d\n", ret);
387 		return ret;
388 	}
389 
390 	ret = ath12k_hif_power_up(ab);
391 	if (ret) {
392 		ath12k_err(ab, "failed to power up :%d\n", ret);
393 		goto err_qmi_deinit;
394 	}
395 
396 	return 0;
397 
398 err_qmi_deinit:
399 	ath12k_qmi_deinit_service(ab);
400 	return ret;
401 }
402 
ath12k_core_soc_destroy(struct ath12k_base * ab)403 static void ath12k_core_soc_destroy(struct ath12k_base *ab)
404 {
405 	ath12k_dp_free(ab);
406 	ath12k_reg_free(ab);
407 	ath12k_qmi_deinit_service(ab);
408 }
409 
ath12k_core_pdev_create(struct ath12k_base * ab)410 static int ath12k_core_pdev_create(struct ath12k_base *ab)
411 {
412 	int ret;
413 
414 	ret = ath12k_mac_register(ab);
415 	if (ret) {
416 		ath12k_err(ab, "failed register the radio with mac80211: %d\n", ret);
417 		return ret;
418 	}
419 
420 	ret = ath12k_dp_pdev_alloc(ab);
421 	if (ret) {
422 		ath12k_err(ab, "failed to attach DP pdev: %d\n", ret);
423 		goto err_mac_unregister;
424 	}
425 
426 	return 0;
427 
428 err_mac_unregister:
429 	ath12k_mac_unregister(ab);
430 
431 	return ret;
432 }
433 
ath12k_core_pdev_destroy(struct ath12k_base * ab)434 static void ath12k_core_pdev_destroy(struct ath12k_base *ab)
435 {
436 	ath12k_mac_unregister(ab);
437 	ath12k_hif_irq_disable(ab);
438 	ath12k_dp_pdev_free(ab);
439 }
440 
ath12k_core_start(struct ath12k_base * ab,enum ath12k_firmware_mode mode)441 static int ath12k_core_start(struct ath12k_base *ab,
442 			     enum ath12k_firmware_mode mode)
443 {
444 	int ret;
445 
446 	ret = ath12k_wmi_attach(ab);
447 	if (ret) {
448 		ath12k_err(ab, "failed to attach wmi: %d\n", ret);
449 		return ret;
450 	}
451 
452 	ret = ath12k_htc_init(ab);
453 	if (ret) {
454 		ath12k_err(ab, "failed to init htc: %d\n", ret);
455 		goto err_wmi_detach;
456 	}
457 
458 	ret = ath12k_hif_start(ab);
459 	if (ret) {
460 		ath12k_err(ab, "failed to start HIF: %d\n", ret);
461 		goto err_wmi_detach;
462 	}
463 
464 	ret = ath12k_htc_wait_target(&ab->htc);
465 	if (ret) {
466 		ath12k_err(ab, "failed to connect to HTC: %d\n", ret);
467 		goto err_hif_stop;
468 	}
469 
470 	ret = ath12k_dp_htt_connect(&ab->dp);
471 	if (ret) {
472 		ath12k_err(ab, "failed to connect to HTT: %d\n", ret);
473 		goto err_hif_stop;
474 	}
475 
476 	ret = ath12k_wmi_connect(ab);
477 	if (ret) {
478 		ath12k_err(ab, "failed to connect wmi: %d\n", ret);
479 		goto err_hif_stop;
480 	}
481 
482 	ret = ath12k_htc_start(&ab->htc);
483 	if (ret) {
484 		ath12k_err(ab, "failed to start HTC: %d\n", ret);
485 		goto err_hif_stop;
486 	}
487 
488 	ret = ath12k_wmi_wait_for_service_ready(ab);
489 	if (ret) {
490 		ath12k_err(ab, "failed to receive wmi service ready event: %d\n",
491 			   ret);
492 		goto err_hif_stop;
493 	}
494 
495 	ret = ath12k_mac_allocate(ab);
496 	if (ret) {
497 		ath12k_err(ab, "failed to create new hw device with mac80211 :%d\n",
498 			   ret);
499 		goto err_hif_stop;
500 	}
501 
502 	ath12k_dp_cc_config(ab);
503 
504 	ath12k_dp_pdev_pre_alloc(ab);
505 
506 	ret = ath12k_dp_rx_pdev_reo_setup(ab);
507 	if (ret) {
508 		ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
509 		goto err_mac_destroy;
510 	}
511 
512 	ret = ath12k_wmi_cmd_init(ab);
513 	if (ret) {
514 		ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret);
515 		goto err_reo_cleanup;
516 	}
517 
518 	ret = ath12k_wmi_wait_for_unified_ready(ab);
519 	if (ret) {
520 		ath12k_err(ab, "failed to receive wmi unified ready event: %d\n",
521 			   ret);
522 		goto err_reo_cleanup;
523 	}
524 
525 	/* put hardware to DBS mode */
526 	if (ab->hw_params->single_pdev_only) {
527 		ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS);
528 		if (ret) {
529 			ath12k_err(ab, "failed to send dbs mode: %d\n", ret);
530 			goto err_reo_cleanup;
531 		}
532 	}
533 
534 	ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab);
535 	if (ret) {
536 		ath12k_err(ab, "failed to send htt version request message: %d\n",
537 			   ret);
538 		goto err_reo_cleanup;
539 	}
540 
541 	return 0;
542 
543 err_reo_cleanup:
544 	ath12k_dp_rx_pdev_reo_cleanup(ab);
545 err_mac_destroy:
546 	ath12k_mac_destroy(ab);
547 err_hif_stop:
548 	ath12k_hif_stop(ab);
549 err_wmi_detach:
550 	ath12k_wmi_detach(ab);
551 	return ret;
552 }
553 
ath12k_core_start_firmware(struct ath12k_base * ab,enum ath12k_firmware_mode mode)554 static int ath12k_core_start_firmware(struct ath12k_base *ab,
555 				      enum ath12k_firmware_mode mode)
556 {
557 	int ret;
558 
559 	ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3,
560 				    &ab->qmi.ce_cfg.shadow_reg_v3_len);
561 
562 	ret = ath12k_qmi_firmware_start(ab, mode);
563 	if (ret) {
564 		ath12k_err(ab, "failed to send firmware start: %d\n", ret);
565 		return ret;
566 	}
567 
568 	return ret;
569 }
570 
ath12k_core_qmi_firmware_ready(struct ath12k_base * ab)571 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab)
572 {
573 	int ret;
574 
575 	ret = ath12k_core_start_firmware(ab, ATH12K_FIRMWARE_MODE_NORMAL);
576 	if (ret) {
577 		ath12k_err(ab, "failed to start firmware: %d\n", ret);
578 		return ret;
579 	}
580 
581 	ret = ath12k_ce_init_pipes(ab);
582 	if (ret) {
583 		ath12k_err(ab, "failed to initialize CE: %d\n", ret);
584 		goto err_firmware_stop;
585 	}
586 
587 	ret = ath12k_dp_alloc(ab);
588 	if (ret) {
589 		ath12k_err(ab, "failed to init DP: %d\n", ret);
590 		goto err_firmware_stop;
591 	}
592 
593 	mutex_lock(&ab->core_lock);
594 	ret = ath12k_core_start(ab, ATH12K_FIRMWARE_MODE_NORMAL);
595 	if (ret) {
596 		ath12k_err(ab, "failed to start core: %d\n", ret);
597 		goto err_dp_free;
598 	}
599 
600 	ret = ath12k_core_pdev_create(ab);
601 	if (ret) {
602 		ath12k_err(ab, "failed to create pdev core: %d\n", ret);
603 		goto err_core_stop;
604 	}
605 	ath12k_hif_irq_enable(ab);
606 	mutex_unlock(&ab->core_lock);
607 
608 	return 0;
609 
610 err_core_stop:
611 	ath12k_core_stop(ab);
612 	ath12k_mac_destroy(ab);
613 err_dp_free:
614 	ath12k_dp_free(ab);
615 	mutex_unlock(&ab->core_lock);
616 err_firmware_stop:
617 	ath12k_qmi_firmware_stop(ab);
618 
619 	return ret;
620 }
621 
ath12k_core_reconfigure_on_crash(struct ath12k_base * ab)622 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab)
623 {
624 	int ret;
625 
626 	mutex_lock(&ab->core_lock);
627 	ath12k_hif_irq_disable(ab);
628 	ath12k_dp_pdev_free(ab);
629 	ath12k_hif_stop(ab);
630 	ath12k_wmi_detach(ab);
631 	ath12k_dp_rx_pdev_reo_cleanup(ab);
632 	mutex_unlock(&ab->core_lock);
633 
634 	ath12k_dp_free(ab);
635 	ath12k_hal_srng_deinit(ab);
636 
637 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
638 
639 	ret = ath12k_hal_srng_init(ab);
640 	if (ret)
641 		return ret;
642 
643 	clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
644 
645 	ret = ath12k_core_qmi_firmware_ready(ab);
646 	if (ret)
647 		goto err_hal_srng_deinit;
648 
649 	clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags);
650 
651 	return 0;
652 
653 err_hal_srng_deinit:
654 	ath12k_hal_srng_deinit(ab);
655 	return ret;
656 }
657 
ath12k_core_halt(struct ath12k * ar)658 void ath12k_core_halt(struct ath12k *ar)
659 {
660 	struct ath12k_base *ab = ar->ab;
661 
662 	lockdep_assert_held(&ar->conf_mutex);
663 
664 	ar->num_created_vdevs = 0;
665 	ar->allocated_vdev_map = 0;
666 
667 	ath12k_mac_scan_finish(ar);
668 	ath12k_mac_peer_cleanup_all(ar);
669 	cancel_delayed_work_sync(&ar->scan.timeout);
670 	cancel_work_sync(&ar->regd_update_work);
671 
672 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
673 	synchronize_rcu();
674 	INIT_LIST_HEAD(&ar->arvifs);
675 	idr_init(&ar->txmgmt_idr);
676 }
677 
ath12k_core_pre_reconfigure_recovery(struct ath12k_base * ab)678 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab)
679 {
680 	struct ath12k *ar;
681 	struct ath12k_pdev *pdev;
682 	int i;
683 
684 	spin_lock_bh(&ab->base_lock);
685 	ab->stats.fw_crash_counter++;
686 	spin_unlock_bh(&ab->base_lock);
687 
688 	for (i = 0; i < ab->num_radios; i++) {
689 		pdev = &ab->pdevs[i];
690 		ar = pdev->ar;
691 		if (!ar || ar->state == ATH12K_STATE_OFF)
692 			continue;
693 
694 		ieee80211_stop_queues(ar->hw);
695 		ath12k_mac_drain_tx(ar);
696 		complete(&ar->scan.started);
697 		complete(&ar->scan.completed);
698 		complete(&ar->peer_assoc_done);
699 		complete(&ar->peer_delete_done);
700 		complete(&ar->install_key_done);
701 		complete(&ar->vdev_setup_done);
702 		complete(&ar->vdev_delete_done);
703 		complete(&ar->bss_survey_done);
704 
705 		wake_up(&ar->dp.tx_empty_waitq);
706 		idr_for_each(&ar->txmgmt_idr,
707 			     ath12k_mac_tx_mgmt_pending_free, ar);
708 		idr_destroy(&ar->txmgmt_idr);
709 		wake_up(&ar->txmgmt_empty_waitq);
710 	}
711 
712 	wake_up(&ab->wmi_ab.tx_credits_wq);
713 	wake_up(&ab->peer_mapping_wq);
714 }
715 
ath12k_core_post_reconfigure_recovery(struct ath12k_base * ab)716 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab)
717 {
718 	struct ath12k *ar;
719 	struct ath12k_pdev *pdev;
720 	int i;
721 
722 	for (i = 0; i < ab->num_radios; i++) {
723 		pdev = &ab->pdevs[i];
724 		ar = pdev->ar;
725 		if (!ar || ar->state == ATH12K_STATE_OFF)
726 			continue;
727 
728 		mutex_lock(&ar->conf_mutex);
729 
730 		switch (ar->state) {
731 		case ATH12K_STATE_ON:
732 			ar->state = ATH12K_STATE_RESTARTING;
733 			ath12k_core_halt(ar);
734 			ieee80211_restart_hw(ar->hw);
735 			break;
736 		case ATH12K_STATE_OFF:
737 			ath12k_warn(ab,
738 				    "cannot restart radio %d that hasn't been started\n",
739 				    i);
740 			break;
741 		case ATH12K_STATE_RESTARTING:
742 			break;
743 		case ATH12K_STATE_RESTARTED:
744 			ar->state = ATH12K_STATE_WEDGED;
745 			fallthrough;
746 		case ATH12K_STATE_WEDGED:
747 			ath12k_warn(ab,
748 				    "device is wedged, will not restart radio %d\n", i);
749 			break;
750 		}
751 		mutex_unlock(&ar->conf_mutex);
752 	}
753 	complete(&ab->driver_recovery);
754 }
755 
ath12k_core_restart(struct work_struct * work)756 static void ath12k_core_restart(struct work_struct *work)
757 {
758 	struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work);
759 	int ret;
760 
761 	if (!ab->is_reset)
762 		ath12k_core_pre_reconfigure_recovery(ab);
763 
764 	ret = ath12k_core_reconfigure_on_crash(ab);
765 	if (ret) {
766 		ath12k_err(ab, "failed to reconfigure driver on crash recovery\n");
767 		return;
768 	}
769 
770 	if (ab->is_reset)
771 		complete_all(&ab->reconfigure_complete);
772 
773 	if (!ab->is_reset)
774 		ath12k_core_post_reconfigure_recovery(ab);
775 }
776 
ath12k_core_reset(struct work_struct * work)777 static void ath12k_core_reset(struct work_struct *work)
778 {
779 	struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work);
780 	int reset_count, fail_cont_count;
781 	long time_left;
782 
783 	if (!(test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))) {
784 		ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags);
785 		return;
786 	}
787 
788 	/* Sometimes the recovery will fail and then the next all recovery fail,
789 	 * this is to avoid infinite recovery since it can not recovery success
790 	 */
791 	fail_cont_count = atomic_read(&ab->fail_cont_count);
792 
793 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL)
794 		return;
795 
796 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST &&
797 	    time_before(jiffies, ab->reset_fail_timeout))
798 		return;
799 
800 	reset_count = atomic_inc_return(&ab->reset_count);
801 
802 	if (reset_count > 1) {
803 		/* Sometimes it happened another reset worker before the previous one
804 		 * completed, then the second reset worker will destroy the previous one,
805 		 * thus below is to avoid that.
806 		 */
807 		ath12k_warn(ab, "already resetting count %d\n", reset_count);
808 
809 		reinit_completion(&ab->reset_complete);
810 		time_left = wait_for_completion_timeout(&ab->reset_complete,
811 							ATH12K_RESET_TIMEOUT_HZ);
812 		if (time_left) {
813 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n");
814 			atomic_dec(&ab->reset_count);
815 			return;
816 		}
817 
818 		ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ;
819 		/* Record the continuous recovery fail count when recovery failed*/
820 		fail_cont_count = atomic_inc_return(&ab->fail_cont_count);
821 	}
822 
823 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n");
824 
825 	ab->is_reset = true;
826 	atomic_set(&ab->recovery_count, 0);
827 
828 	ath12k_core_pre_reconfigure_recovery(ab);
829 
830 	reinit_completion(&ab->reconfigure_complete);
831 	ath12k_core_post_reconfigure_recovery(ab);
832 
833 	reinit_completion(&ab->recovery_start);
834 	atomic_set(&ab->recovery_start_count, 0);
835 
836 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n");
837 
838 	time_left = wait_for_completion_timeout(&ab->recovery_start,
839 						ATH12K_RECOVER_START_TIMEOUT_HZ);
840 
841 	ath12k_hif_power_down(ab);
842 	ath12k_hif_power_up(ab);
843 
844 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n");
845 }
846 
ath12k_core_pre_init(struct ath12k_base * ab)847 int ath12k_core_pre_init(struct ath12k_base *ab)
848 {
849 	int ret;
850 
851 	ret = ath12k_hw_init(ab);
852 	if (ret) {
853 		ath12k_err(ab, "failed to init hw params: %d\n", ret);
854 		return ret;
855 	}
856 
857 	return 0;
858 }
859 
ath12k_core_init(struct ath12k_base * ab)860 int ath12k_core_init(struct ath12k_base *ab)
861 {
862 	int ret;
863 
864 	ret = ath12k_core_soc_create(ab);
865 	if (ret) {
866 		ath12k_err(ab, "failed to create soc core: %d\n", ret);
867 		return ret;
868 	}
869 
870 	return 0;
871 }
872 
ath12k_core_deinit(struct ath12k_base * ab)873 void ath12k_core_deinit(struct ath12k_base *ab)
874 {
875 	mutex_lock(&ab->core_lock);
876 
877 	ath12k_core_pdev_destroy(ab);
878 	ath12k_core_stop(ab);
879 
880 	mutex_unlock(&ab->core_lock);
881 
882 	ath12k_hif_power_down(ab);
883 	ath12k_mac_destroy(ab);
884 	ath12k_core_soc_destroy(ab);
885 }
886 
ath12k_core_free(struct ath12k_base * ab)887 void ath12k_core_free(struct ath12k_base *ab)
888 {
889 	timer_delete_sync(&ab->rx_replenish_retry);
890 	destroy_workqueue(ab->workqueue_aux);
891 	destroy_workqueue(ab->workqueue);
892 	kfree(ab);
893 }
894 
ath12k_core_alloc(struct device * dev,size_t priv_size,enum ath12k_bus bus)895 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
896 				      enum ath12k_bus bus)
897 {
898 	struct ath12k_base *ab;
899 
900 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
901 	if (!ab)
902 		return NULL;
903 
904 	init_completion(&ab->driver_recovery);
905 
906 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
907 	if (!ab->workqueue)
908 		goto err_sc_free;
909 
910 	ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq");
911 	if (!ab->workqueue_aux)
912 		goto err_free_wq;
913 
914 	mutex_init(&ab->core_lock);
915 	spin_lock_init(&ab->base_lock);
916 	init_completion(&ab->reset_complete);
917 	init_completion(&ab->reconfigure_complete);
918 	init_completion(&ab->recovery_start);
919 
920 	INIT_LIST_HEAD(&ab->peers);
921 	init_waitqueue_head(&ab->peer_mapping_wq);
922 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
923 	INIT_WORK(&ab->restart_work, ath12k_core_restart);
924 	INIT_WORK(&ab->reset_work, ath12k_core_reset);
925 	timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0);
926 	init_completion(&ab->htc_suspend);
927 
928 	ab->dev = dev;
929 	ab->hif.bus = bus;
930 
931 	return ab;
932 
933 err_free_wq:
934 	destroy_workqueue(ab->workqueue);
935 err_sc_free:
936 	kfree(ab);
937 	return NULL;
938 }
939 
940 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11be wireless LAN cards.");
941 MODULE_LICENSE("Dual BSD/GPL");
942