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