Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   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_2_0_VERSION,
  43		.name = "qca988x hw2.0",
  44		.patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
  45		.fw = {
  46			.dir = QCA988X_HW_2_0_FW_DIR,
  47			.fw = QCA988X_HW_2_0_FW_FILE,
  48			.otp = QCA988X_HW_2_0_OTP_FILE,
  49			.board = QCA988X_HW_2_0_BOARD_DATA_FILE,
  50		},
  51	},
  52};
  53
  54static void ath10k_send_suspend_complete(struct ath10k *ar)
  55{
  56	ath10k_dbg(ATH10K_DBG_BOOT, "boot suspend complete\n");
  57
  58	complete(&ar->target_suspend);
  59}
  60
  61static int ath10k_init_connect_htc(struct ath10k *ar)
  62{
  63	int status;
  64
  65	status = ath10k_wmi_connect_htc_service(ar);
  66	if (status)
  67		goto conn_fail;
  68
  69	/* Start HTC */
  70	status = ath10k_htc_start(&ar->htc);
  71	if (status)
  72		goto conn_fail;
  73
  74	/* Wait for WMI event to be ready */
  75	status = ath10k_wmi_wait_for_service_ready(ar);
  76	if (status <= 0) {
  77		ath10k_warn("wmi service ready event not received");
  78		status = -ETIMEDOUT;
  79		goto timeout;
  80	}
  81
  82	ath10k_dbg(ATH10K_DBG_BOOT, "boot wmi ready\n");
  83	return 0;
  84
  85timeout:
  86	ath10k_htc_stop(&ar->htc);
  87conn_fail:
  88	return status;
  89}
  90
  91static int ath10k_init_configure_target(struct ath10k *ar)
  92{
  93	u32 param_host;
  94	int ret;
  95
  96	/* tell target which HTC version it is used*/
  97	ret = ath10k_bmi_write32(ar, hi_app_host_interest,
  98				 HTC_PROTOCOL_VERSION);
  99	if (ret) {
 100		ath10k_err("settings HTC version failed\n");
 101		return ret;
 102	}
 103
 104	/* set the firmware mode to STA/IBSS/AP */
 105	ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
 106	if (ret) {
 107		ath10k_err("setting firmware mode (1/2) failed\n");
 108		return ret;
 109	}
 110
 111	/* TODO following parameters need to be re-visited. */
 112	/* num_device */
 113	param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
 114	/* Firmware mode */
 115	/* FIXME: Why FW_MODE_AP ??.*/
 116	param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
 117	/* mac_addr_method */
 118	param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
 119	/* firmware_bridge */
 120	param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
 121	/* fwsubmode */
 122	param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
 123
 124	ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
 125	if (ret) {
 126		ath10k_err("setting firmware mode (2/2) failed\n");
 127		return ret;
 128	}
 129
 130	/* We do all byte-swapping on the host */
 131	ret = ath10k_bmi_write32(ar, hi_be, 0);
 132	if (ret) {
 133		ath10k_err("setting host CPU BE mode failed\n");
 134		return ret;
 135	}
 136
 137	/* FW descriptor/Data swap flags */
 138	ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
 139
 140	if (ret) {
 141		ath10k_err("setting FW data/desc swap flags failed\n");
 142		return ret;
 143	}
 144
 145	return 0;
 146}
 147
 148static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
 149						   const char *dir,
 150						   const char *file)
 151{
 152	char filename[100];
 153	const struct firmware *fw;
 154	int ret;
 155
 156	if (file == NULL)
 157		return ERR_PTR(-ENOENT);
 158
 159	if (dir == NULL)
 160		dir = ".";
 161
 162	snprintf(filename, sizeof(filename), "%s/%s", dir, file);
 163	ret = request_firmware(&fw, filename, ar->dev);
 164	if (ret)
 165		return ERR_PTR(ret);
 166
 167	return fw;
 168}
 169
 170static int ath10k_push_board_ext_data(struct ath10k *ar)
 171{
 172	u32 board_data_size = QCA988X_BOARD_DATA_SZ;
 173	u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
 174	u32 board_ext_data_addr;
 175	int ret;
 176
 177	ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
 178	if (ret) {
 179		ath10k_err("could not read board ext data addr (%d)\n", ret);
 180		return ret;
 181	}
 182
 183	ath10k_dbg(ATH10K_DBG_BOOT,
 184		   "boot push board extended data addr 0x%x\n",
 185		   board_ext_data_addr);
 186
 187	if (board_ext_data_addr == 0)
 188		return 0;
 189
 190	if (ar->board_len != (board_data_size + board_ext_data_size)) {
 191		ath10k_err("invalid board (ext) data sizes %zu != %d+%d\n",
 192			   ar->board_len, board_data_size, board_ext_data_size);
 193		return -EINVAL;
 194	}
 195
 196	ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
 197				      ar->board_data + board_data_size,
 198				      board_ext_data_size);
 199	if (ret) {
 200		ath10k_err("could not write board ext data (%d)\n", ret);
 201		return ret;
 202	}
 203
 204	ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
 205				 (board_ext_data_size << 16) | 1);
 206	if (ret) {
 207		ath10k_err("could not write board ext data bit (%d)\n", ret);
 208		return ret;
 209	}
 210
 211	return 0;
 212}
 213
 214static int ath10k_download_board_data(struct ath10k *ar)
 215{
 216	u32 board_data_size = QCA988X_BOARD_DATA_SZ;
 217	u32 address;
 218	int ret;
 219
 220	ret = ath10k_push_board_ext_data(ar);
 221	if (ret) {
 222		ath10k_err("could not push board ext data (%d)\n", ret);
 223		goto exit;
 224	}
 225
 226	ret = ath10k_bmi_read32(ar, hi_board_data, &address);
 227	if (ret) {
 228		ath10k_err("could not read board data addr (%d)\n", ret);
 229		goto exit;
 230	}
 231
 232	ret = ath10k_bmi_write_memory(ar, address, ar->board_data,
 233				      min_t(u32, board_data_size,
 234					    ar->board_len));
 235	if (ret) {
 236		ath10k_err("could not write board data (%d)\n", ret);
 237		goto exit;
 238	}
 239
 240	ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
 241	if (ret) {
 242		ath10k_err("could not write board data bit (%d)\n", ret);
 243		goto exit;
 244	}
 245
 246exit:
 247	return ret;
 248}
 249
 250static int ath10k_download_and_run_otp(struct ath10k *ar)
 251{
 252	u32 address = ar->hw_params.patch_load_addr;
 253	u32 exec_param;
 254	int ret;
 255
 256	/* OTP is optional */
 257
 258	if (!ar->otp_data || !ar->otp_len)
 259		return 0;
 260
 261	ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
 262	if (ret) {
 263		ath10k_err("could not write otp (%d)\n", ret);
 264		goto exit;
 265	}
 266
 267	exec_param = 0;
 268	ret = ath10k_bmi_execute(ar, address, &exec_param);
 269	if (ret) {
 270		ath10k_err("could not execute otp (%d)\n", ret);
 271		goto exit;
 272	}
 273
 274exit:
 275	return ret;
 276}
 277
 278static int ath10k_download_fw(struct ath10k *ar)
 279{
 280	u32 address;
 281	int ret;
 282
 283	address = ar->hw_params.patch_load_addr;
 284
 285	ret = ath10k_bmi_fast_download(ar, address, ar->firmware_data,
 286				       ar->firmware_len);
 287	if (ret) {
 288		ath10k_err("could not write fw (%d)\n", ret);
 289		goto exit;
 290	}
 291
 292exit:
 293	return ret;
 294}
 295
 296static void ath10k_core_free_firmware_files(struct ath10k *ar)
 297{
 298	if (ar->board && !IS_ERR(ar->board))
 299		release_firmware(ar->board);
 300
 301	if (ar->otp && !IS_ERR(ar->otp))
 302		release_firmware(ar->otp);
 303
 304	if (ar->firmware && !IS_ERR(ar->firmware))
 305		release_firmware(ar->firmware);
 306
 307	ar->board = NULL;
 308	ar->board_data = NULL;
 309	ar->board_len = 0;
 310
 311	ar->otp = NULL;
 312	ar->otp_data = NULL;
 313	ar->otp_len = 0;
 314
 315	ar->firmware = NULL;
 316	ar->firmware_data = NULL;
 317	ar->firmware_len = 0;
 318}
 319
 320static int ath10k_core_fetch_firmware_api_1(struct ath10k *ar)
 321{
 322	int ret = 0;
 323
 324	if (ar->hw_params.fw.fw == NULL) {
 325		ath10k_err("firmware file not defined\n");
 326		return -EINVAL;
 327	}
 328
 329	if (ar->hw_params.fw.board == NULL) {
 330		ath10k_err("board data file not defined");
 331		return -EINVAL;
 332	}
 333
 334	ar->board = ath10k_fetch_fw_file(ar,
 335					 ar->hw_params.fw.dir,
 336					 ar->hw_params.fw.board);
 337	if (IS_ERR(ar->board)) {
 338		ret = PTR_ERR(ar->board);
 339		ath10k_err("could not fetch board data (%d)\n", ret);
 340		goto err;
 341	}
 342
 343	ar->board_data = ar->board->data;
 344	ar->board_len = ar->board->size;
 345
 346	ar->firmware = ath10k_fetch_fw_file(ar,
 347					    ar->hw_params.fw.dir,
 348					    ar->hw_params.fw.fw);
 349	if (IS_ERR(ar->firmware)) {
 350		ret = PTR_ERR(ar->firmware);
 351		ath10k_err("could not fetch firmware (%d)\n", ret);
 352		goto err;
 353	}
 354
 355	ar->firmware_data = ar->firmware->data;
 356	ar->firmware_len = ar->firmware->size;
 357
 358	/* OTP may be undefined. If so, don't fetch it at all */
 359	if (ar->hw_params.fw.otp == NULL)
 360		return 0;
 361
 362	ar->otp = ath10k_fetch_fw_file(ar,
 363				       ar->hw_params.fw.dir,
 364				       ar->hw_params.fw.otp);
 365	if (IS_ERR(ar->otp)) {
 366		ret = PTR_ERR(ar->otp);
 367		ath10k_err("could not fetch otp (%d)\n", ret);
 368		goto err;
 369	}
 370
 371	ar->otp_data = ar->otp->data;
 372	ar->otp_len = ar->otp->size;
 373
 374	return 0;
 375
 376err:
 377	ath10k_core_free_firmware_files(ar);
 378	return ret;
 379}
 380
 381static int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name)
 382{
 383	size_t magic_len, len, ie_len;
 384	int ie_id, i, index, bit, ret;
 385	struct ath10k_fw_ie *hdr;
 386	const u8 *data;
 387	__le32 *timestamp;
 388
 389	/* first fetch the firmware file (firmware-*.bin) */
 390	ar->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, name);
 391	if (IS_ERR(ar->firmware)) {
 392		ath10k_err("Could not fetch firmware file '%s': %ld\n",
 393			   name, PTR_ERR(ar->firmware));
 394		return PTR_ERR(ar->firmware);
 395	}
 396
 397	data = ar->firmware->data;
 398	len = ar->firmware->size;
 399
 400	/* magic also includes the null byte, check that as well */
 401	magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
 402
 403	if (len < magic_len) {
 404		ath10k_err("firmware image too small to contain magic: %zu\n",
 405			   len);
 406		ret = -EINVAL;
 407		goto err;
 408	}
 409
 410	if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
 411		ath10k_err("Invalid firmware magic\n");
 412		ret = -EINVAL;
 413		goto err;
 414	}
 415
 416	/* jump over the padding */
 417	magic_len = ALIGN(magic_len, 4);
 418
 419	len -= magic_len;
 420	data += magic_len;
 421
 422	/* loop elements */
 423	while (len > sizeof(struct ath10k_fw_ie)) {
 424		hdr = (struct ath10k_fw_ie *)data;
 425
 426		ie_id = le32_to_cpu(hdr->id);
 427		ie_len = le32_to_cpu(hdr->len);
 428
 429		len -= sizeof(*hdr);
 430		data += sizeof(*hdr);
 431
 432		if (len < ie_len) {
 433			ath10k_err("Invalid length for FW IE %d (%zu < %zu)\n",
 434				   ie_id, len, ie_len);
 435			ret = -EINVAL;
 436			goto err;
 437		}
 438
 439		switch (ie_id) {
 440		case ATH10K_FW_IE_FW_VERSION:
 441			if (ie_len > sizeof(ar->hw->wiphy->fw_version) - 1)
 442				break;
 443
 444			memcpy(ar->hw->wiphy->fw_version, data, ie_len);
 445			ar->hw->wiphy->fw_version[ie_len] = '\0';
 446
 447			ath10k_dbg(ATH10K_DBG_BOOT,
 448				   "found fw version %s\n",
 449				    ar->hw->wiphy->fw_version);
 450			break;
 451		case ATH10K_FW_IE_TIMESTAMP:
 452			if (ie_len != sizeof(u32))
 453				break;
 454
 455			timestamp = (__le32 *)data;
 456
 457			ath10k_dbg(ATH10K_DBG_BOOT, "found fw timestamp %d\n",
 458				   le32_to_cpup(timestamp));
 459			break;
 460		case ATH10K_FW_IE_FEATURES:
 461			ath10k_dbg(ATH10K_DBG_BOOT,
 462				   "found firmware features ie (%zd B)\n",
 463				   ie_len);
 464
 465			for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
 466				index = i / 8;
 467				bit = i % 8;
 468
 469				if (index == ie_len)
 470					break;
 471
 472				if (data[index] & (1 << bit)) {
 473					ath10k_dbg(ATH10K_DBG_BOOT,
 474						   "Enabling feature bit: %i\n",
 475						   i);
 476					__set_bit(i, ar->fw_features);
 477				}
 478			}
 479
 480			ath10k_dbg_dump(ATH10K_DBG_BOOT, "features", "",
 481					ar->fw_features,
 482					sizeof(ar->fw_features));
 483			break;
 484		case ATH10K_FW_IE_FW_IMAGE:
 485			ath10k_dbg(ATH10K_DBG_BOOT,
 486				   "found fw image ie (%zd B)\n",
 487				   ie_len);
 488
 489			ar->firmware_data = data;
 490			ar->firmware_len = ie_len;
 491
 492			break;
 493		case ATH10K_FW_IE_OTP_IMAGE:
 494			ath10k_dbg(ATH10K_DBG_BOOT,
 495				   "found otp image ie (%zd B)\n",
 496				   ie_len);
 497
 498			ar->otp_data = data;
 499			ar->otp_len = ie_len;
 500
 501			break;
 502		default:
 503			ath10k_warn("Unknown FW IE: %u\n",
 504				    le32_to_cpu(hdr->id));
 505			break;
 506		}
 507
 508		/* jump over the padding */
 509		ie_len = ALIGN(ie_len, 4);
 510
 511		len -= ie_len;
 512		data += ie_len;
 513	}
 514
 515	if (!ar->firmware_data || !ar->firmware_len) {
 516		ath10k_warn("No ATH10K_FW_IE_FW_IMAGE found from %s, skipping\n",
 517			    name);
 518		ret = -ENOMEDIUM;
 519		goto err;
 520	}
 521
 522	/* now fetch the board file */
 523	if (ar->hw_params.fw.board == NULL) {
 524		ath10k_err("board data file not defined");
 525		ret = -EINVAL;
 526		goto err;
 527	}
 528
 529	ar->board = ath10k_fetch_fw_file(ar,
 530					 ar->hw_params.fw.dir,
 531					 ar->hw_params.fw.board);
 532	if (IS_ERR(ar->board)) {
 533		ret = PTR_ERR(ar->board);
 534		ath10k_err("could not fetch board data (%d)\n", ret);
 535		goto err;
 536	}
 537
 538	ar->board_data = ar->board->data;
 539	ar->board_len = ar->board->size;
 540
 541	return 0;
 542
 543err:
 544	ath10k_core_free_firmware_files(ar);
 545	return ret;
 546}
 547
 548static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
 549{
 550	int ret;
 551
 552	ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API2_FILE);
 553	if (ret == 0) {
 554		ar->fw_api = 2;
 555		goto out;
 556	}
 557
 558	ret = ath10k_core_fetch_firmware_api_1(ar);
 559	if (ret)
 560		return ret;
 561
 562	ar->fw_api = 1;
 563
 564out:
 565	ath10k_dbg(ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
 566
 567	return 0;
 568}
 569
 570static int ath10k_init_download_firmware(struct ath10k *ar)
 571{
 572	int ret;
 573
 574	ret = ath10k_download_board_data(ar);
 575	if (ret)
 576		return ret;
 577
 578	ret = ath10k_download_and_run_otp(ar);
 579	if (ret)
 580		return ret;
 581
 582	ret = ath10k_download_fw(ar);
 583	if (ret)
 584		return ret;
 585
 586	return ret;
 587}
 588
 589static int ath10k_init_uart(struct ath10k *ar)
 590{
 591	int ret;
 592
 593	/*
 594	 * Explicitly setting UART prints to zero as target turns it on
 595	 * based on scratch registers.
 596	 */
 597	ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
 598	if (ret) {
 599		ath10k_warn("could not disable UART prints (%d)\n", ret);
 600		return ret;
 601	}
 602
 603	if (!uart_print)
 604		return 0;
 605
 606	ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
 607	if (ret) {
 608		ath10k_warn("could not enable UART prints (%d)\n", ret);
 609		return ret;
 610	}
 611
 612	ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
 613	if (ret) {
 614		ath10k_warn("could not enable UART prints (%d)\n", ret);
 615		return ret;
 616	}
 617
 618	/* Set the UART baud rate to 19200. */
 619	ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
 620	if (ret) {
 621		ath10k_warn("could not set the baud rate (%d)\n", ret);
 622		return ret;
 623	}
 624
 625	ath10k_info("UART prints enabled\n");
 626	return 0;
 627}
 628
 629static int ath10k_init_hw_params(struct ath10k *ar)
 630{
 631	const struct ath10k_hw_params *uninitialized_var(hw_params);
 632	int i;
 633
 634	for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
 635		hw_params = &ath10k_hw_params_list[i];
 636
 637		if (hw_params->id == ar->target_version)
 638			break;
 639	}
 640
 641	if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
 642		ath10k_err("Unsupported hardware version: 0x%x\n",
 643			   ar->target_version);
 644		return -EINVAL;
 645	}
 646
 647	ar->hw_params = *hw_params;
 648
 649	ath10k_dbg(ATH10K_DBG_BOOT, "Hardware name %s version 0x%x\n",
 650		   ar->hw_params.name, ar->target_version);
 651
 652	return 0;
 653}
 654
 655static void ath10k_core_restart(struct work_struct *work)
 656{
 657	struct ath10k *ar = container_of(work, struct ath10k, restart_work);
 658
 659	mutex_lock(&ar->conf_mutex);
 660
 661	switch (ar->state) {
 662	case ATH10K_STATE_ON:
 663		ath10k_halt(ar);
 664		ar->state = ATH10K_STATE_RESTARTING;
 665		ieee80211_restart_hw(ar->hw);
 666		break;
 667	case ATH10K_STATE_OFF:
 668		/* this can happen if driver is being unloaded
 669		 * or if the crash happens during FW probing */
 670		ath10k_warn("cannot restart a device that hasn't been started\n");
 671		break;
 672	case ATH10K_STATE_RESTARTING:
 673	case ATH10K_STATE_RESTARTED:
 674		ar->state = ATH10K_STATE_WEDGED;
 675		/* fall through */
 676	case ATH10K_STATE_WEDGED:
 677		ath10k_warn("device is wedged, will not restart\n");
 678		break;
 679	}
 680
 681	mutex_unlock(&ar->conf_mutex);
 682}
 683
 684struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
 685				  const struct ath10k_hif_ops *hif_ops)
 686{
 687	struct ath10k *ar;
 688
 689	ar = ath10k_mac_create();
 690	if (!ar)
 691		return NULL;
 692
 693	ar->ath_common.priv = ar;
 694	ar->ath_common.hw = ar->hw;
 695
 696	ar->p2p = !!ath10k_p2p;
 697	ar->dev = dev;
 698
 699	ar->hif.priv = hif_priv;
 700	ar->hif.ops = hif_ops;
 701
 702	init_completion(&ar->scan.started);
 703	init_completion(&ar->scan.completed);
 704	init_completion(&ar->scan.on_channel);
 705	init_completion(&ar->target_suspend);
 706
 707	init_completion(&ar->install_key_done);
 708	init_completion(&ar->vdev_setup_done);
 709
 710	setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
 711
 712	ar->workqueue = create_singlethread_workqueue("ath10k_wq");
 713	if (!ar->workqueue)
 714		goto err_wq;
 715
 716	mutex_init(&ar->conf_mutex);
 717	spin_lock_init(&ar->data_lock);
 718
 719	INIT_LIST_HEAD(&ar->peers);
 720	init_waitqueue_head(&ar->peer_mapping_wq);
 721
 722	init_completion(&ar->offchan_tx_completed);
 723	INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
 724	skb_queue_head_init(&ar->offchan_tx_queue);
 725
 726	INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
 727	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
 728
 729	INIT_WORK(&ar->restart_work, ath10k_core_restart);
 730
 731	return ar;
 732
 733err_wq:
 734	ath10k_mac_destroy(ar);
 735	return NULL;
 736}
 737EXPORT_SYMBOL(ath10k_core_create);
 738
 739void ath10k_core_destroy(struct ath10k *ar)
 740{
 741	flush_workqueue(ar->workqueue);
 742	destroy_workqueue(ar->workqueue);
 743
 744	ath10k_mac_destroy(ar);
 745}
 746EXPORT_SYMBOL(ath10k_core_destroy);
 747
 748int ath10k_core_start(struct ath10k *ar)
 749{
 750	int status;
 751
 752	lockdep_assert_held(&ar->conf_mutex);
 753
 754	ath10k_bmi_start(ar);
 755
 756	if (ath10k_init_configure_target(ar)) {
 757		status = -EINVAL;
 758		goto err;
 759	}
 760
 761	status = ath10k_init_download_firmware(ar);
 762	if (status)
 763		goto err;
 764
 765	status = ath10k_init_uart(ar);
 766	if (status)
 767		goto err;
 768
 769	ar->htc.htc_ops.target_send_suspend_complete =
 770		ath10k_send_suspend_complete;
 771
 772	status = ath10k_htc_init(ar);
 773	if (status) {
 774		ath10k_err("could not init HTC (%d)\n", status);
 775		goto err;
 776	}
 777
 778	status = ath10k_bmi_done(ar);
 779	if (status)
 780		goto err;
 781
 782	status = ath10k_wmi_attach(ar);
 783	if (status) {
 784		ath10k_err("WMI attach failed: %d\n", status);
 785		goto err;
 786	}
 787
 788	status = ath10k_hif_start(ar);
 789	if (status) {
 790		ath10k_err("could not start HIF: %d\n", status);
 791		goto err_wmi_detach;
 792	}
 793
 794	status = ath10k_htc_wait_target(&ar->htc);
 795	if (status) {
 796		ath10k_err("failed to connect to HTC: %d\n", status);
 797		goto err_hif_stop;
 798	}
 799
 800	status = ath10k_htt_attach(ar);
 801	if (status) {
 802		ath10k_err("could not attach htt (%d)\n", status);
 803		goto err_hif_stop;
 804	}
 805
 806	status = ath10k_init_connect_htc(ar);
 807	if (status)
 808		goto err_htt_detach;
 809
 810	ath10k_dbg(ATH10K_DBG_BOOT, "firmware %s booted\n",
 811		   ar->hw->wiphy->fw_version);
 812
 813	status = ath10k_wmi_cmd_init(ar);
 814	if (status) {
 815		ath10k_err("could not send WMI init command (%d)\n", status);
 816		goto err_disconnect_htc;
 817	}
 818
 819	status = ath10k_wmi_wait_for_unified_ready(ar);
 820	if (status <= 0) {
 821		ath10k_err("wmi unified ready event not received\n");
 822		status = -ETIMEDOUT;
 823		goto err_disconnect_htc;
 824	}
 825
 826	status = ath10k_htt_attach_target(&ar->htt);
 827	if (status)
 828		goto err_disconnect_htc;
 829
 830	status = ath10k_debug_start(ar);
 831	if (status)
 832		goto err_disconnect_htc;
 833
 834	ar->free_vdev_map = (1 << TARGET_NUM_VDEVS) - 1;
 835	INIT_LIST_HEAD(&ar->arvifs);
 836
 837	if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
 838		ath10k_info("%s (0x%x) fw %s api %d htt %d.%d\n",
 839			    ar->hw_params.name, ar->target_version,
 840			    ar->hw->wiphy->fw_version, ar->fw_api,
 841			    ar->htt.target_version_major,
 842			    ar->htt.target_version_minor);
 843
 844	__set_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags);
 845
 846	return 0;
 847
 848err_disconnect_htc:
 849	ath10k_htc_stop(&ar->htc);
 850err_htt_detach:
 851	ath10k_htt_detach(&ar->htt);
 852err_hif_stop:
 853	ath10k_hif_stop(ar);
 854err_wmi_detach:
 855	ath10k_wmi_detach(ar);
 856err:
 857	return status;
 858}
 859EXPORT_SYMBOL(ath10k_core_start);
 860
 861int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt)
 862{
 863	int ret;
 864
 865	reinit_completion(&ar->target_suspend);
 866
 867	ret = ath10k_wmi_pdev_suspend_target(ar, suspend_opt);
 868	if (ret) {
 869		ath10k_warn("could not suspend target (%d)\n", ret);
 870		return ret;
 871	}
 872
 873	ret = wait_for_completion_timeout(&ar->target_suspend, 1 * HZ);
 874
 875	if (ret == 0) {
 876		ath10k_warn("suspend timed out - target pause event never came\n");
 877		return -ETIMEDOUT;
 878	}
 879
 880	return 0;
 881}
 882
 883void ath10k_core_stop(struct ath10k *ar)
 884{
 885	lockdep_assert_held(&ar->conf_mutex);
 886
 887	/* try to suspend target */
 888	ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND_AND_DISABLE_INTR);
 889	ath10k_debug_stop(ar);
 890	ath10k_htc_stop(&ar->htc);
 891	ath10k_htt_detach(&ar->htt);
 892	ath10k_wmi_detach(ar);
 893}
 894EXPORT_SYMBOL(ath10k_core_stop);
 895
 896/* mac80211 manages fw/hw initialization through start/stop hooks. However in
 897 * order to know what hw capabilities should be advertised to mac80211 it is
 898 * necessary to load the firmware (and tear it down immediately since start
 899 * hook will try to init it again) before registering */
 900static int ath10k_core_probe_fw(struct ath10k *ar)
 901{
 902	struct bmi_target_info target_info;
 903	int ret = 0;
 904
 905	ret = ath10k_hif_power_up(ar);
 906	if (ret) {
 907		ath10k_err("could not start pci hif (%d)\n", ret);
 908		return ret;
 909	}
 910
 911	memset(&target_info, 0, sizeof(target_info));
 912	ret = ath10k_bmi_get_target_info(ar, &target_info);
 913	if (ret) {
 914		ath10k_err("could not get target info (%d)\n", ret);
 915		ath10k_hif_power_down(ar);
 916		return ret;
 917	}
 918
 919	ar->target_version = target_info.version;
 920	ar->hw->wiphy->hw_version = target_info.version;
 921
 922	ret = ath10k_init_hw_params(ar);
 923	if (ret) {
 924		ath10k_err("could not get hw params (%d)\n", ret);
 925		ath10k_hif_power_down(ar);
 926		return ret;
 927	}
 928
 929	ret = ath10k_core_fetch_firmware_files(ar);
 930	if (ret) {
 931		ath10k_err("could not fetch firmware files (%d)\n", ret);
 932		ath10k_hif_power_down(ar);
 933		return ret;
 934	}
 935
 936	mutex_lock(&ar->conf_mutex);
 937
 938	ret = ath10k_core_start(ar);
 939	if (ret) {
 940		ath10k_err("could not init core (%d)\n", ret);
 941		ath10k_core_free_firmware_files(ar);
 942		ath10k_hif_power_down(ar);
 943		mutex_unlock(&ar->conf_mutex);
 944		return ret;
 945	}
 946
 947	ath10k_core_stop(ar);
 948
 949	mutex_unlock(&ar->conf_mutex);
 950
 951	ath10k_hif_power_down(ar);
 952	return 0;
 953}
 954
 955static int ath10k_core_check_chip_id(struct ath10k *ar)
 956{
 957	u32 hw_revision = MS(ar->chip_id, SOC_CHIP_ID_REV);
 958
 959	ath10k_dbg(ATH10K_DBG_BOOT, "boot chip_id 0x%08x hw_revision 0x%x\n",
 960		   ar->chip_id, hw_revision);
 961
 962	/* Check that we are not using hw1.0 (some of them have same pci id
 963	 * as hw2.0) before doing anything else as ath10k crashes horribly
 964	 * due to missing hw1.0 workarounds. */
 965	switch (hw_revision) {
 966	case QCA988X_HW_1_0_CHIP_ID_REV:
 967		ath10k_err("ERROR: qca988x hw1.0 is not supported\n");
 968		return -EOPNOTSUPP;
 969
 970	case QCA988X_HW_2_0_CHIP_ID_REV:
 971		/* known hardware revision, continue normally */
 972		return 0;
 973
 974	default:
 975		ath10k_warn("Warning: hardware revision unknown (0x%x), expect problems\n",
 976			    ar->chip_id);
 977		return 0;
 978	}
 979
 980	return 0;
 981}
 982
 983int ath10k_core_register(struct ath10k *ar, u32 chip_id)
 984{
 985	int status;
 986
 987	ar->chip_id = chip_id;
 988
 989	status = ath10k_core_check_chip_id(ar);
 990	if (status) {
 991		ath10k_err("Unsupported chip id 0x%08x\n", ar->chip_id);
 992		return status;
 993	}
 994
 995	status = ath10k_core_probe_fw(ar);
 996	if (status) {
 997		ath10k_err("could not probe fw (%d)\n", status);
 998		return status;
 999	}
1000
1001	status = ath10k_mac_register(ar);
1002	if (status) {
1003		ath10k_err("could not register to mac80211 (%d)\n", status);
1004		goto err_release_fw;
1005	}
1006
1007	status = ath10k_debug_create(ar);
1008	if (status) {
1009		ath10k_err("unable to initialize debugfs\n");
1010		goto err_unregister_mac;
1011	}
1012
1013	return 0;
1014
1015err_unregister_mac:
1016	ath10k_mac_unregister(ar);
1017err_release_fw:
1018	ath10k_core_free_firmware_files(ar);
1019	return status;
1020}
1021EXPORT_SYMBOL(ath10k_core_register);
1022
1023void ath10k_core_unregister(struct ath10k *ar)
1024{
1025	/* We must unregister from mac80211 before we stop HTC and HIF.
1026	 * Otherwise we will fail to submit commands to FW and mac80211 will be
1027	 * unhappy about callback failures. */
1028	ath10k_mac_unregister(ar);
1029
1030	ath10k_core_free_firmware_files(ar);
1031
1032	ath10k_debug_destroy(ar);
1033}
1034EXPORT_SYMBOL(ath10k_core_unregister);
1035
1036MODULE_AUTHOR("Qualcomm Atheros");
1037MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
1038MODULE_LICENSE("Dual BSD/GPL");