xref: /openbmc/linux/drivers/net/wireless/ath/ath10k/core.c (revision 029f7f3b8701cc7aca8bdb31f0c7edd6a479e357)
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/firmware.h>
20 #include <linux/of.h>
21 
22 #include "core.h"
23 #include "mac.h"
24 #include "htc.h"
25 #include "hif.h"
26 #include "wmi.h"
27 #include "bmi.h"
28 #include "debug.h"
29 #include "htt.h"
30 #include "testmode.h"
31 #include "wmi-ops.h"
32 
33 unsigned int ath10k_debug_mask;
34 static unsigned int ath10k_cryptmode_param;
35 static bool uart_print;
36 static bool skip_otp;
37 static bool rawmode;
38 
39 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
40 module_param_named(cryptmode, ath10k_cryptmode_param, uint, 0644);
41 module_param(uart_print, bool, 0644);
42 module_param(skip_otp, bool, 0644);
43 module_param(rawmode, bool, 0644);
44 
45 MODULE_PARM_DESC(debug_mask, "Debugging mask");
46 MODULE_PARM_DESC(uart_print, "Uart target debugging");
47 MODULE_PARM_DESC(skip_otp, "Skip otp failure for calibration in testmode");
48 MODULE_PARM_DESC(cryptmode, "Crypto mode: 0-hardware, 1-software");
49 MODULE_PARM_DESC(rawmode, "Use raw 802.11 frame datapath");
50 
51 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
52 	{
53 		.id = QCA988X_HW_2_0_VERSION,
54 		.name = "qca988x hw2.0",
55 		.patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
56 		.uart_pin = 7,
57 		.has_shifted_cc_wraparound = true,
58 		.otp_exe_param = 0,
59 		.channel_counters_freq_hz = 88000,
60 		.max_probe_resp_desc_thres = 0,
61 		.fw = {
62 			.dir = QCA988X_HW_2_0_FW_DIR,
63 			.fw = QCA988X_HW_2_0_FW_FILE,
64 			.otp = QCA988X_HW_2_0_OTP_FILE,
65 			.board = QCA988X_HW_2_0_BOARD_DATA_FILE,
66 			.board_size = QCA988X_BOARD_DATA_SZ,
67 			.board_ext_size = QCA988X_BOARD_EXT_DATA_SZ,
68 		},
69 	},
70 	{
71 		.id = QCA6174_HW_2_1_VERSION,
72 		.name = "qca6174 hw2.1",
73 		.patch_load_addr = QCA6174_HW_2_1_PATCH_LOAD_ADDR,
74 		.uart_pin = 6,
75 		.otp_exe_param = 0,
76 		.channel_counters_freq_hz = 88000,
77 		.max_probe_resp_desc_thres = 0,
78 		.fw = {
79 			.dir = QCA6174_HW_2_1_FW_DIR,
80 			.fw = QCA6174_HW_2_1_FW_FILE,
81 			.otp = QCA6174_HW_2_1_OTP_FILE,
82 			.board = QCA6174_HW_2_1_BOARD_DATA_FILE,
83 			.board_size = QCA6174_BOARD_DATA_SZ,
84 			.board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
85 		},
86 	},
87 	{
88 		.id = QCA6174_HW_3_0_VERSION,
89 		.name = "qca6174 hw3.0",
90 		.patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
91 		.uart_pin = 6,
92 		.otp_exe_param = 0,
93 		.channel_counters_freq_hz = 88000,
94 		.max_probe_resp_desc_thres = 0,
95 		.fw = {
96 			.dir = QCA6174_HW_3_0_FW_DIR,
97 			.fw = QCA6174_HW_3_0_FW_FILE,
98 			.otp = QCA6174_HW_3_0_OTP_FILE,
99 			.board = QCA6174_HW_3_0_BOARD_DATA_FILE,
100 			.board_size = QCA6174_BOARD_DATA_SZ,
101 			.board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
102 		},
103 	},
104 	{
105 		.id = QCA6174_HW_3_2_VERSION,
106 		.name = "qca6174 hw3.2",
107 		.patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
108 		.uart_pin = 6,
109 		.otp_exe_param = 0,
110 		.channel_counters_freq_hz = 88000,
111 		.max_probe_resp_desc_thres = 0,
112 		.fw = {
113 			/* uses same binaries as hw3.0 */
114 			.dir = QCA6174_HW_3_0_FW_DIR,
115 			.fw = QCA6174_HW_3_0_FW_FILE,
116 			.otp = QCA6174_HW_3_0_OTP_FILE,
117 			.board = QCA6174_HW_3_0_BOARD_DATA_FILE,
118 			.board_size = QCA6174_BOARD_DATA_SZ,
119 			.board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
120 		},
121 	},
122 	{
123 		.id = QCA99X0_HW_2_0_DEV_VERSION,
124 		.name = "qca99x0 hw2.0",
125 		.patch_load_addr = QCA99X0_HW_2_0_PATCH_LOAD_ADDR,
126 		.uart_pin = 7,
127 		.otp_exe_param = 0x00000700,
128 		.continuous_frag_desc = true,
129 		.channel_counters_freq_hz = 150000,
130 		.max_probe_resp_desc_thres = 24,
131 		.fw = {
132 			.dir = QCA99X0_HW_2_0_FW_DIR,
133 			.fw = QCA99X0_HW_2_0_FW_FILE,
134 			.otp = QCA99X0_HW_2_0_OTP_FILE,
135 			.board = QCA99X0_HW_2_0_BOARD_DATA_FILE,
136 			.board_size = QCA99X0_BOARD_DATA_SZ,
137 			.board_ext_size = QCA99X0_BOARD_EXT_DATA_SZ,
138 		},
139 	},
140 	{
141 		.id = QCA9377_HW_1_0_DEV_VERSION,
142 		.name = "qca9377 hw1.0",
143 		.patch_load_addr = QCA9377_HW_1_0_PATCH_LOAD_ADDR,
144 		.uart_pin = 7,
145 		.otp_exe_param = 0,
146 		.fw = {
147 			.dir = QCA9377_HW_1_0_FW_DIR,
148 			.fw = QCA9377_HW_1_0_FW_FILE,
149 			.otp = QCA9377_HW_1_0_OTP_FILE,
150 			.board = QCA9377_HW_1_0_BOARD_DATA_FILE,
151 			.board_size = QCA9377_BOARD_DATA_SZ,
152 			.board_ext_size = QCA9377_BOARD_EXT_DATA_SZ,
153 		},
154 	},
155 };
156 
157 static const char *const ath10k_core_fw_feature_str[] = {
158 	[ATH10K_FW_FEATURE_EXT_WMI_MGMT_RX] = "wmi-mgmt-rx",
159 	[ATH10K_FW_FEATURE_WMI_10X] = "wmi-10.x",
160 	[ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX] = "has-wmi-mgmt-tx",
161 	[ATH10K_FW_FEATURE_NO_P2P] = "no-p2p",
162 	[ATH10K_FW_FEATURE_WMI_10_2] = "wmi-10.2",
163 	[ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT] = "multi-vif-ps",
164 	[ATH10K_FW_FEATURE_WOWLAN_SUPPORT] = "wowlan",
165 	[ATH10K_FW_FEATURE_IGNORE_OTP_RESULT] = "ignore-otp",
166 	[ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING] = "no-4addr-pad",
167 	[ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT] = "skip-clock-init",
168 	[ATH10K_FW_FEATURE_RAW_MODE_SUPPORT] = "raw-mode",
169 	[ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA] = "adaptive-cca",
170 };
171 
172 static unsigned int ath10k_core_get_fw_feature_str(char *buf,
173 						   size_t buf_len,
174 						   enum ath10k_fw_features feat)
175 {
176 	/* make sure that ath10k_core_fw_feature_str[] gets updated */
177 	BUILD_BUG_ON(ARRAY_SIZE(ath10k_core_fw_feature_str) !=
178 		     ATH10K_FW_FEATURE_COUNT);
179 
180 	if (feat >= ARRAY_SIZE(ath10k_core_fw_feature_str) ||
181 	    WARN_ON(!ath10k_core_fw_feature_str[feat])) {
182 		return scnprintf(buf, buf_len, "bit%d", feat);
183 	}
184 
185 	return scnprintf(buf, buf_len, "%s", ath10k_core_fw_feature_str[feat]);
186 }
187 
188 void ath10k_core_get_fw_features_str(struct ath10k *ar,
189 				     char *buf,
190 				     size_t buf_len)
191 {
192 	unsigned int len = 0;
193 	int i;
194 
195 	for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
196 		if (test_bit(i, ar->fw_features)) {
197 			if (len > 0)
198 				len += scnprintf(buf + len, buf_len - len, ",");
199 
200 			len += ath10k_core_get_fw_feature_str(buf + len,
201 							      buf_len - len,
202 							      i);
203 		}
204 	}
205 }
206 
207 static void ath10k_send_suspend_complete(struct ath10k *ar)
208 {
209 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot suspend complete\n");
210 
211 	complete(&ar->target_suspend);
212 }
213 
214 static int ath10k_init_configure_target(struct ath10k *ar)
215 {
216 	u32 param_host;
217 	int ret;
218 
219 	/* tell target which HTC version it is used*/
220 	ret = ath10k_bmi_write32(ar, hi_app_host_interest,
221 				 HTC_PROTOCOL_VERSION);
222 	if (ret) {
223 		ath10k_err(ar, "settings HTC version failed\n");
224 		return ret;
225 	}
226 
227 	/* set the firmware mode to STA/IBSS/AP */
228 	ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
229 	if (ret) {
230 		ath10k_err(ar, "setting firmware mode (1/2) failed\n");
231 		return ret;
232 	}
233 
234 	/* TODO following parameters need to be re-visited. */
235 	/* num_device */
236 	param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
237 	/* Firmware mode */
238 	/* FIXME: Why FW_MODE_AP ??.*/
239 	param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
240 	/* mac_addr_method */
241 	param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
242 	/* firmware_bridge */
243 	param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
244 	/* fwsubmode */
245 	param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
246 
247 	ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
248 	if (ret) {
249 		ath10k_err(ar, "setting firmware mode (2/2) failed\n");
250 		return ret;
251 	}
252 
253 	/* We do all byte-swapping on the host */
254 	ret = ath10k_bmi_write32(ar, hi_be, 0);
255 	if (ret) {
256 		ath10k_err(ar, "setting host CPU BE mode failed\n");
257 		return ret;
258 	}
259 
260 	/* FW descriptor/Data swap flags */
261 	ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
262 
263 	if (ret) {
264 		ath10k_err(ar, "setting FW data/desc swap flags failed\n");
265 		return ret;
266 	}
267 
268 	/* Some devices have a special sanity check that verifies the PCI
269 	 * Device ID is written to this host interest var. It is known to be
270 	 * required to boot QCA6164.
271 	 */
272 	ret = ath10k_bmi_write32(ar, hi_hci_uart_pwr_mgmt_params_ext,
273 				 ar->dev_id);
274 	if (ret) {
275 		ath10k_err(ar, "failed to set pwr_mgmt_params: %d\n", ret);
276 		return ret;
277 	}
278 
279 	return 0;
280 }
281 
282 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
283 						   const char *dir,
284 						   const char *file)
285 {
286 	char filename[100];
287 	const struct firmware *fw;
288 	int ret;
289 
290 	if (file == NULL)
291 		return ERR_PTR(-ENOENT);
292 
293 	if (dir == NULL)
294 		dir = ".";
295 
296 	snprintf(filename, sizeof(filename), "%s/%s", dir, file);
297 	ret = request_firmware(&fw, filename, ar->dev);
298 	if (ret)
299 		return ERR_PTR(ret);
300 
301 	return fw;
302 }
303 
304 static int ath10k_push_board_ext_data(struct ath10k *ar, const void *data,
305 				      size_t data_len)
306 {
307 	u32 board_data_size = ar->hw_params.fw.board_size;
308 	u32 board_ext_data_size = ar->hw_params.fw.board_ext_size;
309 	u32 board_ext_data_addr;
310 	int ret;
311 
312 	ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
313 	if (ret) {
314 		ath10k_err(ar, "could not read board ext data addr (%d)\n",
315 			   ret);
316 		return ret;
317 	}
318 
319 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
320 		   "boot push board extended data addr 0x%x\n",
321 		   board_ext_data_addr);
322 
323 	if (board_ext_data_addr == 0)
324 		return 0;
325 
326 	if (data_len != (board_data_size + board_ext_data_size)) {
327 		ath10k_err(ar, "invalid board (ext) data sizes %zu != %d+%d\n",
328 			   data_len, board_data_size, board_ext_data_size);
329 		return -EINVAL;
330 	}
331 
332 	ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
333 				      data + board_data_size,
334 				      board_ext_data_size);
335 	if (ret) {
336 		ath10k_err(ar, "could not write board ext data (%d)\n", ret);
337 		return ret;
338 	}
339 
340 	ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
341 				 (board_ext_data_size << 16) | 1);
342 	if (ret) {
343 		ath10k_err(ar, "could not write board ext data bit (%d)\n",
344 			   ret);
345 		return ret;
346 	}
347 
348 	return 0;
349 }
350 
351 static int ath10k_download_board_data(struct ath10k *ar, const void *data,
352 				      size_t data_len)
353 {
354 	u32 board_data_size = ar->hw_params.fw.board_size;
355 	u32 address;
356 	int ret;
357 
358 	ret = ath10k_push_board_ext_data(ar, data, data_len);
359 	if (ret) {
360 		ath10k_err(ar, "could not push board ext data (%d)\n", ret);
361 		goto exit;
362 	}
363 
364 	ret = ath10k_bmi_read32(ar, hi_board_data, &address);
365 	if (ret) {
366 		ath10k_err(ar, "could not read board data addr (%d)\n", ret);
367 		goto exit;
368 	}
369 
370 	ret = ath10k_bmi_write_memory(ar, address, data,
371 				      min_t(u32, board_data_size,
372 					    data_len));
373 	if (ret) {
374 		ath10k_err(ar, "could not write board data (%d)\n", ret);
375 		goto exit;
376 	}
377 
378 	ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
379 	if (ret) {
380 		ath10k_err(ar, "could not write board data bit (%d)\n", ret);
381 		goto exit;
382 	}
383 
384 exit:
385 	return ret;
386 }
387 
388 static int ath10k_download_cal_file(struct ath10k *ar)
389 {
390 	int ret;
391 
392 	if (!ar->cal_file)
393 		return -ENOENT;
394 
395 	if (IS_ERR(ar->cal_file))
396 		return PTR_ERR(ar->cal_file);
397 
398 	ret = ath10k_download_board_data(ar, ar->cal_file->data,
399 					 ar->cal_file->size);
400 	if (ret) {
401 		ath10k_err(ar, "failed to download cal_file data: %d\n", ret);
402 		return ret;
403 	}
404 
405 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cal file downloaded\n");
406 
407 	return 0;
408 }
409 
410 static int ath10k_download_cal_dt(struct ath10k *ar)
411 {
412 	struct device_node *node;
413 	int data_len;
414 	void *data;
415 	int ret;
416 
417 	node = ar->dev->of_node;
418 	if (!node)
419 		/* Device Tree is optional, don't print any warnings if
420 		 * there's no node for ath10k.
421 		 */
422 		return -ENOENT;
423 
424 	if (!of_get_property(node, "qcom,ath10k-calibration-data",
425 			     &data_len)) {
426 		/* The calibration data node is optional */
427 		return -ENOENT;
428 	}
429 
430 	if (data_len != QCA988X_CAL_DATA_LEN) {
431 		ath10k_warn(ar, "invalid calibration data length in DT: %d\n",
432 			    data_len);
433 		ret = -EMSGSIZE;
434 		goto out;
435 	}
436 
437 	data = kmalloc(data_len, GFP_KERNEL);
438 	if (!data) {
439 		ret = -ENOMEM;
440 		goto out;
441 	}
442 
443 	ret = of_property_read_u8_array(node, "qcom,ath10k-calibration-data",
444 					data, data_len);
445 	if (ret) {
446 		ath10k_warn(ar, "failed to read calibration data from DT: %d\n",
447 			    ret);
448 		goto out_free;
449 	}
450 
451 	ret = ath10k_download_board_data(ar, data, data_len);
452 	if (ret) {
453 		ath10k_warn(ar, "failed to download calibration data from Device Tree: %d\n",
454 			    ret);
455 		goto out_free;
456 	}
457 
458 	ret = 0;
459 
460 out_free:
461 	kfree(data);
462 
463 out:
464 	return ret;
465 }
466 
467 static int ath10k_core_get_board_id_from_otp(struct ath10k *ar)
468 {
469 	u32 result, address;
470 	u8 board_id, chip_id;
471 	int ret;
472 
473 	address = ar->hw_params.patch_load_addr;
474 
475 	if (!ar->otp_data || !ar->otp_len) {
476 		ath10k_warn(ar,
477 			    "failed to retrieve board id because of invalid otp\n");
478 		return -ENODATA;
479 	}
480 
481 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
482 		   "boot upload otp to 0x%x len %zd for board id\n",
483 		   address, ar->otp_len);
484 
485 	ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
486 	if (ret) {
487 		ath10k_err(ar, "could not write otp for board id check: %d\n",
488 			   ret);
489 		return ret;
490 	}
491 
492 	ret = ath10k_bmi_execute(ar, address, BMI_PARAM_GET_EEPROM_BOARD_ID,
493 				 &result);
494 	if (ret) {
495 		ath10k_err(ar, "could not execute otp for board id check: %d\n",
496 			   ret);
497 		return ret;
498 	}
499 
500 	board_id = MS(result, ATH10K_BMI_BOARD_ID_FROM_OTP);
501 	chip_id = MS(result, ATH10K_BMI_CHIP_ID_FROM_OTP);
502 
503 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
504 		   "boot get otp board id result 0x%08x board_id %d chip_id %d\n",
505 		   result, board_id, chip_id);
506 
507 	if ((result & ATH10K_BMI_BOARD_ID_STATUS_MASK) != 0)
508 		return -EOPNOTSUPP;
509 
510 	ar->id.bmi_ids_valid = true;
511 	ar->id.bmi_board_id = board_id;
512 	ar->id.bmi_chip_id = chip_id;
513 
514 	return 0;
515 }
516 
517 static int ath10k_download_and_run_otp(struct ath10k *ar)
518 {
519 	u32 result, address = ar->hw_params.patch_load_addr;
520 	u32 bmi_otp_exe_param = ar->hw_params.otp_exe_param;
521 	int ret;
522 
523 	ret = ath10k_download_board_data(ar, ar->board_data, ar->board_len);
524 	if (ret) {
525 		ath10k_err(ar, "failed to download board data: %d\n", ret);
526 		return ret;
527 	}
528 
529 	/* OTP is optional */
530 
531 	if (!ar->otp_data || !ar->otp_len) {
532 		ath10k_warn(ar, "Not running otp, calibration will be incorrect (otp-data %p otp_len %zd)!\n",
533 			    ar->otp_data, ar->otp_len);
534 		return 0;
535 	}
536 
537 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot upload otp to 0x%x len %zd\n",
538 		   address, ar->otp_len);
539 
540 	ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
541 	if (ret) {
542 		ath10k_err(ar, "could not write otp (%d)\n", ret);
543 		return ret;
544 	}
545 
546 	ret = ath10k_bmi_execute(ar, address, bmi_otp_exe_param, &result);
547 	if (ret) {
548 		ath10k_err(ar, "could not execute otp (%d)\n", ret);
549 		return ret;
550 	}
551 
552 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot otp execute result %d\n", result);
553 
554 	if (!(skip_otp || test_bit(ATH10K_FW_FEATURE_IGNORE_OTP_RESULT,
555 				   ar->fw_features)) &&
556 	    result != 0) {
557 		ath10k_err(ar, "otp calibration failed: %d", result);
558 		return -EINVAL;
559 	}
560 
561 	return 0;
562 }
563 
564 static int ath10k_download_fw(struct ath10k *ar, enum ath10k_firmware_mode mode)
565 {
566 	u32 address, data_len;
567 	const char *mode_name;
568 	const void *data;
569 	int ret;
570 
571 	address = ar->hw_params.patch_load_addr;
572 
573 	switch (mode) {
574 	case ATH10K_FIRMWARE_MODE_NORMAL:
575 		data = ar->firmware_data;
576 		data_len = ar->firmware_len;
577 		mode_name = "normal";
578 		ret = ath10k_swap_code_seg_configure(ar,
579 						     ATH10K_SWAP_CODE_SEG_BIN_TYPE_FW);
580 		if (ret) {
581 			ath10k_err(ar, "failed to configure fw code swap: %d\n",
582 				   ret);
583 			return ret;
584 		}
585 		break;
586 	case ATH10K_FIRMWARE_MODE_UTF:
587 		data = ar->testmode.utf_firmware_data;
588 		data_len = ar->testmode.utf_firmware_len;
589 		mode_name = "utf";
590 		break;
591 	default:
592 		ath10k_err(ar, "unknown firmware mode: %d\n", mode);
593 		return -EINVAL;
594 	}
595 
596 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
597 		   "boot uploading firmware image %p len %d mode %s\n",
598 		   data, data_len, mode_name);
599 
600 	ret = ath10k_bmi_fast_download(ar, address, data, data_len);
601 	if (ret) {
602 		ath10k_err(ar, "failed to download %s firmware: %d\n",
603 			   mode_name, ret);
604 		return ret;
605 	}
606 
607 	return ret;
608 }
609 
610 static void ath10k_core_free_board_files(struct ath10k *ar)
611 {
612 	if (!IS_ERR(ar->board))
613 		release_firmware(ar->board);
614 
615 	ar->board = NULL;
616 	ar->board_data = NULL;
617 	ar->board_len = 0;
618 }
619 
620 static void ath10k_core_free_firmware_files(struct ath10k *ar)
621 {
622 	if (!IS_ERR(ar->otp))
623 		release_firmware(ar->otp);
624 
625 	if (!IS_ERR(ar->firmware))
626 		release_firmware(ar->firmware);
627 
628 	if (!IS_ERR(ar->cal_file))
629 		release_firmware(ar->cal_file);
630 
631 	ath10k_swap_code_seg_release(ar);
632 
633 	ar->otp = NULL;
634 	ar->otp_data = NULL;
635 	ar->otp_len = 0;
636 
637 	ar->firmware = NULL;
638 	ar->firmware_data = NULL;
639 	ar->firmware_len = 0;
640 
641 	ar->cal_file = NULL;
642 }
643 
644 static int ath10k_fetch_cal_file(struct ath10k *ar)
645 {
646 	char filename[100];
647 
648 	/* cal-<bus>-<id>.bin */
649 	scnprintf(filename, sizeof(filename), "cal-%s-%s.bin",
650 		  ath10k_bus_str(ar->hif.bus), dev_name(ar->dev));
651 
652 	ar->cal_file = ath10k_fetch_fw_file(ar, ATH10K_FW_DIR, filename);
653 	if (IS_ERR(ar->cal_file))
654 		/* calibration file is optional, don't print any warnings */
655 		return PTR_ERR(ar->cal_file);
656 
657 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "found calibration file %s/%s\n",
658 		   ATH10K_FW_DIR, filename);
659 
660 	return 0;
661 }
662 
663 static int ath10k_core_fetch_board_data_api_1(struct ath10k *ar)
664 {
665 	if (!ar->hw_params.fw.board) {
666 		ath10k_err(ar, "failed to find board file fw entry\n");
667 		return -EINVAL;
668 	}
669 
670 	ar->board = ath10k_fetch_fw_file(ar,
671 					 ar->hw_params.fw.dir,
672 					 ar->hw_params.fw.board);
673 	if (IS_ERR(ar->board))
674 		return PTR_ERR(ar->board);
675 
676 	ar->board_data = ar->board->data;
677 	ar->board_len = ar->board->size;
678 
679 	return 0;
680 }
681 
682 static int ath10k_core_parse_bd_ie_board(struct ath10k *ar,
683 					 const void *buf, size_t buf_len,
684 					 const char *boardname)
685 {
686 	const struct ath10k_fw_ie *hdr;
687 	bool name_match_found;
688 	int ret, board_ie_id;
689 	size_t board_ie_len;
690 	const void *board_ie_data;
691 
692 	name_match_found = false;
693 
694 	/* go through ATH10K_BD_IE_BOARD_ elements */
695 	while (buf_len > sizeof(struct ath10k_fw_ie)) {
696 		hdr = buf;
697 		board_ie_id = le32_to_cpu(hdr->id);
698 		board_ie_len = le32_to_cpu(hdr->len);
699 		board_ie_data = hdr->data;
700 
701 		buf_len -= sizeof(*hdr);
702 		buf += sizeof(*hdr);
703 
704 		if (buf_len < ALIGN(board_ie_len, 4)) {
705 			ath10k_err(ar, "invalid ATH10K_BD_IE_BOARD length: %zu < %zu\n",
706 				   buf_len, ALIGN(board_ie_len, 4));
707 			ret = -EINVAL;
708 			goto out;
709 		}
710 
711 		switch (board_ie_id) {
712 		case ATH10K_BD_IE_BOARD_NAME:
713 			ath10k_dbg_dump(ar, ATH10K_DBG_BOOT, "board name", "",
714 					board_ie_data, board_ie_len);
715 
716 			if (board_ie_len != strlen(boardname))
717 				break;
718 
719 			ret = memcmp(board_ie_data, boardname, strlen(boardname));
720 			if (ret)
721 				break;
722 
723 			name_match_found = true;
724 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
725 				   "boot found match for name '%s'",
726 				   boardname);
727 			break;
728 		case ATH10K_BD_IE_BOARD_DATA:
729 			if (!name_match_found)
730 				/* no match found */
731 				break;
732 
733 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
734 				   "boot found board data for '%s'",
735 				   boardname);
736 
737 			ar->board_data = board_ie_data;
738 			ar->board_len = board_ie_len;
739 
740 			ret = 0;
741 			goto out;
742 		default:
743 			ath10k_warn(ar, "unknown ATH10K_BD_IE_BOARD found: %d\n",
744 				    board_ie_id);
745 			break;
746 		}
747 
748 		/* jump over the padding */
749 		board_ie_len = ALIGN(board_ie_len, 4);
750 
751 		buf_len -= board_ie_len;
752 		buf += board_ie_len;
753 	}
754 
755 	/* no match found */
756 	ret = -ENOENT;
757 
758 out:
759 	return ret;
760 }
761 
762 static int ath10k_core_fetch_board_data_api_n(struct ath10k *ar,
763 					      const char *boardname,
764 					      const char *filename)
765 {
766 	size_t len, magic_len, ie_len;
767 	struct ath10k_fw_ie *hdr;
768 	const u8 *data;
769 	int ret, ie_id;
770 
771 	ar->board = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, filename);
772 	if (IS_ERR(ar->board))
773 		return PTR_ERR(ar->board);
774 
775 	data = ar->board->data;
776 	len = ar->board->size;
777 
778 	/* magic has extra null byte padded */
779 	magic_len = strlen(ATH10K_BOARD_MAGIC) + 1;
780 	if (len < magic_len) {
781 		ath10k_err(ar, "failed to find magic value in %s/%s, file too short: %zu\n",
782 			   ar->hw_params.fw.dir, filename, len);
783 		ret = -EINVAL;
784 		goto err;
785 	}
786 
787 	if (memcmp(data, ATH10K_BOARD_MAGIC, magic_len)) {
788 		ath10k_err(ar, "found invalid board magic\n");
789 		ret = -EINVAL;
790 		goto err;
791 	}
792 
793 	/* magic is padded to 4 bytes */
794 	magic_len = ALIGN(magic_len, 4);
795 	if (len < magic_len) {
796 		ath10k_err(ar, "failed: %s/%s too small to contain board data, len: %zu\n",
797 			   ar->hw_params.fw.dir, filename, len);
798 		ret = -EINVAL;
799 		goto err;
800 	}
801 
802 	data += magic_len;
803 	len -= magic_len;
804 
805 	while (len > sizeof(struct ath10k_fw_ie)) {
806 		hdr = (struct ath10k_fw_ie *)data;
807 		ie_id = le32_to_cpu(hdr->id);
808 		ie_len = le32_to_cpu(hdr->len);
809 
810 		len -= sizeof(*hdr);
811 		data = hdr->data;
812 
813 		if (len < ALIGN(ie_len, 4)) {
814 			ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
815 				   ie_id, ie_len, len);
816 			ret = -EINVAL;
817 			goto err;
818 		}
819 
820 		switch (ie_id) {
821 		case ATH10K_BD_IE_BOARD:
822 			ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
823 							    boardname);
824 			if (ret == -ENOENT)
825 				/* no match found, continue */
826 				break;
827 			else if (ret)
828 				/* there was an error, bail out */
829 				goto err;
830 
831 			/* board data found */
832 			goto out;
833 		}
834 
835 		/* jump over the padding */
836 		ie_len = ALIGN(ie_len, 4);
837 
838 		len -= ie_len;
839 		data += ie_len;
840 	}
841 
842 out:
843 	if (!ar->board_data || !ar->board_len) {
844 		ath10k_err(ar,
845 			   "failed to fetch board data for %s from %s/%s\n",
846 			   ar->hw_params.fw.dir, boardname, filename);
847 		ret = -ENODATA;
848 		goto err;
849 	}
850 
851 	return 0;
852 
853 err:
854 	ath10k_core_free_board_files(ar);
855 	return ret;
856 }
857 
858 static int ath10k_core_create_board_name(struct ath10k *ar, char *name,
859 					 size_t name_len)
860 {
861 	if (ar->id.bmi_ids_valid) {
862 		scnprintf(name, name_len,
863 			  "bus=%s,bmi-chip-id=%d,bmi-board-id=%d",
864 			  ath10k_bus_str(ar->hif.bus),
865 			  ar->id.bmi_chip_id,
866 			  ar->id.bmi_board_id);
867 		goto out;
868 	}
869 
870 	scnprintf(name, name_len,
871 		  "bus=%s,vendor=%04x,device=%04x,subsystem-vendor=%04x,subsystem-device=%04x",
872 		  ath10k_bus_str(ar->hif.bus),
873 		  ar->id.vendor, ar->id.device,
874 		  ar->id.subsystem_vendor, ar->id.subsystem_device);
875 
876 out:
877 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using board name '%s'\n", name);
878 
879 	return 0;
880 }
881 
882 static int ath10k_core_fetch_board_file(struct ath10k *ar)
883 {
884 	char boardname[100];
885 	int ret;
886 
887 	ret = ath10k_core_create_board_name(ar, boardname, sizeof(boardname));
888 	if (ret) {
889 		ath10k_err(ar, "failed to create board name: %d", ret);
890 		return ret;
891 	}
892 
893 	ar->bd_api = 2;
894 	ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
895 						 ATH10K_BOARD_API2_FILE);
896 	if (!ret)
897 		goto success;
898 
899 	ar->bd_api = 1;
900 	ret = ath10k_core_fetch_board_data_api_1(ar);
901 	if (ret) {
902 		ath10k_err(ar, "failed to fetch board data\n");
903 		return ret;
904 	}
905 
906 success:
907 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "using board api %d\n", ar->bd_api);
908 	return 0;
909 }
910 
911 static int ath10k_core_fetch_firmware_api_1(struct ath10k *ar)
912 {
913 	int ret = 0;
914 
915 	if (ar->hw_params.fw.fw == NULL) {
916 		ath10k_err(ar, "firmware file not defined\n");
917 		return -EINVAL;
918 	}
919 
920 	ar->firmware = ath10k_fetch_fw_file(ar,
921 					    ar->hw_params.fw.dir,
922 					    ar->hw_params.fw.fw);
923 	if (IS_ERR(ar->firmware)) {
924 		ret = PTR_ERR(ar->firmware);
925 		ath10k_err(ar, "could not fetch firmware (%d)\n", ret);
926 		goto err;
927 	}
928 
929 	ar->firmware_data = ar->firmware->data;
930 	ar->firmware_len = ar->firmware->size;
931 
932 	/* OTP may be undefined. If so, don't fetch it at all */
933 	if (ar->hw_params.fw.otp == NULL)
934 		return 0;
935 
936 	ar->otp = ath10k_fetch_fw_file(ar,
937 				       ar->hw_params.fw.dir,
938 				       ar->hw_params.fw.otp);
939 	if (IS_ERR(ar->otp)) {
940 		ret = PTR_ERR(ar->otp);
941 		ath10k_err(ar, "could not fetch otp (%d)\n", ret);
942 		goto err;
943 	}
944 
945 	ar->otp_data = ar->otp->data;
946 	ar->otp_len = ar->otp->size;
947 
948 	return 0;
949 
950 err:
951 	ath10k_core_free_firmware_files(ar);
952 	return ret;
953 }
954 
955 static int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name)
956 {
957 	size_t magic_len, len, ie_len;
958 	int ie_id, i, index, bit, ret;
959 	struct ath10k_fw_ie *hdr;
960 	const u8 *data;
961 	__le32 *timestamp, *version;
962 
963 	/* first fetch the firmware file (firmware-*.bin) */
964 	ar->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, name);
965 	if (IS_ERR(ar->firmware)) {
966 		ath10k_err(ar, "could not fetch firmware file '%s/%s': %ld\n",
967 			   ar->hw_params.fw.dir, name, PTR_ERR(ar->firmware));
968 		return PTR_ERR(ar->firmware);
969 	}
970 
971 	data = ar->firmware->data;
972 	len = ar->firmware->size;
973 
974 	/* magic also includes the null byte, check that as well */
975 	magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
976 
977 	if (len < magic_len) {
978 		ath10k_err(ar, "firmware file '%s/%s' too small to contain magic: %zu\n",
979 			   ar->hw_params.fw.dir, name, len);
980 		ret = -EINVAL;
981 		goto err;
982 	}
983 
984 	if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
985 		ath10k_err(ar, "invalid firmware magic\n");
986 		ret = -EINVAL;
987 		goto err;
988 	}
989 
990 	/* jump over the padding */
991 	magic_len = ALIGN(magic_len, 4);
992 
993 	len -= magic_len;
994 	data += magic_len;
995 
996 	/* loop elements */
997 	while (len > sizeof(struct ath10k_fw_ie)) {
998 		hdr = (struct ath10k_fw_ie *)data;
999 
1000 		ie_id = le32_to_cpu(hdr->id);
1001 		ie_len = le32_to_cpu(hdr->len);
1002 
1003 		len -= sizeof(*hdr);
1004 		data += sizeof(*hdr);
1005 
1006 		if (len < ie_len) {
1007 			ath10k_err(ar, "invalid length for FW IE %d (%zu < %zu)\n",
1008 				   ie_id, len, ie_len);
1009 			ret = -EINVAL;
1010 			goto err;
1011 		}
1012 
1013 		switch (ie_id) {
1014 		case ATH10K_FW_IE_FW_VERSION:
1015 			if (ie_len > sizeof(ar->hw->wiphy->fw_version) - 1)
1016 				break;
1017 
1018 			memcpy(ar->hw->wiphy->fw_version, data, ie_len);
1019 			ar->hw->wiphy->fw_version[ie_len] = '\0';
1020 
1021 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
1022 				   "found fw version %s\n",
1023 				    ar->hw->wiphy->fw_version);
1024 			break;
1025 		case ATH10K_FW_IE_TIMESTAMP:
1026 			if (ie_len != sizeof(u32))
1027 				break;
1028 
1029 			timestamp = (__le32 *)data;
1030 
1031 			ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw timestamp %d\n",
1032 				   le32_to_cpup(timestamp));
1033 			break;
1034 		case ATH10K_FW_IE_FEATURES:
1035 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
1036 				   "found firmware features ie (%zd B)\n",
1037 				   ie_len);
1038 
1039 			for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
1040 				index = i / 8;
1041 				bit = i % 8;
1042 
1043 				if (index == ie_len)
1044 					break;
1045 
1046 				if (data[index] & (1 << bit)) {
1047 					ath10k_dbg(ar, ATH10K_DBG_BOOT,
1048 						   "Enabling feature bit: %i\n",
1049 						   i);
1050 					__set_bit(i, ar->fw_features);
1051 				}
1052 			}
1053 
1054 			ath10k_dbg_dump(ar, ATH10K_DBG_BOOT, "features", "",
1055 					ar->fw_features,
1056 					sizeof(ar->fw_features));
1057 			break;
1058 		case ATH10K_FW_IE_FW_IMAGE:
1059 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
1060 				   "found fw image ie (%zd B)\n",
1061 				   ie_len);
1062 
1063 			ar->firmware_data = data;
1064 			ar->firmware_len = ie_len;
1065 
1066 			break;
1067 		case ATH10K_FW_IE_OTP_IMAGE:
1068 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
1069 				   "found otp image ie (%zd B)\n",
1070 				   ie_len);
1071 
1072 			ar->otp_data = data;
1073 			ar->otp_len = ie_len;
1074 
1075 			break;
1076 		case ATH10K_FW_IE_WMI_OP_VERSION:
1077 			if (ie_len != sizeof(u32))
1078 				break;
1079 
1080 			version = (__le32 *)data;
1081 
1082 			ar->wmi.op_version = le32_to_cpup(version);
1083 
1084 			ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie wmi op version %d\n",
1085 				   ar->wmi.op_version);
1086 			break;
1087 		case ATH10K_FW_IE_HTT_OP_VERSION:
1088 			if (ie_len != sizeof(u32))
1089 				break;
1090 
1091 			version = (__le32 *)data;
1092 
1093 			ar->htt.op_version = le32_to_cpup(version);
1094 
1095 			ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie htt op version %d\n",
1096 				   ar->htt.op_version);
1097 			break;
1098 		case ATH10K_FW_IE_FW_CODE_SWAP_IMAGE:
1099 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
1100 				   "found fw code swap image ie (%zd B)\n",
1101 				   ie_len);
1102 			ar->swap.firmware_codeswap_data = data;
1103 			ar->swap.firmware_codeswap_len = ie_len;
1104 			break;
1105 		default:
1106 			ath10k_warn(ar, "Unknown FW IE: %u\n",
1107 				    le32_to_cpu(hdr->id));
1108 			break;
1109 		}
1110 
1111 		/* jump over the padding */
1112 		ie_len = ALIGN(ie_len, 4);
1113 
1114 		len -= ie_len;
1115 		data += ie_len;
1116 	}
1117 
1118 	if (!ar->firmware_data || !ar->firmware_len) {
1119 		ath10k_warn(ar, "No ATH10K_FW_IE_FW_IMAGE found from '%s/%s', skipping\n",
1120 			    ar->hw_params.fw.dir, name);
1121 		ret = -ENOMEDIUM;
1122 		goto err;
1123 	}
1124 
1125 	return 0;
1126 
1127 err:
1128 	ath10k_core_free_firmware_files(ar);
1129 	return ret;
1130 }
1131 
1132 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
1133 {
1134 	int ret;
1135 
1136 	/* calibration file is optional, don't check for any errors */
1137 	ath10k_fetch_cal_file(ar);
1138 
1139 	ar->fw_api = 5;
1140 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
1141 
1142 	ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API5_FILE);
1143 	if (ret == 0)
1144 		goto success;
1145 
1146 	ar->fw_api = 4;
1147 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
1148 
1149 	ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API4_FILE);
1150 	if (ret == 0)
1151 		goto success;
1152 
1153 	ar->fw_api = 3;
1154 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
1155 
1156 	ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API3_FILE);
1157 	if (ret == 0)
1158 		goto success;
1159 
1160 	ar->fw_api = 2;
1161 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
1162 
1163 	ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API2_FILE);
1164 	if (ret == 0)
1165 		goto success;
1166 
1167 	ar->fw_api = 1;
1168 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
1169 
1170 	ret = ath10k_core_fetch_firmware_api_1(ar);
1171 	if (ret)
1172 		return ret;
1173 
1174 success:
1175 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
1176 
1177 	return 0;
1178 }
1179 
1180 static int ath10k_download_cal_data(struct ath10k *ar)
1181 {
1182 	int ret;
1183 
1184 	ret = ath10k_download_cal_file(ar);
1185 	if (ret == 0) {
1186 		ar->cal_mode = ATH10K_CAL_MODE_FILE;
1187 		goto done;
1188 	}
1189 
1190 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1191 		   "boot did not find a calibration file, try DT next: %d\n",
1192 		   ret);
1193 
1194 	ret = ath10k_download_cal_dt(ar);
1195 	if (ret == 0) {
1196 		ar->cal_mode = ATH10K_CAL_MODE_DT;
1197 		goto done;
1198 	}
1199 
1200 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1201 		   "boot did not find DT entry, try OTP next: %d\n",
1202 		   ret);
1203 
1204 	ret = ath10k_download_and_run_otp(ar);
1205 	if (ret) {
1206 		ath10k_err(ar, "failed to run otp: %d\n", ret);
1207 		return ret;
1208 	}
1209 
1210 	ar->cal_mode = ATH10K_CAL_MODE_OTP;
1211 
1212 done:
1213 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using calibration mode %s\n",
1214 		   ath10k_cal_mode_str(ar->cal_mode));
1215 	return 0;
1216 }
1217 
1218 static int ath10k_init_uart(struct ath10k *ar)
1219 {
1220 	int ret;
1221 
1222 	/*
1223 	 * Explicitly setting UART prints to zero as target turns it on
1224 	 * based on scratch registers.
1225 	 */
1226 	ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
1227 	if (ret) {
1228 		ath10k_warn(ar, "could not disable UART prints (%d)\n", ret);
1229 		return ret;
1230 	}
1231 
1232 	if (!uart_print)
1233 		return 0;
1234 
1235 	ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, ar->hw_params.uart_pin);
1236 	if (ret) {
1237 		ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
1238 		return ret;
1239 	}
1240 
1241 	ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
1242 	if (ret) {
1243 		ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
1244 		return ret;
1245 	}
1246 
1247 	/* Set the UART baud rate to 19200. */
1248 	ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
1249 	if (ret) {
1250 		ath10k_warn(ar, "could not set the baud rate (%d)\n", ret);
1251 		return ret;
1252 	}
1253 
1254 	ath10k_info(ar, "UART prints enabled\n");
1255 	return 0;
1256 }
1257 
1258 static int ath10k_init_hw_params(struct ath10k *ar)
1259 {
1260 	const struct ath10k_hw_params *uninitialized_var(hw_params);
1261 	int i;
1262 
1263 	for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
1264 		hw_params = &ath10k_hw_params_list[i];
1265 
1266 		if (hw_params->id == ar->target_version)
1267 			break;
1268 	}
1269 
1270 	if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
1271 		ath10k_err(ar, "Unsupported hardware version: 0x%x\n",
1272 			   ar->target_version);
1273 		return -EINVAL;
1274 	}
1275 
1276 	ar->hw_params = *hw_params;
1277 
1278 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "Hardware name %s version 0x%x\n",
1279 		   ar->hw_params.name, ar->target_version);
1280 
1281 	return 0;
1282 }
1283 
1284 static void ath10k_core_restart(struct work_struct *work)
1285 {
1286 	struct ath10k *ar = container_of(work, struct ath10k, restart_work);
1287 
1288 	set_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1289 
1290 	/* Place a barrier to make sure the compiler doesn't reorder
1291 	 * CRASH_FLUSH and calling other functions.
1292 	 */
1293 	barrier();
1294 
1295 	ieee80211_stop_queues(ar->hw);
1296 	ath10k_drain_tx(ar);
1297 	complete_all(&ar->scan.started);
1298 	complete_all(&ar->scan.completed);
1299 	complete_all(&ar->scan.on_channel);
1300 	complete_all(&ar->offchan_tx_completed);
1301 	complete_all(&ar->install_key_done);
1302 	complete_all(&ar->vdev_setup_done);
1303 	complete_all(&ar->thermal.wmi_sync);
1304 	wake_up(&ar->htt.empty_tx_wq);
1305 	wake_up(&ar->wmi.tx_credits_wq);
1306 	wake_up(&ar->peer_mapping_wq);
1307 
1308 	mutex_lock(&ar->conf_mutex);
1309 
1310 	switch (ar->state) {
1311 	case ATH10K_STATE_ON:
1312 		ar->state = ATH10K_STATE_RESTARTING;
1313 		ath10k_hif_stop(ar);
1314 		ath10k_scan_finish(ar);
1315 		ieee80211_restart_hw(ar->hw);
1316 		break;
1317 	case ATH10K_STATE_OFF:
1318 		/* this can happen if driver is being unloaded
1319 		 * or if the crash happens during FW probing */
1320 		ath10k_warn(ar, "cannot restart a device that hasn't been started\n");
1321 		break;
1322 	case ATH10K_STATE_RESTARTING:
1323 		/* hw restart might be requested from multiple places */
1324 		break;
1325 	case ATH10K_STATE_RESTARTED:
1326 		ar->state = ATH10K_STATE_WEDGED;
1327 		/* fall through */
1328 	case ATH10K_STATE_WEDGED:
1329 		ath10k_warn(ar, "device is wedged, will not restart\n");
1330 		break;
1331 	case ATH10K_STATE_UTF:
1332 		ath10k_warn(ar, "firmware restart in UTF mode not supported\n");
1333 		break;
1334 	}
1335 
1336 	mutex_unlock(&ar->conf_mutex);
1337 }
1338 
1339 static int ath10k_core_init_firmware_features(struct ath10k *ar)
1340 {
1341 	if (test_bit(ATH10K_FW_FEATURE_WMI_10_2, ar->fw_features) &&
1342 	    !test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
1343 		ath10k_err(ar, "feature bits corrupted: 10.2 feature requires 10.x feature to be set as well");
1344 		return -EINVAL;
1345 	}
1346 
1347 	if (ar->wmi.op_version >= ATH10K_FW_WMI_OP_VERSION_MAX) {
1348 		ath10k_err(ar, "unsupported WMI OP version (max %d): %d\n",
1349 			   ATH10K_FW_WMI_OP_VERSION_MAX, ar->wmi.op_version);
1350 		return -EINVAL;
1351 	}
1352 
1353 	ar->wmi.rx_decap_mode = ATH10K_HW_TXRX_NATIVE_WIFI;
1354 	switch (ath10k_cryptmode_param) {
1355 	case ATH10K_CRYPT_MODE_HW:
1356 		clear_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1357 		clear_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags);
1358 		break;
1359 	case ATH10K_CRYPT_MODE_SW:
1360 		if (!test_bit(ATH10K_FW_FEATURE_RAW_MODE_SUPPORT,
1361 			      ar->fw_features)) {
1362 			ath10k_err(ar, "cryptmode > 0 requires raw mode support from firmware");
1363 			return -EINVAL;
1364 		}
1365 
1366 		set_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1367 		set_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags);
1368 		break;
1369 	default:
1370 		ath10k_info(ar, "invalid cryptmode: %d\n",
1371 			    ath10k_cryptmode_param);
1372 		return -EINVAL;
1373 	}
1374 
1375 	ar->htt.max_num_amsdu = ATH10K_HTT_MAX_NUM_AMSDU_DEFAULT;
1376 	ar->htt.max_num_ampdu = ATH10K_HTT_MAX_NUM_AMPDU_DEFAULT;
1377 
1378 	if (rawmode) {
1379 		if (!test_bit(ATH10K_FW_FEATURE_RAW_MODE_SUPPORT,
1380 			      ar->fw_features)) {
1381 			ath10k_err(ar, "rawmode = 1 requires support from firmware");
1382 			return -EINVAL;
1383 		}
1384 		set_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1385 	}
1386 
1387 	if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
1388 		ar->wmi.rx_decap_mode = ATH10K_HW_TXRX_RAW;
1389 
1390 		/* Workaround:
1391 		 *
1392 		 * Firmware A-MSDU aggregation breaks with RAW Tx encap mode
1393 		 * and causes enormous performance issues (malformed frames,
1394 		 * etc).
1395 		 *
1396 		 * Disabling A-MSDU makes RAW mode stable with heavy traffic
1397 		 * albeit a bit slower compared to regular operation.
1398 		 */
1399 		ar->htt.max_num_amsdu = 1;
1400 	}
1401 
1402 	/* Backwards compatibility for firmwares without
1403 	 * ATH10K_FW_IE_WMI_OP_VERSION.
1404 	 */
1405 	if (ar->wmi.op_version == ATH10K_FW_WMI_OP_VERSION_UNSET) {
1406 		if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
1407 			if (test_bit(ATH10K_FW_FEATURE_WMI_10_2,
1408 				     ar->fw_features))
1409 				ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_10_2;
1410 			else
1411 				ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_10_1;
1412 		} else {
1413 			ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_MAIN;
1414 		}
1415 	}
1416 
1417 	switch (ar->wmi.op_version) {
1418 	case ATH10K_FW_WMI_OP_VERSION_MAIN:
1419 		ar->max_num_peers = TARGET_NUM_PEERS;
1420 		ar->max_num_stations = TARGET_NUM_STATIONS;
1421 		ar->max_num_vdevs = TARGET_NUM_VDEVS;
1422 		ar->htt.max_num_pending_tx = TARGET_NUM_MSDU_DESC;
1423 		ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1424 			WMI_STAT_PEER;
1425 		ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1426 		break;
1427 	case ATH10K_FW_WMI_OP_VERSION_10_1:
1428 	case ATH10K_FW_WMI_OP_VERSION_10_2:
1429 	case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1430 		ar->max_num_peers = TARGET_10X_NUM_PEERS;
1431 		ar->max_num_stations = TARGET_10X_NUM_STATIONS;
1432 		ar->max_num_vdevs = TARGET_10X_NUM_VDEVS;
1433 		ar->htt.max_num_pending_tx = TARGET_10X_NUM_MSDU_DESC;
1434 		ar->fw_stats_req_mask = WMI_STAT_PEER;
1435 		ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1436 		break;
1437 	case ATH10K_FW_WMI_OP_VERSION_TLV:
1438 		ar->max_num_peers = TARGET_TLV_NUM_PEERS;
1439 		ar->max_num_stations = TARGET_TLV_NUM_STATIONS;
1440 		ar->max_num_vdevs = TARGET_TLV_NUM_VDEVS;
1441 		ar->max_num_tdls_vdevs = TARGET_TLV_NUM_TDLS_VDEVS;
1442 		ar->htt.max_num_pending_tx = TARGET_TLV_NUM_MSDU_DESC;
1443 		ar->wow.max_num_patterns = TARGET_TLV_NUM_WOW_PATTERNS;
1444 		ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1445 			WMI_STAT_PEER;
1446 		ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1447 		break;
1448 	case ATH10K_FW_WMI_OP_VERSION_10_4:
1449 		ar->max_num_peers = TARGET_10_4_NUM_PEERS;
1450 		ar->max_num_stations = TARGET_10_4_NUM_STATIONS;
1451 		ar->num_active_peers = TARGET_10_4_ACTIVE_PEERS;
1452 		ar->max_num_vdevs = TARGET_10_4_NUM_VDEVS;
1453 		ar->num_tids = TARGET_10_4_TGT_NUM_TIDS;
1454 		ar->htt.max_num_pending_tx = TARGET_10_4_NUM_MSDU_DESC;
1455 		ar->fw_stats_req_mask = WMI_STAT_PEER;
1456 		ar->max_spatial_stream = WMI_10_4_MAX_SPATIAL_STREAM;
1457 		break;
1458 	case ATH10K_FW_WMI_OP_VERSION_UNSET:
1459 	case ATH10K_FW_WMI_OP_VERSION_MAX:
1460 		WARN_ON(1);
1461 		return -EINVAL;
1462 	}
1463 
1464 	/* Backwards compatibility for firmwares without
1465 	 * ATH10K_FW_IE_HTT_OP_VERSION.
1466 	 */
1467 	if (ar->htt.op_version == ATH10K_FW_HTT_OP_VERSION_UNSET) {
1468 		switch (ar->wmi.op_version) {
1469 		case ATH10K_FW_WMI_OP_VERSION_MAIN:
1470 			ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_MAIN;
1471 			break;
1472 		case ATH10K_FW_WMI_OP_VERSION_10_1:
1473 		case ATH10K_FW_WMI_OP_VERSION_10_2:
1474 		case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1475 			ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_10_1;
1476 			break;
1477 		case ATH10K_FW_WMI_OP_VERSION_TLV:
1478 			ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_TLV;
1479 			break;
1480 		case ATH10K_FW_WMI_OP_VERSION_10_4:
1481 		case ATH10K_FW_WMI_OP_VERSION_UNSET:
1482 		case ATH10K_FW_WMI_OP_VERSION_MAX:
1483 			WARN_ON(1);
1484 			return -EINVAL;
1485 		}
1486 	}
1487 
1488 	return 0;
1489 }
1490 
1491 int ath10k_core_start(struct ath10k *ar, enum ath10k_firmware_mode mode)
1492 {
1493 	int status;
1494 
1495 	lockdep_assert_held(&ar->conf_mutex);
1496 
1497 	clear_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1498 
1499 	ath10k_bmi_start(ar);
1500 
1501 	if (ath10k_init_configure_target(ar)) {
1502 		status = -EINVAL;
1503 		goto err;
1504 	}
1505 
1506 	status = ath10k_download_cal_data(ar);
1507 	if (status)
1508 		goto err;
1509 
1510 	/* Some of of qca988x solutions are having global reset issue
1511 	 * during target initialization. Bypassing PLL setting before
1512 	 * downloading firmware and letting the SoC run on REF_CLK is
1513 	 * fixing the problem. Corresponding firmware change is also needed
1514 	 * to set the clock source once the target is initialized.
1515 	 */
1516 	if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT,
1517 		     ar->fw_features)) {
1518 		status = ath10k_bmi_write32(ar, hi_skip_clock_init, 1);
1519 		if (status) {
1520 			ath10k_err(ar, "could not write to skip_clock_init: %d\n",
1521 				   status);
1522 			goto err;
1523 		}
1524 	}
1525 
1526 	status = ath10k_download_fw(ar, mode);
1527 	if (status)
1528 		goto err;
1529 
1530 	status = ath10k_init_uart(ar);
1531 	if (status)
1532 		goto err;
1533 
1534 	ar->htc.htc_ops.target_send_suspend_complete =
1535 		ath10k_send_suspend_complete;
1536 
1537 	status = ath10k_htc_init(ar);
1538 	if (status) {
1539 		ath10k_err(ar, "could not init HTC (%d)\n", status);
1540 		goto err;
1541 	}
1542 
1543 	status = ath10k_bmi_done(ar);
1544 	if (status)
1545 		goto err;
1546 
1547 	status = ath10k_wmi_attach(ar);
1548 	if (status) {
1549 		ath10k_err(ar, "WMI attach failed: %d\n", status);
1550 		goto err;
1551 	}
1552 
1553 	status = ath10k_htt_init(ar);
1554 	if (status) {
1555 		ath10k_err(ar, "failed to init htt: %d\n", status);
1556 		goto err_wmi_detach;
1557 	}
1558 
1559 	status = ath10k_htt_tx_alloc(&ar->htt);
1560 	if (status) {
1561 		ath10k_err(ar, "failed to alloc htt tx: %d\n", status);
1562 		goto err_wmi_detach;
1563 	}
1564 
1565 	status = ath10k_htt_rx_alloc(&ar->htt);
1566 	if (status) {
1567 		ath10k_err(ar, "failed to alloc htt rx: %d\n", status);
1568 		goto err_htt_tx_detach;
1569 	}
1570 
1571 	status = ath10k_hif_start(ar);
1572 	if (status) {
1573 		ath10k_err(ar, "could not start HIF: %d\n", status);
1574 		goto err_htt_rx_detach;
1575 	}
1576 
1577 	status = ath10k_htc_wait_target(&ar->htc);
1578 	if (status) {
1579 		ath10k_err(ar, "failed to connect to HTC: %d\n", status);
1580 		goto err_hif_stop;
1581 	}
1582 
1583 	if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1584 		status = ath10k_htt_connect(&ar->htt);
1585 		if (status) {
1586 			ath10k_err(ar, "failed to connect htt (%d)\n", status);
1587 			goto err_hif_stop;
1588 		}
1589 	}
1590 
1591 	status = ath10k_wmi_connect(ar);
1592 	if (status) {
1593 		ath10k_err(ar, "could not connect wmi: %d\n", status);
1594 		goto err_hif_stop;
1595 	}
1596 
1597 	status = ath10k_htc_start(&ar->htc);
1598 	if (status) {
1599 		ath10k_err(ar, "failed to start htc: %d\n", status);
1600 		goto err_hif_stop;
1601 	}
1602 
1603 	if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1604 		status = ath10k_wmi_wait_for_service_ready(ar);
1605 		if (status) {
1606 			ath10k_warn(ar, "wmi service ready event not received");
1607 			goto err_hif_stop;
1608 		}
1609 	}
1610 
1611 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "firmware %s booted\n",
1612 		   ar->hw->wiphy->fw_version);
1613 
1614 	status = ath10k_wmi_cmd_init(ar);
1615 	if (status) {
1616 		ath10k_err(ar, "could not send WMI init command (%d)\n",
1617 			   status);
1618 		goto err_hif_stop;
1619 	}
1620 
1621 	status = ath10k_wmi_wait_for_unified_ready(ar);
1622 	if (status) {
1623 		ath10k_err(ar, "wmi unified ready event not received\n");
1624 		goto err_hif_stop;
1625 	}
1626 
1627 	/* If firmware indicates Full Rx Reorder support it must be used in a
1628 	 * slightly different manner. Let HTT code know.
1629 	 */
1630 	ar->htt.rx_ring.in_ord_rx = !!(test_bit(WMI_SERVICE_RX_FULL_REORDER,
1631 						ar->wmi.svc_map));
1632 
1633 	status = ath10k_htt_rx_ring_refill(ar);
1634 	if (status) {
1635 		ath10k_err(ar, "failed to refill htt rx ring: %d\n", status);
1636 		goto err_hif_stop;
1637 	}
1638 
1639 	/* we don't care about HTT in UTF mode */
1640 	if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1641 		status = ath10k_htt_setup(&ar->htt);
1642 		if (status) {
1643 			ath10k_err(ar, "failed to setup htt: %d\n", status);
1644 			goto err_hif_stop;
1645 		}
1646 	}
1647 
1648 	status = ath10k_debug_start(ar);
1649 	if (status)
1650 		goto err_hif_stop;
1651 
1652 	ar->free_vdev_map = (1LL << ar->max_num_vdevs) - 1;
1653 
1654 	INIT_LIST_HEAD(&ar->arvifs);
1655 
1656 	return 0;
1657 
1658 err_hif_stop:
1659 	ath10k_hif_stop(ar);
1660 err_htt_rx_detach:
1661 	ath10k_htt_rx_free(&ar->htt);
1662 err_htt_tx_detach:
1663 	ath10k_htt_tx_free(&ar->htt);
1664 err_wmi_detach:
1665 	ath10k_wmi_detach(ar);
1666 err:
1667 	return status;
1668 }
1669 EXPORT_SYMBOL(ath10k_core_start);
1670 
1671 int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt)
1672 {
1673 	int ret;
1674 	unsigned long time_left;
1675 
1676 	reinit_completion(&ar->target_suspend);
1677 
1678 	ret = ath10k_wmi_pdev_suspend_target(ar, suspend_opt);
1679 	if (ret) {
1680 		ath10k_warn(ar, "could not suspend target (%d)\n", ret);
1681 		return ret;
1682 	}
1683 
1684 	time_left = wait_for_completion_timeout(&ar->target_suspend, 1 * HZ);
1685 
1686 	if (!time_left) {
1687 		ath10k_warn(ar, "suspend timed out - target pause event never came\n");
1688 		return -ETIMEDOUT;
1689 	}
1690 
1691 	return 0;
1692 }
1693 
1694 void ath10k_core_stop(struct ath10k *ar)
1695 {
1696 	lockdep_assert_held(&ar->conf_mutex);
1697 	ath10k_debug_stop(ar);
1698 
1699 	/* try to suspend target */
1700 	if (ar->state != ATH10K_STATE_RESTARTING &&
1701 	    ar->state != ATH10K_STATE_UTF)
1702 		ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND_AND_DISABLE_INTR);
1703 
1704 	ath10k_hif_stop(ar);
1705 	ath10k_htt_tx_free(&ar->htt);
1706 	ath10k_htt_rx_free(&ar->htt);
1707 	ath10k_wmi_detach(ar);
1708 }
1709 EXPORT_SYMBOL(ath10k_core_stop);
1710 
1711 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
1712  * order to know what hw capabilities should be advertised to mac80211 it is
1713  * necessary to load the firmware (and tear it down immediately since start
1714  * hook will try to init it again) before registering */
1715 static int ath10k_core_probe_fw(struct ath10k *ar)
1716 {
1717 	struct bmi_target_info target_info;
1718 	int ret = 0;
1719 
1720 	ret = ath10k_hif_power_up(ar);
1721 	if (ret) {
1722 		ath10k_err(ar, "could not start pci hif (%d)\n", ret);
1723 		return ret;
1724 	}
1725 
1726 	memset(&target_info, 0, sizeof(target_info));
1727 	ret = ath10k_bmi_get_target_info(ar, &target_info);
1728 	if (ret) {
1729 		ath10k_err(ar, "could not get target info (%d)\n", ret);
1730 		goto err_power_down;
1731 	}
1732 
1733 	ar->target_version = target_info.version;
1734 	ar->hw->wiphy->hw_version = target_info.version;
1735 
1736 	ret = ath10k_init_hw_params(ar);
1737 	if (ret) {
1738 		ath10k_err(ar, "could not get hw params (%d)\n", ret);
1739 		goto err_power_down;
1740 	}
1741 
1742 	ret = ath10k_core_fetch_firmware_files(ar);
1743 	if (ret) {
1744 		ath10k_err(ar, "could not fetch firmware files (%d)\n", ret);
1745 		goto err_power_down;
1746 	}
1747 
1748 	ret = ath10k_core_get_board_id_from_otp(ar);
1749 	if (ret && ret != -EOPNOTSUPP) {
1750 		ath10k_err(ar, "failed to get board id from otp for qca99x0: %d\n",
1751 			   ret);
1752 		return ret;
1753 	}
1754 
1755 	ret = ath10k_core_fetch_board_file(ar);
1756 	if (ret) {
1757 		ath10k_err(ar, "failed to fetch board file: %d\n", ret);
1758 		goto err_free_firmware_files;
1759 	}
1760 
1761 	ret = ath10k_core_init_firmware_features(ar);
1762 	if (ret) {
1763 		ath10k_err(ar, "fatal problem with firmware features: %d\n",
1764 			   ret);
1765 		goto err_free_firmware_files;
1766 	}
1767 
1768 	ret = ath10k_swap_code_seg_init(ar);
1769 	if (ret) {
1770 		ath10k_err(ar, "failed to initialize code swap segment: %d\n",
1771 			   ret);
1772 		goto err_free_firmware_files;
1773 	}
1774 
1775 	mutex_lock(&ar->conf_mutex);
1776 
1777 	ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
1778 	if (ret) {
1779 		ath10k_err(ar, "could not init core (%d)\n", ret);
1780 		goto err_unlock;
1781 	}
1782 
1783 	ath10k_print_driver_info(ar);
1784 	ath10k_core_stop(ar);
1785 
1786 	mutex_unlock(&ar->conf_mutex);
1787 
1788 	ath10k_hif_power_down(ar);
1789 	return 0;
1790 
1791 err_unlock:
1792 	mutex_unlock(&ar->conf_mutex);
1793 
1794 err_free_firmware_files:
1795 	ath10k_core_free_firmware_files(ar);
1796 
1797 err_power_down:
1798 	ath10k_hif_power_down(ar);
1799 
1800 	return ret;
1801 }
1802 
1803 static void ath10k_core_register_work(struct work_struct *work)
1804 {
1805 	struct ath10k *ar = container_of(work, struct ath10k, register_work);
1806 	int status;
1807 
1808 	status = ath10k_core_probe_fw(ar);
1809 	if (status) {
1810 		ath10k_err(ar, "could not probe fw (%d)\n", status);
1811 		goto err;
1812 	}
1813 
1814 	status = ath10k_mac_register(ar);
1815 	if (status) {
1816 		ath10k_err(ar, "could not register to mac80211 (%d)\n", status);
1817 		goto err_release_fw;
1818 	}
1819 
1820 	status = ath10k_debug_register(ar);
1821 	if (status) {
1822 		ath10k_err(ar, "unable to initialize debugfs\n");
1823 		goto err_unregister_mac;
1824 	}
1825 
1826 	status = ath10k_spectral_create(ar);
1827 	if (status) {
1828 		ath10k_err(ar, "failed to initialize spectral\n");
1829 		goto err_debug_destroy;
1830 	}
1831 
1832 	status = ath10k_thermal_register(ar);
1833 	if (status) {
1834 		ath10k_err(ar, "could not register thermal device: %d\n",
1835 			   status);
1836 		goto err_spectral_destroy;
1837 	}
1838 
1839 	set_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags);
1840 	return;
1841 
1842 err_spectral_destroy:
1843 	ath10k_spectral_destroy(ar);
1844 err_debug_destroy:
1845 	ath10k_debug_destroy(ar);
1846 err_unregister_mac:
1847 	ath10k_mac_unregister(ar);
1848 err_release_fw:
1849 	ath10k_core_free_firmware_files(ar);
1850 err:
1851 	/* TODO: It's probably a good idea to release device from the driver
1852 	 * but calling device_release_driver() here will cause a deadlock.
1853 	 */
1854 	return;
1855 }
1856 
1857 int ath10k_core_register(struct ath10k *ar, u32 chip_id)
1858 {
1859 	ar->chip_id = chip_id;
1860 	queue_work(ar->workqueue, &ar->register_work);
1861 
1862 	return 0;
1863 }
1864 EXPORT_SYMBOL(ath10k_core_register);
1865 
1866 void ath10k_core_unregister(struct ath10k *ar)
1867 {
1868 	cancel_work_sync(&ar->register_work);
1869 
1870 	if (!test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags))
1871 		return;
1872 
1873 	ath10k_thermal_unregister(ar);
1874 	/* Stop spectral before unregistering from mac80211 to remove the
1875 	 * relayfs debugfs file cleanly. Otherwise the parent debugfs tree
1876 	 * would be already be free'd recursively, leading to a double free.
1877 	 */
1878 	ath10k_spectral_destroy(ar);
1879 
1880 	/* We must unregister from mac80211 before we stop HTC and HIF.
1881 	 * Otherwise we will fail to submit commands to FW and mac80211 will be
1882 	 * unhappy about callback failures. */
1883 	ath10k_mac_unregister(ar);
1884 
1885 	ath10k_testmode_destroy(ar);
1886 
1887 	ath10k_core_free_firmware_files(ar);
1888 	ath10k_core_free_board_files(ar);
1889 
1890 	ath10k_debug_unregister(ar);
1891 }
1892 EXPORT_SYMBOL(ath10k_core_unregister);
1893 
1894 struct ath10k *ath10k_core_create(size_t priv_size, struct device *dev,
1895 				  enum ath10k_bus bus,
1896 				  enum ath10k_hw_rev hw_rev,
1897 				  const struct ath10k_hif_ops *hif_ops)
1898 {
1899 	struct ath10k *ar;
1900 	int ret;
1901 
1902 	ar = ath10k_mac_create(priv_size);
1903 	if (!ar)
1904 		return NULL;
1905 
1906 	ar->ath_common.priv = ar;
1907 	ar->ath_common.hw = ar->hw;
1908 	ar->dev = dev;
1909 	ar->hw_rev = hw_rev;
1910 	ar->hif.ops = hif_ops;
1911 	ar->hif.bus = bus;
1912 
1913 	switch (hw_rev) {
1914 	case ATH10K_HW_QCA988X:
1915 		ar->regs = &qca988x_regs;
1916 		ar->hw_values = &qca988x_values;
1917 		break;
1918 	case ATH10K_HW_QCA6174:
1919 	case ATH10K_HW_QCA9377:
1920 		ar->regs = &qca6174_regs;
1921 		ar->hw_values = &qca6174_values;
1922 		break;
1923 	case ATH10K_HW_QCA99X0:
1924 		ar->regs = &qca99x0_regs;
1925 		ar->hw_values = &qca99x0_values;
1926 		break;
1927 	default:
1928 		ath10k_err(ar, "unsupported core hardware revision %d\n",
1929 			   hw_rev);
1930 		ret = -ENOTSUPP;
1931 		goto err_free_mac;
1932 	}
1933 
1934 	init_completion(&ar->scan.started);
1935 	init_completion(&ar->scan.completed);
1936 	init_completion(&ar->scan.on_channel);
1937 	init_completion(&ar->target_suspend);
1938 	init_completion(&ar->wow.wakeup_completed);
1939 
1940 	init_completion(&ar->install_key_done);
1941 	init_completion(&ar->vdev_setup_done);
1942 	init_completion(&ar->thermal.wmi_sync);
1943 
1944 	INIT_DELAYED_WORK(&ar->scan.timeout, ath10k_scan_timeout_work);
1945 
1946 	ar->workqueue = create_singlethread_workqueue("ath10k_wq");
1947 	if (!ar->workqueue)
1948 		goto err_free_mac;
1949 
1950 	ar->workqueue_aux = create_singlethread_workqueue("ath10k_aux_wq");
1951 	if (!ar->workqueue_aux)
1952 		goto err_free_wq;
1953 
1954 	mutex_init(&ar->conf_mutex);
1955 	spin_lock_init(&ar->data_lock);
1956 
1957 	INIT_LIST_HEAD(&ar->peers);
1958 	init_waitqueue_head(&ar->peer_mapping_wq);
1959 	init_waitqueue_head(&ar->htt.empty_tx_wq);
1960 	init_waitqueue_head(&ar->wmi.tx_credits_wq);
1961 
1962 	init_completion(&ar->offchan_tx_completed);
1963 	INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
1964 	skb_queue_head_init(&ar->offchan_tx_queue);
1965 
1966 	INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
1967 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
1968 
1969 	INIT_WORK(&ar->register_work, ath10k_core_register_work);
1970 	INIT_WORK(&ar->restart_work, ath10k_core_restart);
1971 
1972 	ret = ath10k_debug_create(ar);
1973 	if (ret)
1974 		goto err_free_aux_wq;
1975 
1976 	return ar;
1977 
1978 err_free_aux_wq:
1979 	destroy_workqueue(ar->workqueue_aux);
1980 err_free_wq:
1981 	destroy_workqueue(ar->workqueue);
1982 
1983 err_free_mac:
1984 	ath10k_mac_destroy(ar);
1985 
1986 	return NULL;
1987 }
1988 EXPORT_SYMBOL(ath10k_core_create);
1989 
1990 void ath10k_core_destroy(struct ath10k *ar)
1991 {
1992 	flush_workqueue(ar->workqueue);
1993 	destroy_workqueue(ar->workqueue);
1994 
1995 	flush_workqueue(ar->workqueue_aux);
1996 	destroy_workqueue(ar->workqueue_aux);
1997 
1998 	ath10k_debug_destroy(ar);
1999 	ath10k_wmi_free_host_mem(ar);
2000 	ath10k_mac_destroy(ar);
2001 }
2002 EXPORT_SYMBOL(ath10k_core_destroy);
2003 
2004 MODULE_AUTHOR("Qualcomm Atheros");
2005 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
2006 MODULE_LICENSE("Dual BSD/GPL");
2007