core.c revision cd003fad17d9258efdc5dd658666731377cfebd1
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
30unsigned int ath10k_debug_mask;
31static bool uart_print;
32static unsigned int ath10k_p2p;
33module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
34module_param(uart_print, bool, 0644);
35module_param_named(p2p, ath10k_p2p, uint, 0644);
36MODULE_PARM_DESC(debug_mask, "Debugging mask");
37MODULE_PARM_DESC(uart_print, "Uart target debugging");
38MODULE_PARM_DESC(p2p, "Enable ath10k P2P support");
39
40static 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
65static 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
73static 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
94static 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
118timeout:
119	ath10k_htc_stop(&ar->htc);
120conn_fail:
121	return status;
122}
123
124static 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
181static 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
203static 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
248static 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
288exit:
289	release_firmware(fw);
290	return ret;
291}
292
293static 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
329exit:
330	release_firmware(fw);
331	return ret;
332}
333
334static 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
358exit:
359	release_firmware(fw);
360	return ret;
361}
362
363static 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
382static 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
417static 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
443struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
444				  const struct ath10k_hif_ops *hif_ops)
445{
446	struct ath10k *ar;
447
448	ar = ath10k_mac_create();
449	if (!ar)
450		return NULL;
451
452	ar->ath_common.priv = ar;
453	ar->ath_common.hw = ar->hw;
454
455	ar->p2p = !!ath10k_p2p;
456	ar->dev = dev;
457
458	ar->hif.priv = hif_priv;
459	ar->hif.ops = hif_ops;
460
461	ar->free_vdev_map = 0xFF; /* 8 vdevs */
462
463	init_completion(&ar->scan.started);
464	init_completion(&ar->scan.completed);
465	init_completion(&ar->scan.on_channel);
466
467	init_completion(&ar->install_key_done);
468	init_completion(&ar->vdev_setup_done);
469
470	setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
471
472	ar->workqueue = create_singlethread_workqueue("ath10k_wq");
473	if (!ar->workqueue)
474		goto err_wq;
475
476	mutex_init(&ar->conf_mutex);
477	spin_lock_init(&ar->data_lock);
478
479	INIT_LIST_HEAD(&ar->peers);
480	init_waitqueue_head(&ar->peer_mapping_wq);
481
482	init_completion(&ar->offchan_tx_completed);
483	INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
484	skb_queue_head_init(&ar->offchan_tx_queue);
485
486	init_waitqueue_head(&ar->event_queue);
487
488	return ar;
489
490err_wq:
491	ath10k_mac_destroy(ar);
492	return NULL;
493}
494EXPORT_SYMBOL(ath10k_core_create);
495
496void ath10k_core_destroy(struct ath10k *ar)
497{
498	flush_workqueue(ar->workqueue);
499	destroy_workqueue(ar->workqueue);
500
501	ath10k_mac_destroy(ar);
502}
503EXPORT_SYMBOL(ath10k_core_destroy);
504
505
506int ath10k_core_register(struct ath10k *ar)
507{
508	struct bmi_target_info target_info;
509	int status;
510
511	memset(&target_info, 0, sizeof(target_info));
512	status = ath10k_bmi_get_target_info(ar, &target_info);
513	if (status)
514		goto err;
515
516	ar->target_version = target_info.version;
517	ar->hw->wiphy->hw_version = target_info.version;
518
519	status = ath10k_init_hw_params(ar);
520	if (status)
521		goto err;
522
523	if (ath10k_init_configure_target(ar)) {
524		status = -EINVAL;
525		goto err;
526	}
527
528	status = ath10k_init_download_firmware(ar);
529	if (status)
530		goto err;
531
532	status = ath10k_init_uart(ar);
533	if (status)
534		goto err;
535
536	ar->htc.htc_ops.target_send_suspend_complete =
537		ath10k_send_suspend_complete;
538
539	status = ath10k_htc_init(ar);
540	if (status) {
541		ath10k_err("could not init HTC (%d)\n", status);
542		goto err;
543	}
544
545	status = ath10k_bmi_done(ar);
546	if (status)
547		goto err;
548
549	status = ath10k_wmi_attach(ar);
550	if (status) {
551		ath10k_err("WMI attach failed: %d\n", status);
552		goto err;
553	}
554
555	status = ath10k_htc_wait_target(&ar->htc);
556	if (status)
557		goto err_wmi_detach;
558
559	ar->htt = ath10k_htt_attach(ar);
560	if (!ar->htt) {
561		status = -ENOMEM;
562		goto err_wmi_detach;
563	}
564
565	status = ath10k_init_connect_htc(ar);
566	if (status)
567		goto err_htt_detach;
568
569	ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
570
571	status = ath10k_check_fw_version(ar);
572	if (status)
573		goto err_disconnect_htc;
574
575	status = ath10k_wmi_cmd_init(ar);
576	if (status) {
577		ath10k_err("could not send WMI init command (%d)\n", status);
578		goto err_disconnect_htc;
579	}
580
581	status = ath10k_wmi_wait_for_unified_ready(ar);
582	if (status <= 0) {
583		ath10k_err("wmi unified ready event not received\n");
584		status = -ETIMEDOUT;
585		goto err_disconnect_htc;
586	}
587
588	status = ath10k_htt_attach_target(ar->htt);
589	if (status)
590		goto err_disconnect_htc;
591
592	status = ath10k_mac_register(ar);
593	if (status)
594		goto err_disconnect_htc;
595
596	status = ath10k_debug_create(ar);
597	if (status) {
598		ath10k_err("unable to initialize debugfs\n");
599		goto err_unregister_mac;
600	}
601
602	return 0;
603
604err_unregister_mac:
605	ath10k_mac_unregister(ar);
606err_disconnect_htc:
607	ath10k_htc_stop(&ar->htc);
608err_htt_detach:
609	ath10k_htt_detach(ar->htt);
610err_wmi_detach:
611	ath10k_wmi_detach(ar);
612err:
613	return status;
614}
615EXPORT_SYMBOL(ath10k_core_register);
616
617void ath10k_core_unregister(struct ath10k *ar)
618{
619	/* We must unregister from mac80211 before we stop HTC and HIF.
620	 * Otherwise we will fail to submit commands to FW and mac80211 will be
621	 * unhappy about callback failures. */
622	ath10k_mac_unregister(ar);
623	ath10k_htc_stop(&ar->htc);
624	ath10k_htt_detach(ar->htt);
625	ath10k_wmi_detach(ar);
626}
627EXPORT_SYMBOL(ath10k_core_unregister);
628
629int ath10k_core_target_suspend(struct ath10k *ar)
630{
631	int ret;
632
633	ath10k_dbg(ATH10K_DBG_CORE, "%s: called", __func__);
634
635	ret = ath10k_wmi_pdev_suspend_target(ar);
636	if (ret)
637		ath10k_warn("could not suspend target (%d)\n", ret);
638
639	return ret;
640}
641EXPORT_SYMBOL(ath10k_core_target_suspend);
642
643int ath10k_core_target_resume(struct ath10k *ar)
644{
645	int ret;
646
647	ath10k_dbg(ATH10K_DBG_CORE, "%s: called", __func__);
648
649	ret = ath10k_wmi_pdev_resume_target(ar);
650	if (ret)
651		ath10k_warn("could not resume target (%d)\n", ret);
652
653	return ret;
654}
655EXPORT_SYMBOL(ath10k_core_target_resume);
656
657MODULE_AUTHOR("Qualcomm Atheros");
658MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
659MODULE_LICENSE("Dual BSD/GPL");
660