1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/remoteproc.h>
9 #include <linux/firmware.h>
10 #include "core.h"
11 #include "dp_tx.h"
12 #include "dp_rx.h"
13 #include "debug.h"
14 #include "hif.h"
15 
16 unsigned int ath11k_debug_mask;
17 EXPORT_SYMBOL(ath11k_debug_mask);
18 module_param_named(debug_mask, ath11k_debug_mask, uint, 0644);
19 MODULE_PARM_DESC(debug_mask, "Debugging mask");
20 
21 static unsigned int ath11k_crypto_mode;
22 module_param_named(crypto_mode, ath11k_crypto_mode, uint, 0644);
23 MODULE_PARM_DESC(crypto_mode, "crypto mode: 0-hardware, 1-software");
24 
25 /* frame mode values are mapped as per enum ath11k_hw_txrx_mode */
26 unsigned int ath11k_frame_mode = ATH11K_HW_TXRX_NATIVE_WIFI;
27 module_param_named(frame_mode, ath11k_frame_mode, uint, 0644);
28 MODULE_PARM_DESC(frame_mode,
29 		 "Datapath frame mode (0: raw, 1: native wifi (default), 2: ethernet)");
30 
31 static const struct ath11k_hw_params ath11k_hw_params[] = {
32 	{
33 		.hw_rev = ATH11K_HW_IPQ8074,
34 		.name = "ipq8074 hw2.0",
35 		.fw = {
36 			.dir = "IPQ8074/hw2.0",
37 			.board_size = 256 * 1024,
38 			.cal_size = 256 * 1024,
39 		},
40 		.max_radios = 3,
41 		.bdf_addr = 0x4B0C0000,
42 		.hw_ops = &ipq8074_ops,
43 		.ring_mask = &ath11k_hw_ring_mask_ipq8074,
44 		.internal_sleep_clock = false,
45 		.regs = &ipq8074_regs,
46 		.host_ce_config = ath11k_host_ce_config_ipq8074,
47 		.ce_count = 12,
48 		.target_ce_config = ath11k_target_ce_config_wlan_ipq8074,
49 		.target_ce_count = 11,
50 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_ipq8074,
51 		.svc_to_ce_map_len = 21,
52 		.single_pdev_only = false,
53 		.needs_band_to_mac = true,
54 		.rxdma1_enable = true,
55 		.num_rxmda_per_pdev = 1,
56 		.rx_mac_buf_ring = false,
57 		.vdev_start_delay = false,
58 		.htt_peer_map_v2 = true,
59 		.tcl_0_only = false,
60 		.spectral_fft_sz = 2,
61 
62 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
63 					BIT(NL80211_IFTYPE_AP) |
64 					BIT(NL80211_IFTYPE_MESH_POINT),
65 		.supports_monitor = true,
66 		.supports_shadow_regs = false,
67 		.idle_ps = false,
68 	},
69 	{
70 		.hw_rev = ATH11K_HW_IPQ6018_HW10,
71 		.name = "ipq6018 hw1.0",
72 		.fw = {
73 			.dir = "IPQ6018/hw1.0",
74 			.board_size = 256 * 1024,
75 			.cal_size = 256 * 1024,
76 		},
77 		.max_radios = 2,
78 		.bdf_addr = 0x4ABC0000,
79 		.hw_ops = &ipq6018_ops,
80 		.ring_mask = &ath11k_hw_ring_mask_ipq8074,
81 		.internal_sleep_clock = false,
82 		.regs = &ipq8074_regs,
83 		.host_ce_config = ath11k_host_ce_config_ipq8074,
84 		.ce_count = 12,
85 		.target_ce_config = ath11k_target_ce_config_wlan_ipq8074,
86 		.target_ce_count = 11,
87 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_ipq6018,
88 		.svc_to_ce_map_len = 19,
89 		.single_pdev_only = false,
90 		.needs_band_to_mac = true,
91 		.rxdma1_enable = true,
92 		.num_rxmda_per_pdev = 1,
93 		.rx_mac_buf_ring = false,
94 		.vdev_start_delay = false,
95 		.htt_peer_map_v2 = true,
96 		.tcl_0_only = false,
97 		.spectral_fft_sz = 4,
98 
99 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
100 					BIT(NL80211_IFTYPE_AP) |
101 					BIT(NL80211_IFTYPE_MESH_POINT),
102 		.supports_monitor = true,
103 		.supports_shadow_regs = false,
104 		.idle_ps = false,
105 	},
106 	{
107 		.name = "qca6390 hw2.0",
108 		.hw_rev = ATH11K_HW_QCA6390_HW20,
109 		.fw = {
110 			.dir = "QCA6390/hw2.0",
111 			.board_size = 256 * 1024,
112 			.cal_size = 256 * 1024,
113 		},
114 		.max_radios = 3,
115 		.bdf_addr = 0x4B0C0000,
116 		.hw_ops = &qca6390_ops,
117 		.ring_mask = &ath11k_hw_ring_mask_qca6390,
118 		.internal_sleep_clock = true,
119 		.regs = &qca6390_regs,
120 		.host_ce_config = ath11k_host_ce_config_qca6390,
121 		.ce_count = 9,
122 		.target_ce_config = ath11k_target_ce_config_wlan_qca6390,
123 		.target_ce_count = 9,
124 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_qca6390,
125 		.svc_to_ce_map_len = 14,
126 		.single_pdev_only = true,
127 		.needs_band_to_mac = false,
128 		.rxdma1_enable = false,
129 		.num_rxmda_per_pdev = 2,
130 		.rx_mac_buf_ring = true,
131 		.vdev_start_delay = true,
132 		.htt_peer_map_v2 = false,
133 		.tcl_0_only = true,
134 		.spectral_fft_sz = 0,
135 
136 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
137 					BIT(NL80211_IFTYPE_AP),
138 		.supports_monitor = false,
139 		.supports_shadow_regs = true,
140 		.idle_ps = true,
141 	},
142 };
143 
144 static int ath11k_core_create_board_name(struct ath11k_base *ab, char *name,
145 					 size_t name_len)
146 {
147 	scnprintf(name, name_len,
148 		  "bus=%s,qmi-chip-id=%d,qmi-board-id=%d",
149 		  ath11k_bus_str(ab->hif.bus),
150 		  ab->qmi.target.chip_id,
151 		  ab->qmi.target.board_id);
152 
153 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot using board name '%s'\n", name);
154 
155 	return 0;
156 }
157 
158 const struct firmware *ath11k_core_firmware_request(struct ath11k_base *ab,
159 						    const char *file)
160 {
161 	const struct firmware *fw;
162 	char path[100];
163 	int ret;
164 
165 	if (file == NULL)
166 		return ERR_PTR(-ENOENT);
167 
168 	ath11k_core_create_firmware_path(ab, file, path, sizeof(path));
169 
170 	ret = firmware_request_nowarn(&fw, path, ab->dev);
171 	if (ret)
172 		return ERR_PTR(ret);
173 
174 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot firmware request %s size %zu\n",
175 		   path, fw->size);
176 
177 	return fw;
178 }
179 
180 void ath11k_core_free_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
181 {
182 	if (!IS_ERR(bd->fw))
183 		release_firmware(bd->fw);
184 
185 	memset(bd, 0, sizeof(*bd));
186 }
187 
188 static int ath11k_core_parse_bd_ie_board(struct ath11k_base *ab,
189 					 struct ath11k_board_data *bd,
190 					 const void *buf, size_t buf_len,
191 					 const char *boardname,
192 					 int bd_ie_type)
193 {
194 	const struct ath11k_fw_ie *hdr;
195 	bool name_match_found;
196 	int ret, board_ie_id;
197 	size_t board_ie_len;
198 	const void *board_ie_data;
199 
200 	name_match_found = false;
201 
202 	/* go through ATH11K_BD_IE_BOARD_ elements */
203 	while (buf_len > sizeof(struct ath11k_fw_ie)) {
204 		hdr = buf;
205 		board_ie_id = le32_to_cpu(hdr->id);
206 		board_ie_len = le32_to_cpu(hdr->len);
207 		board_ie_data = hdr->data;
208 
209 		buf_len -= sizeof(*hdr);
210 		buf += sizeof(*hdr);
211 
212 		if (buf_len < ALIGN(board_ie_len, 4)) {
213 			ath11k_err(ab, "invalid ATH11K_BD_IE_BOARD length: %zu < %zu\n",
214 				   buf_len, ALIGN(board_ie_len, 4));
215 			ret = -EINVAL;
216 			goto out;
217 		}
218 
219 		switch (board_ie_id) {
220 		case ATH11K_BD_IE_BOARD_NAME:
221 			ath11k_dbg_dump(ab, ATH11K_DBG_BOOT, "board name", "",
222 					board_ie_data, board_ie_len);
223 
224 			if (board_ie_len != strlen(boardname))
225 				break;
226 
227 			ret = memcmp(board_ie_data, boardname, strlen(boardname));
228 			if (ret)
229 				break;
230 
231 			name_match_found = true;
232 			ath11k_dbg(ab, ATH11K_DBG_BOOT,
233 				   "boot found match for name '%s'",
234 				   boardname);
235 			break;
236 		case ATH11K_BD_IE_BOARD_DATA:
237 			if (!name_match_found)
238 				/* no match found */
239 				break;
240 
241 			ath11k_dbg(ab, ATH11K_DBG_BOOT,
242 				   "boot found board data for '%s'", boardname);
243 
244 			bd->data = board_ie_data;
245 			bd->len = board_ie_len;
246 
247 			ret = 0;
248 			goto out;
249 		default:
250 			ath11k_warn(ab, "unknown ATH11K_BD_IE_BOARD found: %d\n",
251 				    board_ie_id);
252 			break;
253 		}
254 
255 		/* jump over the padding */
256 		board_ie_len = ALIGN(board_ie_len, 4);
257 
258 		buf_len -= board_ie_len;
259 		buf += board_ie_len;
260 	}
261 
262 	/* no match found */
263 	ret = -ENOENT;
264 
265 out:
266 	return ret;
267 }
268 
269 static int ath11k_core_fetch_board_data_api_n(struct ath11k_base *ab,
270 					      struct ath11k_board_data *bd,
271 					      const char *boardname)
272 {
273 	size_t len, magic_len;
274 	const u8 *data;
275 	char *filename, filepath[100];
276 	size_t ie_len;
277 	struct ath11k_fw_ie *hdr;
278 	int ret, ie_id;
279 
280 	filename = ATH11K_BOARD_API2_FILE;
281 
282 	if (!bd->fw)
283 		bd->fw = ath11k_core_firmware_request(ab, filename);
284 
285 	if (IS_ERR(bd->fw))
286 		return PTR_ERR(bd->fw);
287 
288 	data = bd->fw->data;
289 	len = bd->fw->size;
290 
291 	ath11k_core_create_firmware_path(ab, filename,
292 					 filepath, sizeof(filepath));
293 
294 	/* magic has extra null byte padded */
295 	magic_len = strlen(ATH11K_BOARD_MAGIC) + 1;
296 	if (len < magic_len) {
297 		ath11k_err(ab, "failed to find magic value in %s, file too short: %zu\n",
298 			   filepath, len);
299 		ret = -EINVAL;
300 		goto err;
301 	}
302 
303 	if (memcmp(data, ATH11K_BOARD_MAGIC, magic_len)) {
304 		ath11k_err(ab, "found invalid board magic\n");
305 		ret = -EINVAL;
306 		goto err;
307 	}
308 
309 	/* magic is padded to 4 bytes */
310 	magic_len = ALIGN(magic_len, 4);
311 	if (len < magic_len) {
312 		ath11k_err(ab, "failed: %s too small to contain board data, len: %zu\n",
313 			   filepath, len);
314 		ret = -EINVAL;
315 		goto err;
316 	}
317 
318 	data += magic_len;
319 	len -= magic_len;
320 
321 	while (len > sizeof(struct ath11k_fw_ie)) {
322 		hdr = (struct ath11k_fw_ie *)data;
323 		ie_id = le32_to_cpu(hdr->id);
324 		ie_len = le32_to_cpu(hdr->len);
325 
326 		len -= sizeof(*hdr);
327 		data = hdr->data;
328 
329 		if (len < ALIGN(ie_len, 4)) {
330 			ath11k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
331 				   ie_id, ie_len, len);
332 			return -EINVAL;
333 		}
334 
335 		switch (ie_id) {
336 		case ATH11K_BD_IE_BOARD:
337 			ret = ath11k_core_parse_bd_ie_board(ab, bd, data,
338 							    ie_len,
339 							    boardname,
340 							    ATH11K_BD_IE_BOARD);
341 			if (ret == -ENOENT)
342 				/* no match found, continue */
343 				break;
344 			else if (ret)
345 				/* there was an error, bail out */
346 				goto err;
347 			/* either found or error, so stop searching */
348 			goto out;
349 		}
350 
351 		/* jump over the padding */
352 		ie_len = ALIGN(ie_len, 4);
353 
354 		len -= ie_len;
355 		data += ie_len;
356 	}
357 
358 out:
359 	if (!bd->data || !bd->len) {
360 		ath11k_err(ab,
361 			   "failed to fetch board data for %s from %s\n",
362 			   boardname, filepath);
363 		ret = -ENODATA;
364 		goto err;
365 	}
366 
367 	return 0;
368 
369 err:
370 	ath11k_core_free_bdf(ab, bd);
371 	return ret;
372 }
373 
374 static int ath11k_core_fetch_board_data_api_1(struct ath11k_base *ab,
375 					      struct ath11k_board_data *bd)
376 {
377 	bd->fw = ath11k_core_firmware_request(ab, ATH11K_DEFAULT_BOARD_FILE);
378 	if (IS_ERR(bd->fw))
379 		return PTR_ERR(bd->fw);
380 
381 	bd->data = bd->fw->data;
382 	bd->len = bd->fw->size;
383 
384 	return 0;
385 }
386 
387 #define BOARD_NAME_SIZE 100
388 int ath11k_core_fetch_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
389 {
390 	char boardname[BOARD_NAME_SIZE];
391 	int ret;
392 
393 	ret = ath11k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
394 	if (ret) {
395 		ath11k_err(ab, "failed to create board name: %d", ret);
396 		return ret;
397 	}
398 
399 	ab->bd_api = 2;
400 	ret = ath11k_core_fetch_board_data_api_n(ab, bd, boardname);
401 	if (!ret)
402 		goto success;
403 
404 	ab->bd_api = 1;
405 	ret = ath11k_core_fetch_board_data_api_1(ab, bd);
406 	if (ret) {
407 		ath11k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n",
408 			   ab->hw_params.fw.dir);
409 		return ret;
410 	}
411 
412 success:
413 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "using board api %d\n", ab->bd_api);
414 	return 0;
415 }
416 
417 static void ath11k_core_stop(struct ath11k_base *ab)
418 {
419 	if (!test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags))
420 		ath11k_qmi_firmware_stop(ab);
421 
422 	ath11k_hif_stop(ab);
423 	ath11k_wmi_detach(ab);
424 	ath11k_dp_pdev_reo_cleanup(ab);
425 
426 	/* De-Init of components as needed */
427 }
428 
429 static int ath11k_core_soc_create(struct ath11k_base *ab)
430 {
431 	int ret;
432 
433 	ret = ath11k_qmi_init_service(ab);
434 	if (ret) {
435 		ath11k_err(ab, "failed to initialize qmi :%d\n", ret);
436 		return ret;
437 	}
438 
439 	ret = ath11k_debugfs_soc_create(ab);
440 	if (ret) {
441 		ath11k_err(ab, "failed to create ath11k debugfs\n");
442 		goto err_qmi_deinit;
443 	}
444 
445 	ret = ath11k_hif_power_up(ab);
446 	if (ret) {
447 		ath11k_err(ab, "failed to power up :%d\n", ret);
448 		goto err_debugfs_reg;
449 	}
450 
451 	return 0;
452 
453 err_debugfs_reg:
454 	ath11k_debugfs_soc_destroy(ab);
455 err_qmi_deinit:
456 	ath11k_qmi_deinit_service(ab);
457 	return ret;
458 }
459 
460 static void ath11k_core_soc_destroy(struct ath11k_base *ab)
461 {
462 	ath11k_debugfs_soc_destroy(ab);
463 	ath11k_dp_free(ab);
464 	ath11k_reg_free(ab);
465 	ath11k_qmi_deinit_service(ab);
466 }
467 
468 static int ath11k_core_pdev_create(struct ath11k_base *ab)
469 {
470 	int ret;
471 
472 	ret = ath11k_debugfs_pdev_create(ab);
473 	if (ret) {
474 		ath11k_err(ab, "failed to create core pdev debugfs: %d\n", ret);
475 		return ret;
476 	}
477 
478 	ret = ath11k_mac_register(ab);
479 	if (ret) {
480 		ath11k_err(ab, "failed register the radio with mac80211: %d\n", ret);
481 		goto err_pdev_debug;
482 	}
483 
484 	ret = ath11k_dp_pdev_alloc(ab);
485 	if (ret) {
486 		ath11k_err(ab, "failed to attach DP pdev: %d\n", ret);
487 		goto err_mac_unregister;
488 	}
489 
490 	ret = ath11k_thermal_register(ab);
491 	if (ret) {
492 		ath11k_err(ab, "could not register thermal device: %d\n",
493 			   ret);
494 		goto err_dp_pdev_free;
495 	}
496 
497 	ret = ath11k_spectral_init(ab);
498 	if (ret) {
499 		ath11k_err(ab, "failed to init spectral %d\n", ret);
500 		goto err_thermal_unregister;
501 	}
502 
503 	return 0;
504 
505 err_thermal_unregister:
506 	ath11k_thermal_unregister(ab);
507 err_dp_pdev_free:
508 	ath11k_dp_pdev_free(ab);
509 err_mac_unregister:
510 	ath11k_mac_unregister(ab);
511 err_pdev_debug:
512 	ath11k_debugfs_pdev_destroy(ab);
513 
514 	return ret;
515 }
516 
517 static void ath11k_core_pdev_destroy(struct ath11k_base *ab)
518 {
519 	ath11k_spectral_deinit(ab);
520 	ath11k_thermal_unregister(ab);
521 	ath11k_mac_unregister(ab);
522 	ath11k_hif_irq_disable(ab);
523 	ath11k_dp_pdev_free(ab);
524 	ath11k_debugfs_pdev_destroy(ab);
525 }
526 
527 static int ath11k_core_start(struct ath11k_base *ab,
528 			     enum ath11k_firmware_mode mode)
529 {
530 	int ret;
531 
532 	ret = ath11k_qmi_firmware_start(ab, mode);
533 	if (ret) {
534 		ath11k_err(ab, "failed to attach wmi: %d\n", ret);
535 		return ret;
536 	}
537 
538 	ret = ath11k_wmi_attach(ab);
539 	if (ret) {
540 		ath11k_err(ab, "failed to attach wmi: %d\n", ret);
541 		goto err_firmware_stop;
542 	}
543 
544 	ret = ath11k_htc_init(ab);
545 	if (ret) {
546 		ath11k_err(ab, "failed to init htc: %d\n", ret);
547 		goto err_wmi_detach;
548 	}
549 
550 	ret = ath11k_hif_start(ab);
551 	if (ret) {
552 		ath11k_err(ab, "failed to start HIF: %d\n", ret);
553 		goto err_wmi_detach;
554 	}
555 
556 	ret = ath11k_htc_wait_target(&ab->htc);
557 	if (ret) {
558 		ath11k_err(ab, "failed to connect to HTC: %d\n", ret);
559 		goto err_hif_stop;
560 	}
561 
562 	ret = ath11k_dp_htt_connect(&ab->dp);
563 	if (ret) {
564 		ath11k_err(ab, "failed to connect to HTT: %d\n", ret);
565 		goto err_hif_stop;
566 	}
567 
568 	ret = ath11k_wmi_connect(ab);
569 	if (ret) {
570 		ath11k_err(ab, "failed to connect wmi: %d\n", ret);
571 		goto err_hif_stop;
572 	}
573 
574 	ret = ath11k_htc_start(&ab->htc);
575 	if (ret) {
576 		ath11k_err(ab, "failed to start HTC: %d\n", ret);
577 		goto err_hif_stop;
578 	}
579 
580 	ret = ath11k_wmi_wait_for_service_ready(ab);
581 	if (ret) {
582 		ath11k_err(ab, "failed to receive wmi service ready event: %d\n",
583 			   ret);
584 		goto err_hif_stop;
585 	}
586 
587 	ret = ath11k_mac_allocate(ab);
588 	if (ret) {
589 		ath11k_err(ab, "failed to create new hw device with mac80211 :%d\n",
590 			   ret);
591 		goto err_hif_stop;
592 	}
593 
594 	ath11k_dp_pdev_pre_alloc(ab);
595 
596 	ret = ath11k_dp_pdev_reo_setup(ab);
597 	if (ret) {
598 		ath11k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
599 		goto err_mac_destroy;
600 	}
601 
602 	ret = ath11k_wmi_cmd_init(ab);
603 	if (ret) {
604 		ath11k_err(ab, "failed to send wmi init cmd: %d\n", ret);
605 		goto err_reo_cleanup;
606 	}
607 
608 	ret = ath11k_wmi_wait_for_unified_ready(ab);
609 	if (ret) {
610 		ath11k_err(ab, "failed to receive wmi unified ready event: %d\n",
611 			   ret);
612 		goto err_reo_cleanup;
613 	}
614 
615 	ret = ath11k_dp_tx_htt_h2t_ver_req_msg(ab);
616 	if (ret) {
617 		ath11k_err(ab, "failed to send htt version request message: %d\n",
618 			   ret);
619 		goto err_reo_cleanup;
620 	}
621 
622 	return 0;
623 
624 err_reo_cleanup:
625 	ath11k_dp_pdev_reo_cleanup(ab);
626 err_mac_destroy:
627 	ath11k_mac_destroy(ab);
628 err_hif_stop:
629 	ath11k_hif_stop(ab);
630 err_wmi_detach:
631 	ath11k_wmi_detach(ab);
632 err_firmware_stop:
633 	ath11k_qmi_firmware_stop(ab);
634 
635 	return ret;
636 }
637 
638 int ath11k_core_qmi_firmware_ready(struct ath11k_base *ab)
639 {
640 	int ret;
641 
642 	ret = ath11k_ce_init_pipes(ab);
643 	if (ret) {
644 		ath11k_err(ab, "failed to initialize CE: %d\n", ret);
645 		return ret;
646 	}
647 
648 	ret = ath11k_dp_alloc(ab);
649 	if (ret) {
650 		ath11k_err(ab, "failed to init DP: %d\n", ret);
651 		return ret;
652 	}
653 
654 	switch (ath11k_crypto_mode) {
655 	case ATH11K_CRYPT_MODE_SW:
656 		set_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags);
657 		set_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
658 		break;
659 	case ATH11K_CRYPT_MODE_HW:
660 		clear_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags);
661 		clear_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
662 		break;
663 	default:
664 		ath11k_info(ab, "invalid crypto_mode: %d\n", ath11k_crypto_mode);
665 		return -EINVAL;
666 	}
667 
668 	if (ath11k_frame_mode == ATH11K_HW_TXRX_RAW)
669 		set_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
670 
671 	mutex_lock(&ab->core_lock);
672 	ret = ath11k_core_start(ab, ATH11K_FIRMWARE_MODE_NORMAL);
673 	if (ret) {
674 		ath11k_err(ab, "failed to start core: %d\n", ret);
675 		goto err_dp_free;
676 	}
677 
678 	ret = ath11k_core_pdev_create(ab);
679 	if (ret) {
680 		ath11k_err(ab, "failed to create pdev core: %d\n", ret);
681 		goto err_core_stop;
682 	}
683 	ath11k_hif_irq_enable(ab);
684 	mutex_unlock(&ab->core_lock);
685 
686 	return 0;
687 
688 err_core_stop:
689 	ath11k_core_stop(ab);
690 	ath11k_mac_destroy(ab);
691 err_dp_free:
692 	ath11k_dp_free(ab);
693 	mutex_unlock(&ab->core_lock);
694 	return ret;
695 }
696 
697 static int ath11k_core_reconfigure_on_crash(struct ath11k_base *ab)
698 {
699 	int ret;
700 
701 	mutex_lock(&ab->core_lock);
702 	ath11k_thermal_unregister(ab);
703 	ath11k_hif_irq_disable(ab);
704 	ath11k_dp_pdev_free(ab);
705 	ath11k_spectral_deinit(ab);
706 	ath11k_hif_stop(ab);
707 	ath11k_wmi_detach(ab);
708 	ath11k_dp_pdev_reo_cleanup(ab);
709 	mutex_unlock(&ab->core_lock);
710 
711 	ath11k_dp_free(ab);
712 	ath11k_hal_srng_deinit(ab);
713 
714 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
715 
716 	ret = ath11k_hal_srng_init(ab);
717 	if (ret)
718 		return ret;
719 
720 	clear_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags);
721 
722 	ret = ath11k_core_qmi_firmware_ready(ab);
723 	if (ret)
724 		goto err_hal_srng_deinit;
725 
726 	clear_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags);
727 
728 	return 0;
729 
730 err_hal_srng_deinit:
731 	ath11k_hal_srng_deinit(ab);
732 	return ret;
733 }
734 
735 void ath11k_core_halt(struct ath11k *ar)
736 {
737 	struct ath11k_base *ab = ar->ab;
738 
739 	lockdep_assert_held(&ar->conf_mutex);
740 
741 	ar->num_created_vdevs = 0;
742 	ar->allocated_vdev_map = 0;
743 
744 	ath11k_mac_scan_finish(ar);
745 	ath11k_mac_peer_cleanup_all(ar);
746 	cancel_delayed_work_sync(&ar->scan.timeout);
747 	cancel_work_sync(&ar->regd_update_work);
748 
749 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
750 	synchronize_rcu();
751 	INIT_LIST_HEAD(&ar->arvifs);
752 	idr_init(&ar->txmgmt_idr);
753 }
754 
755 static void ath11k_core_restart(struct work_struct *work)
756 {
757 	struct ath11k_base *ab = container_of(work, struct ath11k_base, restart_work);
758 	struct ath11k *ar;
759 	struct ath11k_pdev *pdev;
760 	int i, ret = 0;
761 
762 	spin_lock_bh(&ab->base_lock);
763 	ab->stats.fw_crash_counter++;
764 	spin_unlock_bh(&ab->base_lock);
765 
766 	for (i = 0; i < ab->num_radios; i++) {
767 		pdev = &ab->pdevs[i];
768 		ar = pdev->ar;
769 		if (!ar || ar->state == ATH11K_STATE_OFF)
770 			continue;
771 
772 		ieee80211_stop_queues(ar->hw);
773 		ath11k_mac_drain_tx(ar);
774 		complete(&ar->scan.started);
775 		complete(&ar->scan.completed);
776 		complete(&ar->peer_assoc_done);
777 		complete(&ar->install_key_done);
778 		complete(&ar->vdev_setup_done);
779 		complete(&ar->bss_survey_done);
780 		complete(&ar->thermal.wmi_sync);
781 
782 		wake_up(&ar->dp.tx_empty_waitq);
783 		idr_for_each(&ar->txmgmt_idr,
784 			     ath11k_mac_tx_mgmt_pending_free, ar);
785 		idr_destroy(&ar->txmgmt_idr);
786 	}
787 
788 	wake_up(&ab->wmi_ab.tx_credits_wq);
789 	wake_up(&ab->peer_mapping_wq);
790 
791 	ret = ath11k_core_reconfigure_on_crash(ab);
792 	if (ret) {
793 		ath11k_err(ab, "failed to reconfigure driver on crash recovery\n");
794 		return;
795 	}
796 
797 	for (i = 0; i < ab->num_radios; i++) {
798 		pdev = &ab->pdevs[i];
799 		ar = pdev->ar;
800 		if (!ar || ar->state == ATH11K_STATE_OFF)
801 			continue;
802 
803 		mutex_lock(&ar->conf_mutex);
804 
805 		switch (ar->state) {
806 		case ATH11K_STATE_ON:
807 			ar->state = ATH11K_STATE_RESTARTING;
808 			ath11k_core_halt(ar);
809 			ieee80211_restart_hw(ar->hw);
810 			break;
811 		case ATH11K_STATE_OFF:
812 			ath11k_warn(ab,
813 				    "cannot restart radio %d that hasn't been started\n",
814 				    i);
815 			break;
816 		case ATH11K_STATE_RESTARTING:
817 			break;
818 		case ATH11K_STATE_RESTARTED:
819 			ar->state = ATH11K_STATE_WEDGED;
820 			fallthrough;
821 		case ATH11K_STATE_WEDGED:
822 			ath11k_warn(ab,
823 				    "device is wedged, will not restart radio %d\n", i);
824 			break;
825 		}
826 		mutex_unlock(&ar->conf_mutex);
827 	}
828 	complete(&ab->driver_recovery);
829 }
830 
831 static int ath11k_init_hw_params(struct ath11k_base *ab)
832 {
833 	const struct ath11k_hw_params *hw_params = NULL;
834 	int i;
835 
836 	for (i = 0; i < ARRAY_SIZE(ath11k_hw_params); i++) {
837 		hw_params = &ath11k_hw_params[i];
838 
839 		if (hw_params->hw_rev == ab->hw_rev)
840 			break;
841 	}
842 
843 	if (i == ARRAY_SIZE(ath11k_hw_params)) {
844 		ath11k_err(ab, "Unsupported hardware version: 0x%x\n", ab->hw_rev);
845 		return -EINVAL;
846 	}
847 
848 	ab->hw_params = *hw_params;
849 
850 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "Hardware name %s\n", ab->hw_params.name);
851 
852 	return 0;
853 }
854 
855 int ath11k_core_pre_init(struct ath11k_base *ab)
856 {
857 	int ret;
858 
859 	ret = ath11k_init_hw_params(ab);
860 	if (ret) {
861 		ath11k_err(ab, "failed to get hw params: %d\n", ret);
862 		return ret;
863 	}
864 
865 	return 0;
866 }
867 EXPORT_SYMBOL(ath11k_core_pre_init);
868 
869 int ath11k_core_init(struct ath11k_base *ab)
870 {
871 	int ret;
872 
873 	ret = ath11k_core_soc_create(ab);
874 	if (ret) {
875 		ath11k_err(ab, "failed to create soc core: %d\n", ret);
876 		return ret;
877 	}
878 
879 	return 0;
880 }
881 EXPORT_SYMBOL(ath11k_core_init);
882 
883 void ath11k_core_deinit(struct ath11k_base *ab)
884 {
885 	mutex_lock(&ab->core_lock);
886 
887 	ath11k_core_pdev_destroy(ab);
888 	ath11k_core_stop(ab);
889 
890 	mutex_unlock(&ab->core_lock);
891 
892 	ath11k_hif_power_down(ab);
893 	ath11k_mac_destroy(ab);
894 	ath11k_core_soc_destroy(ab);
895 }
896 EXPORT_SYMBOL(ath11k_core_deinit);
897 
898 void ath11k_core_free(struct ath11k_base *ab)
899 {
900 	kfree(ab);
901 }
902 EXPORT_SYMBOL(ath11k_core_free);
903 
904 struct ath11k_base *ath11k_core_alloc(struct device *dev, size_t priv_size,
905 				      enum ath11k_bus bus,
906 				      const struct ath11k_bus_params *bus_params)
907 {
908 	struct ath11k_base *ab;
909 
910 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
911 	if (!ab)
912 		return NULL;
913 
914 	init_completion(&ab->driver_recovery);
915 
916 	ab->workqueue = create_singlethread_workqueue("ath11k_wq");
917 	if (!ab->workqueue)
918 		goto err_sc_free;
919 
920 	mutex_init(&ab->core_lock);
921 	spin_lock_init(&ab->base_lock);
922 
923 	INIT_LIST_HEAD(&ab->peers);
924 	init_waitqueue_head(&ab->peer_mapping_wq);
925 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
926 	INIT_WORK(&ab->restart_work, ath11k_core_restart);
927 	timer_setup(&ab->rx_replenish_retry, ath11k_ce_rx_replenish_retry, 0);
928 	ab->dev = dev;
929 	ab->bus_params = *bus_params;
930 	ab->hif.bus = bus;
931 
932 	return ab;
933 
934 err_sc_free:
935 	kfree(ab);
936 	return NULL;
937 }
938 EXPORT_SYMBOL(ath11k_core_alloc);
939 
940 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11ax wireless LAN cards.");
941 MODULE_LICENSE("Dual BSD/GPL");
942