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 
21 #include "core.h"
22 #include "mac.h"
23 #include "htc.h"
24 #include "hif.h"
25 #include "wmi.h"
26 #include "bmi.h"
27 #include "debug.h"
28 #include "htt.h"
29 #include "testmode.h"
30 
31 unsigned int ath10k_debug_mask;
32 static bool uart_print;
33 static unsigned int ath10k_p2p;
34 static bool skip_otp;
35 
36 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
37 module_param(uart_print, bool, 0644);
38 module_param_named(p2p, ath10k_p2p, uint, 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(p2p, "Enable ath10k P2P support");
44 MODULE_PARM_DESC(skip_otp, "Skip otp failure for calibration in testmode");
45 
46 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
47 	{
48 		.id = QCA988X_HW_2_0_VERSION,
49 		.name = "qca988x hw2.0",
50 		.patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
51 		.fw = {
52 			.dir = QCA988X_HW_2_0_FW_DIR,
53 			.fw = QCA988X_HW_2_0_FW_FILE,
54 			.otp = QCA988X_HW_2_0_OTP_FILE,
55 			.board = QCA988X_HW_2_0_BOARD_DATA_FILE,
56 		},
57 	},
58 };
59 
60 static void ath10k_send_suspend_complete(struct ath10k *ar)
61 {
62 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot suspend complete\n");
63 
64 	complete(&ar->target_suspend);
65 }
66 
67 static int ath10k_init_configure_target(struct ath10k *ar)
68 {
69 	u32 param_host;
70 	int ret;
71 
72 	/* tell target which HTC version it is used*/
73 	ret = ath10k_bmi_write32(ar, hi_app_host_interest,
74 				 HTC_PROTOCOL_VERSION);
75 	if (ret) {
76 		ath10k_err(ar, "settings HTC version failed\n");
77 		return ret;
78 	}
79 
80 	/* set the firmware mode to STA/IBSS/AP */
81 	ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
82 	if (ret) {
83 		ath10k_err(ar, "setting firmware mode (1/2) failed\n");
84 		return ret;
85 	}
86 
87 	/* TODO following parameters need to be re-visited. */
88 	/* num_device */
89 	param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
90 	/* Firmware mode */
91 	/* FIXME: Why FW_MODE_AP ??.*/
92 	param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
93 	/* mac_addr_method */
94 	param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
95 	/* firmware_bridge */
96 	param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
97 	/* fwsubmode */
98 	param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
99 
100 	ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
101 	if (ret) {
102 		ath10k_err(ar, "setting firmware mode (2/2) failed\n");
103 		return ret;
104 	}
105 
106 	/* We do all byte-swapping on the host */
107 	ret = ath10k_bmi_write32(ar, hi_be, 0);
108 	if (ret) {
109 		ath10k_err(ar, "setting host CPU BE mode failed\n");
110 		return ret;
111 	}
112 
113 	/* FW descriptor/Data swap flags */
114 	ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
115 
116 	if (ret) {
117 		ath10k_err(ar, "setting FW data/desc swap flags failed\n");
118 		return ret;
119 	}
120 
121 	return 0;
122 }
123 
124 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
125 						   const char *dir,
126 						   const char *file)
127 {
128 	char filename[100];
129 	const struct firmware *fw;
130 	int ret;
131 
132 	if (file == NULL)
133 		return ERR_PTR(-ENOENT);
134 
135 	if (dir == NULL)
136 		dir = ".";
137 
138 	snprintf(filename, sizeof(filename), "%s/%s", dir, file);
139 	ret = request_firmware(&fw, filename, ar->dev);
140 	if (ret)
141 		return ERR_PTR(ret);
142 
143 	return fw;
144 }
145 
146 static int ath10k_push_board_ext_data(struct ath10k *ar, const void *data,
147 				      size_t data_len)
148 {
149 	u32 board_data_size = QCA988X_BOARD_DATA_SZ;
150 	u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
151 	u32 board_ext_data_addr;
152 	int ret;
153 
154 	ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
155 	if (ret) {
156 		ath10k_err(ar, "could not read board ext data addr (%d)\n",
157 			   ret);
158 		return ret;
159 	}
160 
161 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
162 		   "boot push board extended data addr 0x%x\n",
163 		   board_ext_data_addr);
164 
165 	if (board_ext_data_addr == 0)
166 		return 0;
167 
168 	if (data_len != (board_data_size + board_ext_data_size)) {
169 		ath10k_err(ar, "invalid board (ext) data sizes %zu != %d+%d\n",
170 			   data_len, board_data_size, board_ext_data_size);
171 		return -EINVAL;
172 	}
173 
174 	ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
175 				      data + board_data_size,
176 				      board_ext_data_size);
177 	if (ret) {
178 		ath10k_err(ar, "could not write board ext data (%d)\n", ret);
179 		return ret;
180 	}
181 
182 	ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
183 				 (board_ext_data_size << 16) | 1);
184 	if (ret) {
185 		ath10k_err(ar, "could not write board ext data bit (%d)\n",
186 			   ret);
187 		return ret;
188 	}
189 
190 	return 0;
191 }
192 
193 static int ath10k_download_board_data(struct ath10k *ar, const void *data,
194 				      size_t data_len)
195 {
196 	u32 board_data_size = QCA988X_BOARD_DATA_SZ;
197 	u32 address;
198 	int ret;
199 
200 	ret = ath10k_push_board_ext_data(ar, data, data_len);
201 	if (ret) {
202 		ath10k_err(ar, "could not push board ext data (%d)\n", ret);
203 		goto exit;
204 	}
205 
206 	ret = ath10k_bmi_read32(ar, hi_board_data, &address);
207 	if (ret) {
208 		ath10k_err(ar, "could not read board data addr (%d)\n", ret);
209 		goto exit;
210 	}
211 
212 	ret = ath10k_bmi_write_memory(ar, address, data,
213 				      min_t(u32, board_data_size,
214 					    data_len));
215 	if (ret) {
216 		ath10k_err(ar, "could not write board data (%d)\n", ret);
217 		goto exit;
218 	}
219 
220 	ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
221 	if (ret) {
222 		ath10k_err(ar, "could not write board data bit (%d)\n", ret);
223 		goto exit;
224 	}
225 
226 exit:
227 	return ret;
228 }
229 
230 static int ath10k_download_cal_file(struct ath10k *ar)
231 {
232 	int ret;
233 
234 	if (!ar->cal_file)
235 		return -ENOENT;
236 
237 	if (IS_ERR(ar->cal_file))
238 		return PTR_ERR(ar->cal_file);
239 
240 	ret = ath10k_download_board_data(ar, ar->cal_file->data,
241 					 ar->cal_file->size);
242 	if (ret) {
243 		ath10k_err(ar, "failed to download cal_file data: %d\n", ret);
244 		return ret;
245 	}
246 
247 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cal file downloaded\n");
248 
249 	return 0;
250 }
251 
252 static int ath10k_download_and_run_otp(struct ath10k *ar)
253 {
254 	u32 result, address = ar->hw_params.patch_load_addr;
255 	int ret;
256 
257 	ret = ath10k_download_board_data(ar, ar->board_data, ar->board_len);
258 	if (ret) {
259 		ath10k_err(ar, "failed to download board data: %d\n", ret);
260 		return ret;
261 	}
262 
263 	/* OTP is optional */
264 
265 	if (!ar->otp_data || !ar->otp_len) {
266 		ath10k_warn(ar, "Not running otp, calibration will be incorrect (otp-data %p otp_len %zd)!\n",
267 			    ar->otp_data, ar->otp_len);
268 		return 0;
269 	}
270 
271 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot upload otp to 0x%x len %zd\n",
272 		   address, ar->otp_len);
273 
274 	ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
275 	if (ret) {
276 		ath10k_err(ar, "could not write otp (%d)\n", ret);
277 		return ret;
278 	}
279 
280 	ret = ath10k_bmi_execute(ar, address, 0, &result);
281 	if (ret) {
282 		ath10k_err(ar, "could not execute otp (%d)\n", ret);
283 		return ret;
284 	}
285 
286 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot otp execute result %d\n", result);
287 
288 	if (!skip_otp && result != 0) {
289 		ath10k_err(ar, "otp calibration failed: %d", result);
290 		return -EINVAL;
291 	}
292 
293 	return 0;
294 }
295 
296 static int ath10k_download_fw(struct ath10k *ar, enum ath10k_firmware_mode mode)
297 {
298 	u32 address, data_len;
299 	const char *mode_name;
300 	const void *data;
301 	int ret;
302 
303 	address = ar->hw_params.patch_load_addr;
304 
305 	switch (mode) {
306 	case ATH10K_FIRMWARE_MODE_NORMAL:
307 		data = ar->firmware_data;
308 		data_len = ar->firmware_len;
309 		mode_name = "normal";
310 		break;
311 	case ATH10K_FIRMWARE_MODE_UTF:
312 		data = ar->testmode.utf->data;
313 		data_len = ar->testmode.utf->size;
314 		mode_name = "utf";
315 		break;
316 	default:
317 		ath10k_err(ar, "unknown firmware mode: %d\n", mode);
318 		return -EINVAL;
319 	}
320 
321 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
322 		   "boot uploading firmware image %p len %d mode %s\n",
323 		   data, data_len, mode_name);
324 
325 	ret = ath10k_bmi_fast_download(ar, address, data, data_len);
326 	if (ret) {
327 		ath10k_err(ar, "failed to download %s firmware: %d\n",
328 			   mode_name, ret);
329 		return ret;
330 	}
331 
332 	return ret;
333 }
334 
335 static void ath10k_core_free_firmware_files(struct ath10k *ar)
336 {
337 	if (ar->board && !IS_ERR(ar->board))
338 		release_firmware(ar->board);
339 
340 	if (ar->otp && !IS_ERR(ar->otp))
341 		release_firmware(ar->otp);
342 
343 	if (ar->firmware && !IS_ERR(ar->firmware))
344 		release_firmware(ar->firmware);
345 
346 	if (ar->cal_file && !IS_ERR(ar->cal_file))
347 		release_firmware(ar->cal_file);
348 
349 	ar->board = NULL;
350 	ar->board_data = NULL;
351 	ar->board_len = 0;
352 
353 	ar->otp = NULL;
354 	ar->otp_data = NULL;
355 	ar->otp_len = 0;
356 
357 	ar->firmware = NULL;
358 	ar->firmware_data = NULL;
359 	ar->firmware_len = 0;
360 
361 	ar->cal_file = NULL;
362 }
363 
364 static int ath10k_fetch_cal_file(struct ath10k *ar)
365 {
366 	char filename[100];
367 
368 	/* cal-<bus>-<id>.bin */
369 	scnprintf(filename, sizeof(filename), "cal-%s-%s.bin",
370 		  ath10k_bus_str(ar->hif.bus), dev_name(ar->dev));
371 
372 	ar->cal_file = ath10k_fetch_fw_file(ar, ATH10K_FW_DIR, filename);
373 	if (IS_ERR(ar->cal_file))
374 		/* calibration file is optional, don't print any warnings */
375 		return PTR_ERR(ar->cal_file);
376 
377 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "found calibration file %s/%s\n",
378 		   ATH10K_FW_DIR, filename);
379 
380 	return 0;
381 }
382 
383 static int ath10k_core_fetch_firmware_api_1(struct ath10k *ar)
384 {
385 	int ret = 0;
386 
387 	if (ar->hw_params.fw.fw == NULL) {
388 		ath10k_err(ar, "firmware file not defined\n");
389 		return -EINVAL;
390 	}
391 
392 	if (ar->hw_params.fw.board == NULL) {
393 		ath10k_err(ar, "board data file not defined");
394 		return -EINVAL;
395 	}
396 
397 	ar->board = ath10k_fetch_fw_file(ar,
398 					 ar->hw_params.fw.dir,
399 					 ar->hw_params.fw.board);
400 	if (IS_ERR(ar->board)) {
401 		ret = PTR_ERR(ar->board);
402 		ath10k_err(ar, "could not fetch board data (%d)\n", ret);
403 		goto err;
404 	}
405 
406 	ar->board_data = ar->board->data;
407 	ar->board_len = ar->board->size;
408 
409 	ar->firmware = ath10k_fetch_fw_file(ar,
410 					    ar->hw_params.fw.dir,
411 					    ar->hw_params.fw.fw);
412 	if (IS_ERR(ar->firmware)) {
413 		ret = PTR_ERR(ar->firmware);
414 		ath10k_err(ar, "could not fetch firmware (%d)\n", ret);
415 		goto err;
416 	}
417 
418 	ar->firmware_data = ar->firmware->data;
419 	ar->firmware_len = ar->firmware->size;
420 
421 	/* OTP may be undefined. If so, don't fetch it at all */
422 	if (ar->hw_params.fw.otp == NULL)
423 		return 0;
424 
425 	ar->otp = ath10k_fetch_fw_file(ar,
426 				       ar->hw_params.fw.dir,
427 				       ar->hw_params.fw.otp);
428 	if (IS_ERR(ar->otp)) {
429 		ret = PTR_ERR(ar->otp);
430 		ath10k_err(ar, "could not fetch otp (%d)\n", ret);
431 		goto err;
432 	}
433 
434 	ar->otp_data = ar->otp->data;
435 	ar->otp_len = ar->otp->size;
436 
437 	return 0;
438 
439 err:
440 	ath10k_core_free_firmware_files(ar);
441 	return ret;
442 }
443 
444 static int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name)
445 {
446 	size_t magic_len, len, ie_len;
447 	int ie_id, i, index, bit, ret;
448 	struct ath10k_fw_ie *hdr;
449 	const u8 *data;
450 	__le32 *timestamp;
451 
452 	/* first fetch the firmware file (firmware-*.bin) */
453 	ar->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, name);
454 	if (IS_ERR(ar->firmware)) {
455 		ath10k_err(ar, "could not fetch firmware file '%s/%s': %ld\n",
456 			   ar->hw_params.fw.dir, name, PTR_ERR(ar->firmware));
457 		return PTR_ERR(ar->firmware);
458 	}
459 
460 	data = ar->firmware->data;
461 	len = ar->firmware->size;
462 
463 	/* magic also includes the null byte, check that as well */
464 	magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
465 
466 	if (len < magic_len) {
467 		ath10k_err(ar, "firmware file '%s/%s' too small to contain magic: %zu\n",
468 			   ar->hw_params.fw.dir, name, len);
469 		ret = -EINVAL;
470 		goto err;
471 	}
472 
473 	if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
474 		ath10k_err(ar, "invalid firmware magic\n");
475 		ret = -EINVAL;
476 		goto err;
477 	}
478 
479 	/* jump over the padding */
480 	magic_len = ALIGN(magic_len, 4);
481 
482 	len -= magic_len;
483 	data += magic_len;
484 
485 	/* loop elements */
486 	while (len > sizeof(struct ath10k_fw_ie)) {
487 		hdr = (struct ath10k_fw_ie *)data;
488 
489 		ie_id = le32_to_cpu(hdr->id);
490 		ie_len = le32_to_cpu(hdr->len);
491 
492 		len -= sizeof(*hdr);
493 		data += sizeof(*hdr);
494 
495 		if (len < ie_len) {
496 			ath10k_err(ar, "invalid length for FW IE %d (%zu < %zu)\n",
497 				   ie_id, len, ie_len);
498 			ret = -EINVAL;
499 			goto err;
500 		}
501 
502 		switch (ie_id) {
503 		case ATH10K_FW_IE_FW_VERSION:
504 			if (ie_len > sizeof(ar->hw->wiphy->fw_version) - 1)
505 				break;
506 
507 			memcpy(ar->hw->wiphy->fw_version, data, ie_len);
508 			ar->hw->wiphy->fw_version[ie_len] = '\0';
509 
510 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
511 				   "found fw version %s\n",
512 				    ar->hw->wiphy->fw_version);
513 			break;
514 		case ATH10K_FW_IE_TIMESTAMP:
515 			if (ie_len != sizeof(u32))
516 				break;
517 
518 			timestamp = (__le32 *)data;
519 
520 			ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw timestamp %d\n",
521 				   le32_to_cpup(timestamp));
522 			break;
523 		case ATH10K_FW_IE_FEATURES:
524 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
525 				   "found firmware features ie (%zd B)\n",
526 				   ie_len);
527 
528 			for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
529 				index = i / 8;
530 				bit = i % 8;
531 
532 				if (index == ie_len)
533 					break;
534 
535 				if (data[index] & (1 << bit)) {
536 					ath10k_dbg(ar, ATH10K_DBG_BOOT,
537 						   "Enabling feature bit: %i\n",
538 						   i);
539 					__set_bit(i, ar->fw_features);
540 				}
541 			}
542 
543 			ath10k_dbg_dump(ar, ATH10K_DBG_BOOT, "features", "",
544 					ar->fw_features,
545 					sizeof(ar->fw_features));
546 			break;
547 		case ATH10K_FW_IE_FW_IMAGE:
548 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
549 				   "found fw image ie (%zd B)\n",
550 				   ie_len);
551 
552 			ar->firmware_data = data;
553 			ar->firmware_len = ie_len;
554 
555 			break;
556 		case ATH10K_FW_IE_OTP_IMAGE:
557 			ath10k_dbg(ar, ATH10K_DBG_BOOT,
558 				   "found otp image ie (%zd B)\n",
559 				   ie_len);
560 
561 			ar->otp_data = data;
562 			ar->otp_len = ie_len;
563 
564 			break;
565 		default:
566 			ath10k_warn(ar, "Unknown FW IE: %u\n",
567 				    le32_to_cpu(hdr->id));
568 			break;
569 		}
570 
571 		/* jump over the padding */
572 		ie_len = ALIGN(ie_len, 4);
573 
574 		len -= ie_len;
575 		data += ie_len;
576 	}
577 
578 	if (!ar->firmware_data || !ar->firmware_len) {
579 		ath10k_warn(ar, "No ATH10K_FW_IE_FW_IMAGE found from '%s/%s', skipping\n",
580 			    ar->hw_params.fw.dir, name);
581 		ret = -ENOMEDIUM;
582 		goto err;
583 	}
584 
585 	if (test_bit(ATH10K_FW_FEATURE_WMI_10_2, ar->fw_features) &&
586 	    !test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
587 		ath10k_err(ar, "feature bits corrupted: 10.2 feature requires 10.x feature to be set as well");
588 		ret = -EINVAL;
589 		goto err;
590 	}
591 
592 	/* now fetch the board file */
593 	if (ar->hw_params.fw.board == NULL) {
594 		ath10k_err(ar, "board data file not defined");
595 		ret = -EINVAL;
596 		goto err;
597 	}
598 
599 	ar->board = ath10k_fetch_fw_file(ar,
600 					 ar->hw_params.fw.dir,
601 					 ar->hw_params.fw.board);
602 	if (IS_ERR(ar->board)) {
603 		ret = PTR_ERR(ar->board);
604 		ath10k_err(ar, "could not fetch board data '%s/%s' (%d)\n",
605 			   ar->hw_params.fw.dir, ar->hw_params.fw.board,
606 			   ret);
607 		goto err;
608 	}
609 
610 	ar->board_data = ar->board->data;
611 	ar->board_len = ar->board->size;
612 
613 	return 0;
614 
615 err:
616 	ath10k_core_free_firmware_files(ar);
617 	return ret;
618 }
619 
620 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
621 {
622 	int ret;
623 
624 	/* calibration file is optional, don't check for any errors */
625 	ath10k_fetch_cal_file(ar);
626 
627 	ar->fw_api = 3;
628 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
629 
630 	ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API3_FILE);
631 	if (ret == 0)
632 		goto success;
633 
634 	ar->fw_api = 2;
635 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
636 
637 	ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API2_FILE);
638 	if (ret == 0)
639 		goto success;
640 
641 	ar->fw_api = 1;
642 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
643 
644 	ret = ath10k_core_fetch_firmware_api_1(ar);
645 	if (ret)
646 		return ret;
647 
648 success:
649 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
650 
651 	return 0;
652 }
653 
654 static int ath10k_download_cal_data(struct ath10k *ar)
655 {
656 	int ret;
657 
658 	ret = ath10k_download_cal_file(ar);
659 	if (ret == 0) {
660 		ar->cal_mode = ATH10K_CAL_MODE_FILE;
661 		goto done;
662 	}
663 
664 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
665 		   "boot did not find a calibration file, try OTP next: %d\n",
666 		   ret);
667 
668 	ret = ath10k_download_and_run_otp(ar);
669 	if (ret) {
670 		ath10k_err(ar, "failed to run otp: %d\n", ret);
671 		return ret;
672 	}
673 
674 	ar->cal_mode = ATH10K_CAL_MODE_OTP;
675 
676 done:
677 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using calibration mode %s\n",
678 		   ath10k_cal_mode_str(ar->cal_mode));
679 	return 0;
680 }
681 
682 static int ath10k_init_uart(struct ath10k *ar)
683 {
684 	int ret;
685 
686 	/*
687 	 * Explicitly setting UART prints to zero as target turns it on
688 	 * based on scratch registers.
689 	 */
690 	ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
691 	if (ret) {
692 		ath10k_warn(ar, "could not disable UART prints (%d)\n", ret);
693 		return ret;
694 	}
695 
696 	if (!uart_print)
697 		return 0;
698 
699 	ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
700 	if (ret) {
701 		ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
702 		return ret;
703 	}
704 
705 	ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
706 	if (ret) {
707 		ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
708 		return ret;
709 	}
710 
711 	/* Set the UART baud rate to 19200. */
712 	ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
713 	if (ret) {
714 		ath10k_warn(ar, "could not set the baud rate (%d)\n", ret);
715 		return ret;
716 	}
717 
718 	ath10k_info(ar, "UART prints enabled\n");
719 	return 0;
720 }
721 
722 static int ath10k_init_hw_params(struct ath10k *ar)
723 {
724 	const struct ath10k_hw_params *uninitialized_var(hw_params);
725 	int i;
726 
727 	for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
728 		hw_params = &ath10k_hw_params_list[i];
729 
730 		if (hw_params->id == ar->target_version)
731 			break;
732 	}
733 
734 	if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
735 		ath10k_err(ar, "Unsupported hardware version: 0x%x\n",
736 			   ar->target_version);
737 		return -EINVAL;
738 	}
739 
740 	ar->hw_params = *hw_params;
741 
742 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "Hardware name %s version 0x%x\n",
743 		   ar->hw_params.name, ar->target_version);
744 
745 	return 0;
746 }
747 
748 static void ath10k_core_restart(struct work_struct *work)
749 {
750 	struct ath10k *ar = container_of(work, struct ath10k, restart_work);
751 
752 	set_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
753 
754 	/* Place a barrier to make sure the compiler doesn't reorder
755 	 * CRASH_FLUSH and calling other functions.
756 	 */
757 	barrier();
758 
759 	ieee80211_stop_queues(ar->hw);
760 	ath10k_drain_tx(ar);
761 	complete_all(&ar->scan.started);
762 	complete_all(&ar->scan.completed);
763 	complete_all(&ar->scan.on_channel);
764 	complete_all(&ar->offchan_tx_completed);
765 	complete_all(&ar->install_key_done);
766 	complete_all(&ar->vdev_setup_done);
767 	wake_up(&ar->htt.empty_tx_wq);
768 	wake_up(&ar->wmi.tx_credits_wq);
769 	wake_up(&ar->peer_mapping_wq);
770 
771 	mutex_lock(&ar->conf_mutex);
772 
773 	switch (ar->state) {
774 	case ATH10K_STATE_ON:
775 		ar->state = ATH10K_STATE_RESTARTING;
776 		ath10k_hif_stop(ar);
777 		ath10k_scan_finish(ar);
778 		ieee80211_restart_hw(ar->hw);
779 		break;
780 	case ATH10K_STATE_OFF:
781 		/* this can happen if driver is being unloaded
782 		 * or if the crash happens during FW probing */
783 		ath10k_warn(ar, "cannot restart a device that hasn't been started\n");
784 		break;
785 	case ATH10K_STATE_RESTARTING:
786 		/* hw restart might be requested from multiple places */
787 		break;
788 	case ATH10K_STATE_RESTARTED:
789 		ar->state = ATH10K_STATE_WEDGED;
790 		/* fall through */
791 	case ATH10K_STATE_WEDGED:
792 		ath10k_warn(ar, "device is wedged, will not restart\n");
793 		break;
794 	case ATH10K_STATE_UTF:
795 		ath10k_warn(ar, "firmware restart in UTF mode not supported\n");
796 		break;
797 	}
798 
799 	mutex_unlock(&ar->conf_mutex);
800 }
801 
802 static void ath10k_core_init_max_sta_count(struct ath10k *ar)
803 {
804 	if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
805 		ar->max_num_peers = TARGET_10X_NUM_PEERS;
806 		ar->max_num_stations = TARGET_10X_NUM_STATIONS;
807 	} else {
808 		ar->max_num_peers = TARGET_NUM_PEERS;
809 		ar->max_num_stations = TARGET_NUM_STATIONS;
810 	}
811 }
812 
813 int ath10k_core_start(struct ath10k *ar, enum ath10k_firmware_mode mode)
814 {
815 	int status;
816 
817 	lockdep_assert_held(&ar->conf_mutex);
818 
819 	clear_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
820 
821 	ath10k_bmi_start(ar);
822 
823 	if (ath10k_init_configure_target(ar)) {
824 		status = -EINVAL;
825 		goto err;
826 	}
827 
828 	status = ath10k_download_cal_data(ar);
829 	if (status)
830 		goto err;
831 
832 	status = ath10k_download_fw(ar, mode);
833 	if (status)
834 		goto err;
835 
836 	status = ath10k_init_uart(ar);
837 	if (status)
838 		goto err;
839 
840 	ar->htc.htc_ops.target_send_suspend_complete =
841 		ath10k_send_suspend_complete;
842 
843 	status = ath10k_htc_init(ar);
844 	if (status) {
845 		ath10k_err(ar, "could not init HTC (%d)\n", status);
846 		goto err;
847 	}
848 
849 	status = ath10k_bmi_done(ar);
850 	if (status)
851 		goto err;
852 
853 	status = ath10k_wmi_attach(ar);
854 	if (status) {
855 		ath10k_err(ar, "WMI attach failed: %d\n", status);
856 		goto err;
857 	}
858 
859 	status = ath10k_htt_init(ar);
860 	if (status) {
861 		ath10k_err(ar, "failed to init htt: %d\n", status);
862 		goto err_wmi_detach;
863 	}
864 
865 	status = ath10k_htt_tx_alloc(&ar->htt);
866 	if (status) {
867 		ath10k_err(ar, "failed to alloc htt tx: %d\n", status);
868 		goto err_wmi_detach;
869 	}
870 
871 	status = ath10k_htt_rx_alloc(&ar->htt);
872 	if (status) {
873 		ath10k_err(ar, "failed to alloc htt rx: %d\n", status);
874 		goto err_htt_tx_detach;
875 	}
876 
877 	status = ath10k_hif_start(ar);
878 	if (status) {
879 		ath10k_err(ar, "could not start HIF: %d\n", status);
880 		goto err_htt_rx_detach;
881 	}
882 
883 	status = ath10k_htc_wait_target(&ar->htc);
884 	if (status) {
885 		ath10k_err(ar, "failed to connect to HTC: %d\n", status);
886 		goto err_hif_stop;
887 	}
888 
889 	if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
890 		status = ath10k_htt_connect(&ar->htt);
891 		if (status) {
892 			ath10k_err(ar, "failed to connect htt (%d)\n", status);
893 			goto err_hif_stop;
894 		}
895 	}
896 
897 	status = ath10k_wmi_connect(ar);
898 	if (status) {
899 		ath10k_err(ar, "could not connect wmi: %d\n", status);
900 		goto err_hif_stop;
901 	}
902 
903 	status = ath10k_htc_start(&ar->htc);
904 	if (status) {
905 		ath10k_err(ar, "failed to start htc: %d\n", status);
906 		goto err_hif_stop;
907 	}
908 
909 	if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
910 		status = ath10k_wmi_wait_for_service_ready(ar);
911 		if (status <= 0) {
912 			ath10k_warn(ar, "wmi service ready event not received");
913 			status = -ETIMEDOUT;
914 			goto err_hif_stop;
915 		}
916 	}
917 
918 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "firmware %s booted\n",
919 		   ar->hw->wiphy->fw_version);
920 
921 	status = ath10k_wmi_cmd_init(ar);
922 	if (status) {
923 		ath10k_err(ar, "could not send WMI init command (%d)\n",
924 			   status);
925 		goto err_hif_stop;
926 	}
927 
928 	status = ath10k_wmi_wait_for_unified_ready(ar);
929 	if (status <= 0) {
930 		ath10k_err(ar, "wmi unified ready event not received\n");
931 		status = -ETIMEDOUT;
932 		goto err_hif_stop;
933 	}
934 
935 	/* we don't care about HTT in UTF mode */
936 	if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
937 		status = ath10k_htt_setup(&ar->htt);
938 		if (status) {
939 			ath10k_err(ar, "failed to setup htt: %d\n", status);
940 			goto err_hif_stop;
941 		}
942 	}
943 
944 	status = ath10k_debug_start(ar);
945 	if (status)
946 		goto err_hif_stop;
947 
948 	if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features))
949 		ar->free_vdev_map = (1LL << TARGET_10X_NUM_VDEVS) - 1;
950 	else
951 		ar->free_vdev_map = (1LL << TARGET_NUM_VDEVS) - 1;
952 
953 	INIT_LIST_HEAD(&ar->arvifs);
954 
955 	return 0;
956 
957 err_hif_stop:
958 	ath10k_hif_stop(ar);
959 err_htt_rx_detach:
960 	ath10k_htt_rx_free(&ar->htt);
961 err_htt_tx_detach:
962 	ath10k_htt_tx_free(&ar->htt);
963 err_wmi_detach:
964 	ath10k_wmi_detach(ar);
965 err:
966 	return status;
967 }
968 EXPORT_SYMBOL(ath10k_core_start);
969 
970 int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt)
971 {
972 	int ret;
973 
974 	reinit_completion(&ar->target_suspend);
975 
976 	ret = ath10k_wmi_pdev_suspend_target(ar, suspend_opt);
977 	if (ret) {
978 		ath10k_warn(ar, "could not suspend target (%d)\n", ret);
979 		return ret;
980 	}
981 
982 	ret = wait_for_completion_timeout(&ar->target_suspend, 1 * HZ);
983 
984 	if (ret == 0) {
985 		ath10k_warn(ar, "suspend timed out - target pause event never came\n");
986 		return -ETIMEDOUT;
987 	}
988 
989 	return 0;
990 }
991 
992 void ath10k_core_stop(struct ath10k *ar)
993 {
994 	lockdep_assert_held(&ar->conf_mutex);
995 
996 	/* try to suspend target */
997 	if (ar->state != ATH10K_STATE_RESTARTING &&
998 	    ar->state != ATH10K_STATE_UTF)
999 		ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND_AND_DISABLE_INTR);
1000 
1001 	ath10k_debug_stop(ar);
1002 	ath10k_hif_stop(ar);
1003 	ath10k_htt_tx_free(&ar->htt);
1004 	ath10k_htt_rx_free(&ar->htt);
1005 	ath10k_wmi_detach(ar);
1006 }
1007 EXPORT_SYMBOL(ath10k_core_stop);
1008 
1009 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
1010  * order to know what hw capabilities should be advertised to mac80211 it is
1011  * necessary to load the firmware (and tear it down immediately since start
1012  * hook will try to init it again) before registering */
1013 static int ath10k_core_probe_fw(struct ath10k *ar)
1014 {
1015 	struct bmi_target_info target_info;
1016 	int ret = 0;
1017 
1018 	ret = ath10k_hif_power_up(ar);
1019 	if (ret) {
1020 		ath10k_err(ar, "could not start pci hif (%d)\n", ret);
1021 		return ret;
1022 	}
1023 
1024 	memset(&target_info, 0, sizeof(target_info));
1025 	ret = ath10k_bmi_get_target_info(ar, &target_info);
1026 	if (ret) {
1027 		ath10k_err(ar, "could not get target info (%d)\n", ret);
1028 		ath10k_hif_power_down(ar);
1029 		return ret;
1030 	}
1031 
1032 	ar->target_version = target_info.version;
1033 	ar->hw->wiphy->hw_version = target_info.version;
1034 
1035 	ret = ath10k_init_hw_params(ar);
1036 	if (ret) {
1037 		ath10k_err(ar, "could not get hw params (%d)\n", ret);
1038 		ath10k_hif_power_down(ar);
1039 		return ret;
1040 	}
1041 
1042 	ret = ath10k_core_fetch_firmware_files(ar);
1043 	if (ret) {
1044 		ath10k_err(ar, "could not fetch firmware files (%d)\n", ret);
1045 		ath10k_hif_power_down(ar);
1046 		return ret;
1047 	}
1048 
1049 	ath10k_core_init_max_sta_count(ar);
1050 
1051 	mutex_lock(&ar->conf_mutex);
1052 
1053 	ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
1054 	if (ret) {
1055 		ath10k_err(ar, "could not init core (%d)\n", ret);
1056 		ath10k_core_free_firmware_files(ar);
1057 		ath10k_hif_power_down(ar);
1058 		mutex_unlock(&ar->conf_mutex);
1059 		return ret;
1060 	}
1061 
1062 	ath10k_print_driver_info(ar);
1063 	ath10k_core_stop(ar);
1064 
1065 	mutex_unlock(&ar->conf_mutex);
1066 
1067 	ath10k_hif_power_down(ar);
1068 	return 0;
1069 }
1070 
1071 static int ath10k_core_check_chip_id(struct ath10k *ar)
1072 {
1073 	u32 hw_revision = MS(ar->chip_id, SOC_CHIP_ID_REV);
1074 
1075 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip_id 0x%08x hw_revision 0x%x\n",
1076 		   ar->chip_id, hw_revision);
1077 
1078 	/* Check that we are not using hw1.0 (some of them have same pci id
1079 	 * as hw2.0) before doing anything else as ath10k crashes horribly
1080 	 * due to missing hw1.0 workarounds. */
1081 	switch (hw_revision) {
1082 	case QCA988X_HW_1_0_CHIP_ID_REV:
1083 		ath10k_err(ar, "ERROR: qca988x hw1.0 is not supported\n");
1084 		return -EOPNOTSUPP;
1085 
1086 	case QCA988X_HW_2_0_CHIP_ID_REV:
1087 		/* known hardware revision, continue normally */
1088 		return 0;
1089 
1090 	default:
1091 		ath10k_warn(ar, "Warning: hardware revision unknown (0x%x), expect problems\n",
1092 			    ar->chip_id);
1093 		return 0;
1094 	}
1095 
1096 	return 0;
1097 }
1098 
1099 static void ath10k_core_register_work(struct work_struct *work)
1100 {
1101 	struct ath10k *ar = container_of(work, struct ath10k, register_work);
1102 	int status;
1103 
1104 	status = ath10k_core_probe_fw(ar);
1105 	if (status) {
1106 		ath10k_err(ar, "could not probe fw (%d)\n", status);
1107 		goto err;
1108 	}
1109 
1110 	status = ath10k_mac_register(ar);
1111 	if (status) {
1112 		ath10k_err(ar, "could not register to mac80211 (%d)\n", status);
1113 		goto err_release_fw;
1114 	}
1115 
1116 	status = ath10k_debug_register(ar);
1117 	if (status) {
1118 		ath10k_err(ar, "unable to initialize debugfs\n");
1119 		goto err_unregister_mac;
1120 	}
1121 
1122 	status = ath10k_spectral_create(ar);
1123 	if (status) {
1124 		ath10k_err(ar, "failed to initialize spectral\n");
1125 		goto err_debug_destroy;
1126 	}
1127 
1128 	set_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags);
1129 	return;
1130 
1131 err_debug_destroy:
1132 	ath10k_debug_destroy(ar);
1133 err_unregister_mac:
1134 	ath10k_mac_unregister(ar);
1135 err_release_fw:
1136 	ath10k_core_free_firmware_files(ar);
1137 err:
1138 	/* TODO: It's probably a good idea to release device from the driver
1139 	 * but calling device_release_driver() here will cause a deadlock.
1140 	 */
1141 	return;
1142 }
1143 
1144 int ath10k_core_register(struct ath10k *ar, u32 chip_id)
1145 {
1146 	int status;
1147 
1148 	ar->chip_id = chip_id;
1149 
1150 	status = ath10k_core_check_chip_id(ar);
1151 	if (status) {
1152 		ath10k_err(ar, "Unsupported chip id 0x%08x\n", ar->chip_id);
1153 		return status;
1154 	}
1155 
1156 	queue_work(ar->workqueue, &ar->register_work);
1157 
1158 	return 0;
1159 }
1160 EXPORT_SYMBOL(ath10k_core_register);
1161 
1162 void ath10k_core_unregister(struct ath10k *ar)
1163 {
1164 	cancel_work_sync(&ar->register_work);
1165 
1166 	if (!test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags))
1167 		return;
1168 
1169 	/* Stop spectral before unregistering from mac80211 to remove the
1170 	 * relayfs debugfs file cleanly. Otherwise the parent debugfs tree
1171 	 * would be already be free'd recursively, leading to a double free.
1172 	 */
1173 	ath10k_spectral_destroy(ar);
1174 
1175 	/* We must unregister from mac80211 before we stop HTC and HIF.
1176 	 * Otherwise we will fail to submit commands to FW and mac80211 will be
1177 	 * unhappy about callback failures. */
1178 	ath10k_mac_unregister(ar);
1179 
1180 	ath10k_testmode_destroy(ar);
1181 
1182 	ath10k_core_free_firmware_files(ar);
1183 
1184 	ath10k_debug_unregister(ar);
1185 }
1186 EXPORT_SYMBOL(ath10k_core_unregister);
1187 
1188 struct ath10k *ath10k_core_create(size_t priv_size, struct device *dev,
1189 				  enum ath10k_bus bus,
1190 				  const struct ath10k_hif_ops *hif_ops)
1191 {
1192 	struct ath10k *ar;
1193 	int ret;
1194 
1195 	ar = ath10k_mac_create(priv_size);
1196 	if (!ar)
1197 		return NULL;
1198 
1199 	ar->ath_common.priv = ar;
1200 	ar->ath_common.hw = ar->hw;
1201 
1202 	ar->p2p = !!ath10k_p2p;
1203 	ar->dev = dev;
1204 
1205 	ar->hif.ops = hif_ops;
1206 	ar->hif.bus = bus;
1207 
1208 	init_completion(&ar->scan.started);
1209 	init_completion(&ar->scan.completed);
1210 	init_completion(&ar->scan.on_channel);
1211 	init_completion(&ar->target_suspend);
1212 
1213 	init_completion(&ar->install_key_done);
1214 	init_completion(&ar->vdev_setup_done);
1215 
1216 	INIT_DELAYED_WORK(&ar->scan.timeout, ath10k_scan_timeout_work);
1217 
1218 	ar->workqueue = create_singlethread_workqueue("ath10k_wq");
1219 	if (!ar->workqueue)
1220 		goto err_free_mac;
1221 
1222 	mutex_init(&ar->conf_mutex);
1223 	spin_lock_init(&ar->data_lock);
1224 
1225 	INIT_LIST_HEAD(&ar->peers);
1226 	init_waitqueue_head(&ar->peer_mapping_wq);
1227 	init_waitqueue_head(&ar->htt.empty_tx_wq);
1228 	init_waitqueue_head(&ar->wmi.tx_credits_wq);
1229 
1230 	init_completion(&ar->offchan_tx_completed);
1231 	INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
1232 	skb_queue_head_init(&ar->offchan_tx_queue);
1233 
1234 	INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
1235 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
1236 
1237 	INIT_WORK(&ar->register_work, ath10k_core_register_work);
1238 	INIT_WORK(&ar->restart_work, ath10k_core_restart);
1239 
1240 	ret = ath10k_debug_create(ar);
1241 	if (ret)
1242 		goto err_free_wq;
1243 
1244 	return ar;
1245 
1246 err_free_wq:
1247 	destroy_workqueue(ar->workqueue);
1248 
1249 err_free_mac:
1250 	ath10k_mac_destroy(ar);
1251 
1252 	return NULL;
1253 }
1254 EXPORT_SYMBOL(ath10k_core_create);
1255 
1256 void ath10k_core_destroy(struct ath10k *ar)
1257 {
1258 	flush_workqueue(ar->workqueue);
1259 	destroy_workqueue(ar->workqueue);
1260 
1261 	ath10k_debug_destroy(ar);
1262 	ath10k_mac_destroy(ar);
1263 }
1264 EXPORT_SYMBOL(ath10k_core_destroy);
1265 
1266 MODULE_AUTHOR("Qualcomm Atheros");
1267 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
1268 MODULE_LICENSE("Dual BSD/GPL");
1269