core.c revision 818bdd16b229919cfd07447d261154a1343871e1
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	init_completion(&ar->scan.started);
462	init_completion(&ar->scan.completed);
463	init_completion(&ar->scan.on_channel);
464
465	init_completion(&ar->install_key_done);
466	init_completion(&ar->vdev_setup_done);
467
468	setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
469
470	ar->workqueue = create_singlethread_workqueue("ath10k_wq");
471	if (!ar->workqueue)
472		goto err_wq;
473
474	mutex_init(&ar->conf_mutex);
475	spin_lock_init(&ar->data_lock);
476
477	INIT_LIST_HEAD(&ar->peers);
478	init_waitqueue_head(&ar->peer_mapping_wq);
479
480	init_completion(&ar->offchan_tx_completed);
481	INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
482	skb_queue_head_init(&ar->offchan_tx_queue);
483
484	init_waitqueue_head(&ar->event_queue);
485
486	return ar;
487
488err_wq:
489	ath10k_mac_destroy(ar);
490	return NULL;
491}
492EXPORT_SYMBOL(ath10k_core_create);
493
494void ath10k_core_destroy(struct ath10k *ar)
495{
496	flush_workqueue(ar->workqueue);
497	destroy_workqueue(ar->workqueue);
498
499	ath10k_mac_destroy(ar);
500}
501EXPORT_SYMBOL(ath10k_core_destroy);
502
503int ath10k_core_start(struct ath10k *ar)
504{
505	struct bmi_target_info target_info;
506	int status;
507
508	ath10k_bmi_start(ar);
509
510	memset(&target_info, 0, sizeof(target_info));
511	status = ath10k_bmi_get_target_info(ar, &target_info);
512	if (status)
513		goto err;
514
515	ar->target_version = target_info.version;
516	ar->hw->wiphy->hw_version = target_info.version;
517
518	status = ath10k_init_hw_params(ar);
519	if (status)
520		goto err;
521
522	if (ath10k_init_configure_target(ar)) {
523		status = -EINVAL;
524		goto err;
525	}
526
527	status = ath10k_init_download_firmware(ar);
528	if (status)
529		goto err;
530
531	status = ath10k_init_uart(ar);
532	if (status)
533		goto err;
534
535	ar->htc.htc_ops.target_send_suspend_complete =
536		ath10k_send_suspend_complete;
537
538	status = ath10k_htc_init(ar);
539	if (status) {
540		ath10k_err("could not init HTC (%d)\n", status);
541		goto err;
542	}
543
544	status = ath10k_bmi_done(ar);
545	if (status)
546		goto err;
547
548	status = ath10k_wmi_attach(ar);
549	if (status) {
550		ath10k_err("WMI attach failed: %d\n", status);
551		goto err;
552	}
553
554	status = ath10k_htc_wait_target(&ar->htc);
555	if (status)
556		goto err_wmi_detach;
557
558	status = ath10k_htt_attach(ar);
559	if (status) {
560		ath10k_err("could not attach htt (%d)\n", status);
561		goto err_wmi_detach;
562	}
563
564	status = ath10k_init_connect_htc(ar);
565	if (status)
566		goto err_htt_detach;
567
568	ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
569
570	status = ath10k_check_fw_version(ar);
571	if (status)
572		goto err_disconnect_htc;
573
574	status = ath10k_wmi_cmd_init(ar);
575	if (status) {
576		ath10k_err("could not send WMI init command (%d)\n", status);
577		goto err_disconnect_htc;
578	}
579
580	status = ath10k_wmi_wait_for_unified_ready(ar);
581	if (status <= 0) {
582		ath10k_err("wmi unified ready event not received\n");
583		status = -ETIMEDOUT;
584		goto err_disconnect_htc;
585	}
586
587	status = ath10k_htt_attach_target(&ar->htt);
588	if (status)
589		goto err_disconnect_htc;
590
591	ar->free_vdev_map = (1 << TARGET_NUM_VDEVS) - 1;
592
593	return 0;
594
595err_disconnect_htc:
596	ath10k_htc_stop(&ar->htc);
597err_htt_detach:
598	ath10k_htt_detach(&ar->htt);
599err_wmi_detach:
600	ath10k_wmi_detach(ar);
601err:
602	return status;
603}
604EXPORT_SYMBOL(ath10k_core_start);
605
606void ath10k_core_stop(struct ath10k *ar)
607{
608	ath10k_htc_stop(&ar->htc);
609	ath10k_htt_detach(&ar->htt);
610	ath10k_wmi_detach(ar);
611}
612EXPORT_SYMBOL(ath10k_core_stop);
613
614/* mac80211 manages fw/hw initialization through start/stop hooks. However in
615 * order to know what hw capabilities should be advertised to mac80211 it is
616 * necessary to load the firmware (and tear it down immediately since start
617 * hook will try to init it again) before registering */
618static int ath10k_core_probe_fw(struct ath10k *ar)
619{
620	int ret;
621
622	ret = ath10k_hif_power_up(ar);
623	if (ret) {
624		ath10k_err("could not start pci hif (%d)\n", ret);
625		return ret;
626	}
627
628	ret = ath10k_core_start(ar);
629	if (ret) {
630		ath10k_err("could not init core (%d)\n", ret);
631		ath10k_hif_power_down(ar);
632		return ret;
633	}
634
635	ath10k_core_stop(ar);
636	ath10k_hif_power_down(ar);
637	return 0;
638}
639
640int ath10k_core_register(struct ath10k *ar)
641{
642	int status;
643
644	status = ath10k_core_probe_fw(ar);
645	if (status) {
646		ath10k_err("could not probe fw (%d)\n", status);
647		return status;
648	}
649
650	status = ath10k_mac_register(ar);
651	if (status) {
652		ath10k_err("could not register to mac80211 (%d)\n", status);
653		return status;
654	}
655
656	status = ath10k_debug_create(ar);
657	if (status) {
658		ath10k_err("unable to initialize debugfs\n");
659		goto err_unregister_mac;
660	}
661
662	return 0;
663
664err_unregister_mac:
665	ath10k_mac_unregister(ar);
666	return status;
667}
668EXPORT_SYMBOL(ath10k_core_register);
669
670void ath10k_core_unregister(struct ath10k *ar)
671{
672	/* We must unregister from mac80211 before we stop HTC and HIF.
673	 * Otherwise we will fail to submit commands to FW and mac80211 will be
674	 * unhappy about callback failures. */
675	ath10k_mac_unregister(ar);
676}
677EXPORT_SYMBOL(ath10k_core_unregister);
678
679MODULE_AUTHOR("Qualcomm Atheros");
680MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
681MODULE_LICENSE("Dual BSD/GPL");
682