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