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