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 
30 unsigned int ath10k_debug_mask;
31 static bool uart_print;
32 static unsigned int ath10k_p2p;
33 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
34 module_param(uart_print, bool, 0644);
35 module_param_named(p2p, ath10k_p2p, uint, 0644);
36 MODULE_PARM_DESC(debug_mask, "Debugging mask");
37 MODULE_PARM_DESC(uart_print, "Uart target debugging");
38 MODULE_PARM_DESC(p2p, "Enable ath10k P2P support");
39 
40 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
41 	{
42 		.id = QCA988X_HW_1_0_VERSION,
43 		.name = "qca988x hw1.0",
44 		.patch_load_addr = QCA988X_HW_1_0_PATCH_LOAD_ADDR,
45 		.fw = {
46 			.dir = QCA988X_HW_1_0_FW_DIR,
47 			.fw = QCA988X_HW_1_0_FW_FILE,
48 			.otp = QCA988X_HW_1_0_OTP_FILE,
49 			.board = QCA988X_HW_1_0_BOARD_DATA_FILE,
50 		},
51 	},
52 	{
53 		.id = QCA988X_HW_2_0_VERSION,
54 		.name = "qca988x hw2.0",
55 		.patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
56 		.fw = {
57 			.dir = QCA988X_HW_2_0_FW_DIR,
58 			.fw = QCA988X_HW_2_0_FW_FILE,
59 			.otp = QCA988X_HW_2_0_OTP_FILE,
60 			.board = QCA988X_HW_2_0_BOARD_DATA_FILE,
61 		},
62 	},
63 };
64 
65 static void ath10k_send_suspend_complete(struct ath10k *ar)
66 {
67 	ath10k_dbg(ATH10K_DBG_CORE, "%s\n", __func__);
68 
69 	ar->is_target_paused = true;
70 	wake_up(&ar->event_queue);
71 }
72 
73 static int ath10k_check_fw_version(struct ath10k *ar)
74 {
75 	char version[32];
76 
77 	if (ar->fw_version_major >= SUPPORTED_FW_MAJOR &&
78 	    ar->fw_version_minor >= SUPPORTED_FW_MINOR &&
79 	    ar->fw_version_release >= SUPPORTED_FW_RELEASE &&
80 	    ar->fw_version_build >= SUPPORTED_FW_BUILD)
81 		return 0;
82 
83 	snprintf(version, sizeof(version), "%u.%u.%u.%u",
84 		 SUPPORTED_FW_MAJOR, SUPPORTED_FW_MINOR,
85 		 SUPPORTED_FW_RELEASE, SUPPORTED_FW_BUILD);
86 
87 	ath10k_warn("WARNING: Firmware version %s is not officially supported.\n",
88 		    ar->hw->wiphy->fw_version);
89 	ath10k_warn("Please upgrade to version %s (or newer)\n", version);
90 
91 	return 0;
92 }
93 
94 static int ath10k_init_connect_htc(struct ath10k *ar)
95 {
96 	int status;
97 
98 	status = ath10k_wmi_connect_htc_service(ar);
99 	if (status)
100 		goto conn_fail;
101 
102 	/* Start HTC */
103 	status = ath10k_htc_start(ar->htc);
104 	if (status)
105 		goto conn_fail;
106 
107 	/* Wait for WMI event to be ready */
108 	status = ath10k_wmi_wait_for_service_ready(ar);
109 	if (status <= 0) {
110 		ath10k_warn("wmi service ready event not received");
111 		status = -ETIMEDOUT;
112 		goto timeout;
113 	}
114 
115 	ath10k_dbg(ATH10K_DBG_CORE, "core wmi ready\n");
116 	return 0;
117 
118 timeout:
119 	ath10k_htc_stop(ar->htc);
120 conn_fail:
121 	return status;
122 }
123 
124 static int ath10k_init_configure_target(struct ath10k *ar)
125 {
126 	u32 param_host;
127 	int ret;
128 
129 	/* tell target which HTC version it is used*/
130 	ret = ath10k_bmi_write32(ar, hi_app_host_interest,
131 				 HTC_PROTOCOL_VERSION);
132 	if (ret) {
133 		ath10k_err("settings HTC version failed\n");
134 		return ret;
135 	}
136 
137 	/* set the firmware mode to STA/IBSS/AP */
138 	ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
139 	if (ret) {
140 		ath10k_err("setting firmware mode (1/2) failed\n");
141 		return ret;
142 	}
143 
144 	/* TODO following parameters need to be re-visited. */
145 	/* num_device */
146 	param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
147 	/* Firmware mode */
148 	/* FIXME: Why FW_MODE_AP ??.*/
149 	param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
150 	/* mac_addr_method */
151 	param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
152 	/* firmware_bridge */
153 	param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
154 	/* fwsubmode */
155 	param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
156 
157 	ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
158 	if (ret) {
159 		ath10k_err("setting firmware mode (2/2) failed\n");
160 		return ret;
161 	}
162 
163 	/* We do all byte-swapping on the host */
164 	ret = ath10k_bmi_write32(ar, hi_be, 0);
165 	if (ret) {
166 		ath10k_err("setting host CPU BE mode failed\n");
167 		return ret;
168 	}
169 
170 	/* FW descriptor/Data swap flags */
171 	ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
172 
173 	if (ret) {
174 		ath10k_err("setting FW data/desc swap flags failed\n");
175 		return ret;
176 	}
177 
178 	return 0;
179 }
180 
181 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
182 						   const char *dir,
183 						   const char *file)
184 {
185 	char filename[100];
186 	const struct firmware *fw;
187 	int ret;
188 
189 	if (file == NULL)
190 		return ERR_PTR(-ENOENT);
191 
192 	if (dir == NULL)
193 		dir = ".";
194 
195 	snprintf(filename, sizeof(filename), "%s/%s", dir, file);
196 	ret = request_firmware(&fw, filename, ar->dev);
197 	if (ret)
198 		return ERR_PTR(ret);
199 
200 	return fw;
201 }
202 
203 static int ath10k_push_board_ext_data(struct ath10k *ar,
204 				      const struct firmware *fw)
205 {
206 	u32 board_data_size = QCA988X_BOARD_DATA_SZ;
207 	u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
208 	u32 board_ext_data_addr;
209 	int ret;
210 
211 	ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
212 	if (ret) {
213 		ath10k_err("could not read board ext data addr (%d)\n", ret);
214 		return ret;
215 	}
216 
217 	ath10k_dbg(ATH10K_DBG_CORE,
218 		   "ath10k: Board extended Data download addr: 0x%x\n",
219 		   board_ext_data_addr);
220 
221 	if (board_ext_data_addr == 0)
222 		return 0;
223 
224 	if (fw->size != (board_data_size + board_ext_data_size)) {
225 		ath10k_err("invalid board (ext) data sizes %zu != %d+%d\n",
226 			   fw->size, board_data_size, board_ext_data_size);
227 		return -EINVAL;
228 	}
229 
230 	ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
231 				      fw->data + board_data_size,
232 				      board_ext_data_size);
233 	if (ret) {
234 		ath10k_err("could not write board ext data (%d)\n", ret);
235 		return ret;
236 	}
237 
238 	ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
239 				 (board_ext_data_size << 16) | 1);
240 	if (ret) {
241 		ath10k_err("could not write board ext data bit (%d)\n", ret);
242 		return ret;
243 	}
244 
245 	return 0;
246 }
247 
248 static int ath10k_download_board_data(struct ath10k *ar)
249 {
250 	u32 board_data_size = QCA988X_BOARD_DATA_SZ;
251 	u32 address;
252 	const struct firmware *fw;
253 	int ret;
254 
255 	fw = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir,
256 				  ar->hw_params.fw.board);
257 	if (IS_ERR(fw)) {
258 		ath10k_err("could not fetch board data fw file (%ld)\n",
259 			   PTR_ERR(fw));
260 		return PTR_ERR(fw);
261 	}
262 
263 	ret = ath10k_push_board_ext_data(ar, fw);
264 	if (ret) {
265 		ath10k_err("could not push board ext data (%d)\n", ret);
266 		goto exit;
267 	}
268 
269 	ret = ath10k_bmi_read32(ar, hi_board_data, &address);
270 	if (ret) {
271 		ath10k_err("could not read board data addr (%d)\n", ret);
272 		goto exit;
273 	}
274 
275 	ret = ath10k_bmi_write_memory(ar, address, fw->data,
276 				      min_t(u32, board_data_size, fw->size));
277 	if (ret) {
278 		ath10k_err("could not write board data (%d)\n", ret);
279 		goto exit;
280 	}
281 
282 	ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
283 	if (ret) {
284 		ath10k_err("could not write board data bit (%d)\n", ret);
285 		goto exit;
286 	}
287 
288 exit:
289 	release_firmware(fw);
290 	return ret;
291 }
292 
293 static int ath10k_download_and_run_otp(struct ath10k *ar)
294 {
295 	const struct firmware *fw;
296 	u32 address;
297 	u32 exec_param;
298 	int ret;
299 
300 	/* OTP is optional */
301 
302 	if (ar->hw_params.fw.otp == NULL) {
303 		ath10k_info("otp file not defined\n");
304 		return 0;
305 	}
306 
307 	address = ar->hw_params.patch_load_addr;
308 
309 	fw = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir,
310 				  ar->hw_params.fw.otp);
311 	if (IS_ERR(fw)) {
312 		ath10k_warn("could not fetch otp (%ld)\n", PTR_ERR(fw));
313 		return 0;
314 	}
315 
316 	ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
317 	if (ret) {
318 		ath10k_err("could not write otp (%d)\n", ret);
319 		goto exit;
320 	}
321 
322 	exec_param = 0;
323 	ret = ath10k_bmi_execute(ar, address, &exec_param);
324 	if (ret) {
325 		ath10k_err("could not execute otp (%d)\n", ret);
326 		goto exit;
327 	}
328 
329 exit:
330 	release_firmware(fw);
331 	return ret;
332 }
333 
334 static int ath10k_download_fw(struct ath10k *ar)
335 {
336 	const struct firmware *fw;
337 	u32 address;
338 	int ret;
339 
340 	if (ar->hw_params.fw.fw == NULL)
341 		return -EINVAL;
342 
343 	address = ar->hw_params.patch_load_addr;
344 
345 	fw = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir,
346 				  ar->hw_params.fw.fw);
347 	if (IS_ERR(fw)) {
348 		ath10k_err("could not fetch fw (%ld)\n", PTR_ERR(fw));
349 		return PTR_ERR(fw);
350 	}
351 
352 	ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
353 	if (ret) {
354 		ath10k_err("could not write fw (%d)\n", ret);
355 		goto exit;
356 	}
357 
358 exit:
359 	release_firmware(fw);
360 	return ret;
361 }
362 
363 static int ath10k_init_download_firmware(struct ath10k *ar)
364 {
365 	int ret;
366 
367 	ret = ath10k_download_board_data(ar);
368 	if (ret)
369 		return ret;
370 
371 	ret = ath10k_download_and_run_otp(ar);
372 	if (ret)
373 		return ret;
374 
375 	ret = ath10k_download_fw(ar);
376 	if (ret)
377 		return ret;
378 
379 	return ret;
380 }
381 
382 static int ath10k_init_uart(struct ath10k *ar)
383 {
384 	int ret;
385 
386 	/*
387 	 * Explicitly setting UART prints to zero as target turns it on
388 	 * based on scratch registers.
389 	 */
390 	ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
391 	if (ret) {
392 		ath10k_warn("could not disable UART prints (%d)\n", ret);
393 		return ret;
394 	}
395 
396 	if (!uart_print) {
397 		ath10k_info("UART prints disabled\n");
398 		return 0;
399 	}
400 
401 	ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
402 	if (ret) {
403 		ath10k_warn("could not enable UART prints (%d)\n", ret);
404 		return ret;
405 	}
406 
407 	ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
408 	if (ret) {
409 		ath10k_warn("could not enable UART prints (%d)\n", ret);
410 		return ret;
411 	}
412 
413 	ath10k_info("UART prints enabled\n");
414 	return 0;
415 }
416 
417 static int ath10k_init_hw_params(struct ath10k *ar)
418 {
419 	const struct ath10k_hw_params *uninitialized_var(hw_params);
420 	int i;
421 
422 	for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
423 		hw_params = &ath10k_hw_params_list[i];
424 
425 		if (hw_params->id == ar->target_version)
426 			break;
427 	}
428 
429 	if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
430 		ath10k_err("Unsupported hardware version: 0x%x\n",
431 			   ar->target_version);
432 		return -EINVAL;
433 	}
434 
435 	ar->hw_params = *hw_params;
436 
437 	ath10k_info("Hardware name %s version 0x%x\n",
438 		    ar->hw_params.name, ar->target_version);
439 
440 	return 0;
441 }
442 
443 struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
444 				  enum ath10k_bus bus,
445 				  const struct ath10k_hif_ops *hif_ops)
446 {
447 	struct ath10k *ar;
448 
449 	ar = ath10k_mac_create();
450 	if (!ar)
451 		return NULL;
452 
453 	ar->ath_common.priv = ar;
454 	ar->ath_common.hw = ar->hw;
455 
456 	ar->p2p = !!ath10k_p2p;
457 	ar->dev = dev;
458 
459 	ar->hif.priv = hif_priv;
460 	ar->hif.ops = hif_ops;
461 	ar->hif.bus = bus;
462 
463 	ar->free_vdev_map = 0xFF; /* 8 vdevs */
464 
465 	init_completion(&ar->scan.started);
466 	init_completion(&ar->scan.completed);
467 	init_completion(&ar->scan.on_channel);
468 
469 	init_completion(&ar->install_key_done);
470 	init_completion(&ar->vdev_setup_done);
471 
472 	setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
473 
474 	ar->workqueue = create_singlethread_workqueue("ath10k_wq");
475 	if (!ar->workqueue)
476 		goto err_wq;
477 
478 	mutex_init(&ar->conf_mutex);
479 	spin_lock_init(&ar->data_lock);
480 
481 	INIT_LIST_HEAD(&ar->peers);
482 	init_waitqueue_head(&ar->peer_mapping_wq);
483 
484 	init_completion(&ar->offchan_tx_completed);
485 	INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
486 	skb_queue_head_init(&ar->offchan_tx_queue);
487 
488 	init_waitqueue_head(&ar->event_queue);
489 
490 	return ar;
491 
492 err_wq:
493 	ath10k_mac_destroy(ar);
494 	return NULL;
495 }
496 EXPORT_SYMBOL(ath10k_core_create);
497 
498 void ath10k_core_destroy(struct ath10k *ar)
499 {
500 	flush_workqueue(ar->workqueue);
501 	destroy_workqueue(ar->workqueue);
502 
503 	ath10k_mac_destroy(ar);
504 }
505 EXPORT_SYMBOL(ath10k_core_destroy);
506 
507 
508 int ath10k_core_register(struct ath10k *ar)
509 {
510 	struct ath10k_htc_ops htc_ops;
511 	struct bmi_target_info target_info;
512 	int status;
513 
514 	memset(&target_info, 0, sizeof(target_info));
515 	status = ath10k_bmi_get_target_info(ar, &target_info);
516 	if (status)
517 		goto err;
518 
519 	ar->target_version = target_info.version;
520 	ar->hw->wiphy->hw_version = target_info.version;
521 
522 	status = ath10k_init_hw_params(ar);
523 	if (status)
524 		goto err;
525 
526 	if (ath10k_init_configure_target(ar)) {
527 		status = -EINVAL;
528 		goto err;
529 	}
530 
531 	status = ath10k_init_download_firmware(ar);
532 	if (status)
533 		goto err;
534 
535 	status = ath10k_init_uart(ar);
536 	if (status)
537 		goto err;
538 
539 	htc_ops.target_send_suspend_complete = ath10k_send_suspend_complete;
540 
541 	ar->htc = ath10k_htc_create(ar, &htc_ops);
542 	if (IS_ERR(ar->htc)) {
543 		status = PTR_ERR(ar->htc);
544 		ath10k_err("could not create HTC (%d)\n", status);
545 		goto err;
546 	}
547 
548 	status = ath10k_bmi_done(ar);
549 	if (status)
550 		goto err_htc_destroy;
551 
552 	status = ath10k_wmi_attach(ar);
553 	if (status) {
554 		ath10k_err("WMI attach failed: %d\n", status);
555 		goto err_htc_destroy;
556 	}
557 
558 	status = ath10k_htc_wait_target(ar->htc);
559 	if (status)
560 		goto err_wmi_detach;
561 
562 	ar->htt = ath10k_htt_attach(ar);
563 	if (!ar->htt) {
564 		status = -ENOMEM;
565 		goto err_wmi_detach;
566 	}
567 
568 	status = ath10k_init_connect_htc(ar);
569 	if (status)
570 		goto err_htt_detach;
571 
572 	ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
573 
574 	status = ath10k_check_fw_version(ar);
575 	if (status)
576 		goto err_disconnect_htc;
577 
578 	status = ath10k_wmi_cmd_init(ar);
579 	if (status) {
580 		ath10k_err("could not send WMI init command (%d)\n", status);
581 		goto err_disconnect_htc;
582 	}
583 
584 	status = ath10k_wmi_wait_for_unified_ready(ar);
585 	if (status <= 0) {
586 		ath10k_err("wmi unified ready event not received\n");
587 		status = -ETIMEDOUT;
588 		goto err_disconnect_htc;
589 	}
590 
591 	status = ath10k_htt_attach_target(ar->htt);
592 	if (status)
593 		goto err_disconnect_htc;
594 
595 	status = ath10k_mac_register(ar);
596 	if (status)
597 		goto err_disconnect_htc;
598 
599 	status = ath10k_debug_create(ar);
600 	if (status) {
601 		ath10k_err("unable to initialize debugfs\n");
602 		goto err_unregister_mac;
603 	}
604 
605 	return 0;
606 
607 err_unregister_mac:
608 	ath10k_mac_unregister(ar);
609 err_disconnect_htc:
610 	ath10k_htc_stop(ar->htc);
611 err_htt_detach:
612 	ath10k_htt_detach(ar->htt);
613 err_wmi_detach:
614 	ath10k_wmi_detach(ar);
615 err_htc_destroy:
616 	ath10k_htc_destroy(ar->htc);
617 err:
618 	return status;
619 }
620 EXPORT_SYMBOL(ath10k_core_register);
621 
622 void ath10k_core_unregister(struct ath10k *ar)
623 {
624 	/* We must unregister from mac80211 before we stop HTC and HIF.
625 	 * Otherwise we will fail to submit commands to FW and mac80211 will be
626 	 * unhappy about callback failures. */
627 	ath10k_mac_unregister(ar);
628 	ath10k_htc_stop(ar->htc);
629 	ath10k_htt_detach(ar->htt);
630 	ath10k_wmi_detach(ar);
631 	ath10k_htc_destroy(ar->htc);
632 }
633 EXPORT_SYMBOL(ath10k_core_unregister);
634 
635 int ath10k_core_target_suspend(struct ath10k *ar)
636 {
637 	int ret;
638 
639 	ath10k_dbg(ATH10K_DBG_CORE, "%s: called", __func__);
640 
641 	ret = ath10k_wmi_pdev_suspend_target(ar);
642 	if (ret)
643 		ath10k_warn("could not suspend target (%d)\n", ret);
644 
645 	return ret;
646 }
647 EXPORT_SYMBOL(ath10k_core_target_suspend);
648 
649 int ath10k_core_target_resume(struct ath10k *ar)
650 {
651 	int ret;
652 
653 	ath10k_dbg(ATH10K_DBG_CORE, "%s: called", __func__);
654 
655 	ret = ath10k_wmi_pdev_resume_target(ar);
656 	if (ret)
657 		ath10k_warn("could not resume target (%d)\n", ret);
658 
659 	return ret;
660 }
661 EXPORT_SYMBOL(ath10k_core_target_resume);
662 
663 MODULE_AUTHOR("Qualcomm Atheros");
664 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
665 MODULE_LICENSE("Dual BSD/GPL");
666