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