init.c 44.9 KB
Newer Older
K
Kalle Valo 已提交
1 2 3

/*
 * Copyright (c) 2011 Atheros Communications Inc.
4
 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
K
Kalle Valo 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

19 20
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

21
#include <linux/moduleparam.h>
22
#include <linux/errno.h>
K
Kalle Valo 已提交
23
#include <linux/export.h>
24
#include <linux/of.h>
K
Kalle Valo 已提交
25
#include <linux/mmc/sdio_func.h>
26
#include <linux/vmalloc.h>
K
Kalle Valo 已提交
27

K
Kalle Valo 已提交
28 29 30 31 32
#include "core.h"
#include "cfg80211.h"
#include "target.h"
#include "debug.h"
#include "hif-ops.h"
K
Kalle Valo 已提交
33
#include "htc-ops.h"
K
Kalle Valo 已提交
34

35 36
static const struct ath6kl_hw hw_list[] = {
	{
37
		.id				= AR6003_HW_2_0_VERSION,
38
		.name				= "ar6003 hw 2.0",
39 40 41 42
		.dataset_patch_addr		= 0x57e884,
		.app_load_addr			= 0x543180,
		.board_ext_data_addr		= 0x57e500,
		.reserved_ram_size		= 6912,
43 44
		.refclk_hz			= 26000000,
		.uarttx_pin			= 8,
45
		.flags				= ATH6KL_HW_SDIO_CRC_ERROR_WAR,
46 47 48

		/* hw2.0 needs override address hardcoded */
		.app_start_override_addr	= 0x944C00,
49

50 51 52 53 54 55 56 57
		.fw = {
			.dir		= AR6003_HW_2_0_FW_DIR,
			.otp		= AR6003_HW_2_0_OTP_FILE,
			.fw		= AR6003_HW_2_0_FIRMWARE_FILE,
			.tcmd		= AR6003_HW_2_0_TCMD_FIRMWARE_FILE,
			.patch		= AR6003_HW_2_0_PATCH_FILE,
		},

58 59
		.fw_board		= AR6003_HW_2_0_BOARD_DATA_FILE,
		.fw_default_board	= AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE,
60 61
	},
	{
62
		.id				= AR6003_HW_2_1_1_VERSION,
63
		.name				= "ar6003 hw 2.1.1",
64 65 66 67
		.dataset_patch_addr		= 0x57ff74,
		.app_load_addr			= 0x1234,
		.board_ext_data_addr		= 0x542330,
		.reserved_ram_size		= 512,
68 69
		.refclk_hz			= 26000000,
		.uarttx_pin			= 8,
A
Alex Yang 已提交
70
		.testscript_addr		= 0x57ef74,
71
		.flags				= ATH6KL_HW_SDIO_CRC_ERROR_WAR,
72

73 74 75 76 77 78
		.fw = {
			.dir		= AR6003_HW_2_1_1_FW_DIR,
			.otp		= AR6003_HW_2_1_1_OTP_FILE,
			.fw		= AR6003_HW_2_1_1_FIRMWARE_FILE,
			.tcmd		= AR6003_HW_2_1_1_TCMD_FIRMWARE_FILE,
			.patch		= AR6003_HW_2_1_1_PATCH_FILE,
A
Alex Yang 已提交
79 80
			.utf		= AR6003_HW_2_1_1_UTF_FIRMWARE_FILE,
			.testscript	= AR6003_HW_2_1_1_TESTSCRIPT_FILE,
81 82
		},

83
		.fw_board		= AR6003_HW_2_1_1_BOARD_DATA_FILE,
K
Kalle Valo 已提交
84
		.fw_default_board = AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE,
85 86
	},
	{
87
		.id				= AR6004_HW_1_0_VERSION,
88
		.name				= "ar6004 hw 1.0",
89 90 91 92
		.dataset_patch_addr		= 0x57e884,
		.app_load_addr			= 0x1234,
		.board_ext_data_addr		= 0x437000,
		.reserved_ram_size		= 19456,
93
		.board_addr			= 0x433900,
94 95
		.refclk_hz			= 26000000,
		.uarttx_pin			= 11,
96
		.flags				= 0,
97

98 99 100 101 102
		.fw = {
			.dir		= AR6004_HW_1_0_FW_DIR,
			.fw		= AR6004_HW_1_0_FIRMWARE_FILE,
		},

103 104
		.fw_board		= AR6004_HW_1_0_BOARD_DATA_FILE,
		.fw_default_board	= AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE,
105 106
	},
	{
107
		.id				= AR6004_HW_1_1_VERSION,
108
		.name				= "ar6004 hw 1.1",
109 110 111 112
		.dataset_patch_addr		= 0x57e884,
		.app_load_addr			= 0x1234,
		.board_ext_data_addr		= 0x437000,
		.reserved_ram_size		= 11264,
113
		.board_addr			= 0x43d400,
114 115
		.refclk_hz			= 40000000,
		.uarttx_pin			= 11,
116
		.flags				= 0,
117 118 119 120 121
		.fw = {
			.dir		= AR6004_HW_1_1_FW_DIR,
			.fw		= AR6004_HW_1_1_FIRMWARE_FILE,
		},

122 123
		.fw_board		= AR6004_HW_1_1_BOARD_DATA_FILE,
		.fw_default_board	= AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE,
124
	},
125 126 127 128 129 130 131 132 133 134
	{
		.id				= AR6004_HW_1_2_VERSION,
		.name				= "ar6004 hw 1.2",
		.dataset_patch_addr		= 0x436ecc,
		.app_load_addr			= 0x1234,
		.board_ext_data_addr		= 0x437000,
		.reserved_ram_size		= 9216,
		.board_addr			= 0x435c00,
		.refclk_hz			= 40000000,
		.uarttx_pin			= 11,
135
		.flags				= 0,
136 137 138 139 140 141 142 143

		.fw = {
			.dir		= AR6004_HW_1_2_FW_DIR,
			.fw		= AR6004_HW_1_2_FIRMWARE_FILE,
		},
		.fw_board		= AR6004_HW_1_2_BOARD_DATA_FILE,
		.fw_default_board	= AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE,
	},
144 145 146 147 148 149 150 151 152 153
	{
		.id				= AR6004_HW_1_3_VERSION,
		.name				= "ar6004 hw 1.3",
		.dataset_patch_addr		= 0x437860,
		.app_load_addr			= 0x1234,
		.board_ext_data_addr		= 0x437000,
		.reserved_ram_size		= 7168,
		.board_addr			= 0x436400,
		.refclk_hz                      = 40000000,
		.uarttx_pin                     = 11,
154
		.flags				= 0,
155 156 157 158 159 160 161 162 163

		.fw = {
			.dir            = AR6004_HW_1_3_FW_DIR,
			.fw             = AR6004_HW_1_3_FIRMWARE_FILE,
		},

		.fw_board               = AR6004_HW_1_3_BOARD_DATA_FILE,
		.fw_default_board       = AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE,
	},
164 165
};

K
Kalle Valo 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/*
 * Include definitions here that can be used to tune the WLAN module
 * behavior. Different customers can tune the behavior as per their needs,
 * here.
 */

/*
 * This configuration item enable/disable keepalive support.
 * Keepalive support: In the absence of any data traffic to AP, null
 * frames will be sent to the AP at periodic interval, to keep the association
 * active. This configuration item defines the periodic interval.
 * Use value of zero to disable keepalive support
 * Default: 60 seconds
 */
#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60

/*
 * This configuration item sets the value of disconnect timeout
 * Firmware delays sending the disconnec event to the host for this
 * timeout after is gets disconnected from the current AP.
 * If the firmware successly roams within the disconnect timeout
 * it sends a new connect event
 */
#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10


#define ATH6KL_DATA_OFFSET    64
struct sk_buff *ath6kl_buf_alloc(int size)
{
	struct sk_buff *skb;
	u16 reserved;

	/* Add chacheline space at front and back of buffer */
199 200
	reserved = roundup((2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
		   sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES, 4);
K
Kalle Valo 已提交
201 202 203 204 205 206 207
	skb = dev_alloc_skb(size + reserved);

	if (skb)
		skb_reserve(skb, reserved - L1_CACHE_BYTES);
	return skb;
}

208
void ath6kl_init_profile_info(struct ath6kl_vif *vif)
K
Kalle Valo 已提交
209
{
210 211 212 213 214 215 216 217 218
	vif->ssid_len = 0;
	memset(vif->ssid, 0, sizeof(vif->ssid));

	vif->dot11_auth_mode = OPEN_AUTH;
	vif->auth_mode = NONE_AUTH;
	vif->prwise_crypto = NONE_CRYPT;
	vif->prwise_crypto_len = 0;
	vif->grp_crypto = NONE_CRYPT;
	vif->grp_crypto_len = 0;
219
	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
220 221
	memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
	memset(vif->bssid, 0, sizeof(vif->bssid));
222
	vif->bss_ch = 0;
K
Kalle Valo 已提交
223 224 225 226 227 228 229 230 231 232
}

static int ath6kl_set_host_app_area(struct ath6kl *ar)
{
	u32 address, data;
	struct host_app_area host_app_area;

	/* Fetch the address of the host_app_area_s
	 * instance in the host interest area */
	address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
233
	address = TARG_VTOP(ar->target_type, address);
K
Kalle Valo 已提交
234

235
	if (ath6kl_diag_read32(ar, address, &data))
K
Kalle Valo 已提交
236 237
		return -EIO;

238
	address = TARG_VTOP(ar->target_type, data);
239
	host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
240 241
	if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
			      sizeof(struct host_app_area)))
K
Kalle Valo 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		return -EIO;

	return 0;
}

static inline void set_ac2_ep_map(struct ath6kl *ar,
				  u8 ac,
				  enum htc_endpoint_id ep)
{
	ar->ac2ep_map[ac] = ep;
	ar->ep2ac_map[ep] = ac;
}

/* connect to a service */
static int ath6kl_connectservice(struct ath6kl *ar,
				 struct htc_service_connect_req  *con_req,
				 char *desc)
{
	int status;
	struct htc_service_connect_resp response;

	memset(&response, 0, sizeof(response));

265
	status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
K
Kalle Valo 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	if (status) {
		ath6kl_err("failed to connect to %s service status:%d\n",
			   desc, status);
		return status;
	}

	switch (con_req->svc_id) {
	case WMI_CONTROL_SVC:
		if (test_bit(WMI_ENABLED, &ar->flag))
			ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
		ar->ctrl_ep = response.endpoint;
		break;
	case WMI_DATA_BE_SVC:
		set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
		break;
	case WMI_DATA_BK_SVC:
		set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
		break;
	case WMI_DATA_VI_SVC:
		set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
		break;
	case WMI_DATA_VO_SVC:
		set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
		break;
	default:
		ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
		return -EINVAL;
	}

	return 0;
}

static int ath6kl_init_service_ep(struct ath6kl *ar)
{
	struct htc_service_connect_req connect;

	memset(&connect, 0, sizeof(connect));

	/* these fields are the same for all service endpoints */
305
	connect.ep_cb.tx_comp_multi = ath6kl_tx_complete;
K
Kalle Valo 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	connect.ep_cb.rx = ath6kl_rx;
	connect.ep_cb.rx_refill = ath6kl_rx_refill;
	connect.ep_cb.tx_full = ath6kl_tx_queue_full;

	/*
	 * Set the max queue depth so that our ath6kl_tx_queue_full handler
	 * gets called.
	*/
	connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
	connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
	if (!connect.ep_cb.rx_refill_thresh)
		connect.ep_cb.rx_refill_thresh++;

	/* connect to control service */
	connect.svc_id = WMI_CONTROL_SVC;
	if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
		return -EIO;

	connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;

	/*
	 * Limit the HTC message size on the send path, although e can
	 * receive A-MSDU frames of 4K, we will only send ethernet-sized
	 * (802.3) frames on the send path.
	 */
	connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;

	/*
	 * To reduce the amount of committed memory for larger A_MSDU
	 * frames, use the recv-alloc threshold mechanism for larger
	 * packets.
	 */
	connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
	connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;

	/*
	 * For the remaining data services set the connection flag to
	 * reduce dribbling, if configured to do so.
	 */
	connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
	connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
	connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;

	connect.svc_id = WMI_DATA_BE_SVC;

	if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
		return -EIO;

	/* connect to back-ground map this to WMI LOW_PRI */
	connect.svc_id = WMI_DATA_BK_SVC;
	if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
		return -EIO;

359
	/* connect to Video service, map this to HI PRI */
K
Kalle Valo 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	connect.svc_id = WMI_DATA_VI_SVC;
	if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
		return -EIO;

	/*
	 * Connect to VO service, this is currently not mapped to a WMI
	 * priority stream due to historical reasons. WMI originally
	 * defined 3 priorities over 3 mailboxes We can change this when
	 * WMI is reworked so that priorities are not dependent on
	 * mailboxes.
	 */
	connect.svc_id = WMI_DATA_VO_SVC;
	if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
		return -EIO;

	return 0;
}

378
void ath6kl_init_control_info(struct ath6kl_vif *vif)
K
Kalle Valo 已提交
379
{
380
	ath6kl_init_profile_info(vif);
381
	vif->def_txkey_index = 0;
382
	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
383
	vif->ch_hint = 0;
K
Kalle Valo 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
}

/*
 * Set HTC/Mbox operational parameters, this can only be called when the
 * target is in the BMI phase.
 */
static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
				 u8 htc_ctrl_buf)
{
	int status;
	u32 blk_size;

	blk_size = ar->mbox_info.block_size;

	if (htc_ctrl_buf)
		blk_size |=  ((u32)htc_ctrl_buf) << 16;

	/* set the host interest area for the block size */
K
Kalle Valo 已提交
402
	status = ath6kl_bmi_write_hi32(ar, hi_mbox_io_block_sz, blk_size);
K
Kalle Valo 已提交
403 404 405 406 407 408 409 410 411 412 413
	if (status) {
		ath6kl_err("bmi_write_memory for IO block size failed\n");
		goto out;
	}

	ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
		   blk_size,
		   ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));

	if (mbox_isr_yield_val) {
		/* set the host interest area for the mbox ISR yield limit */
K
Kalle Valo 已提交
414 415
		status = ath6kl_bmi_write_hi32(ar, hi_mbox_isr_yield_limit,
					       mbox_isr_yield_val);
K
Kalle Valo 已提交
416 417 418 419 420 421 422 423 424 425
		if (status) {
			ath6kl_err("bmi_write_memory for yield limit failed\n");
			goto out;
		}
	}

out:
	return status;
}

426
static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
K
Kalle Valo 已提交
427
{
428
	int ret;
K
Kalle Valo 已提交
429 430 431 432 433 434

	/*
	 * Configure the device for rx dot11 header rules. "0,0" are the
	 * default values. Required if checksum offload is needed. Set
	 * RxMetaVersion to 2.
	 */
435 436 437 438 439
	ret = ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
						 ar->rx_meta_ver, 0, 0);
	if (ret) {
		ath6kl_err("unable to set the rx frame format: %d\n", ret);
		return ret;
K
Kalle Valo 已提交
440 441
	}

442 443
	if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN) {
		ret = ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
K
Kalle Valo 已提交
444
					      IGNORE_PS_FAIL_DURING_SCAN);
445 446 447 448
		if (ret) {
			ath6kl_err("unable to set power save fail event policy: %d\n",
				   ret);
			return ret;
K
Kalle Valo 已提交
449
		}
450
	}
K
Kalle Valo 已提交
451

452 453
	if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER)) {
		ret = ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
K
Kalle Valo 已提交
454
						   WMI_FOLLOW_BARKER_IN_ERP);
455 456 457 458
		if (ret) {
			ath6kl_err("unable to set barker preamble policy: %d\n",
				   ret);
			return ret;
K
Kalle Valo 已提交
459
		}
460
	}
K
Kalle Valo 已提交
461

462 463 464 465 466
	ret = ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
					   WLAN_CONFIG_KEEP_ALIVE_INTERVAL);
	if (ret) {
		ath6kl_err("unable to set keep alive interval: %d\n", ret);
		return ret;
K
Kalle Valo 已提交
467 468
	}

469 470 471 472 473
	ret = ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
					 WLAN_CONFIG_DISCONNECT_TIMEOUT);
	if (ret) {
		ath6kl_err("unable to set disconnect timeout: %d\n", ret);
		return ret;
K
Kalle Valo 已提交
474 475
	}

476 477 478 479 480
	if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST)) {
		ret = ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED);
		if (ret) {
			ath6kl_err("unable to set txop bursting: %d\n", ret);
			return ret;
K
Kalle Valo 已提交
481
		}
482
	}
K
Kalle Valo 已提交
483

484
	if (ar->p2p && (ar->vif_max == 1 || idx)) {
485
		ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
486 487 488 489
					      P2P_FLAG_CAPABILITIES_REQ |
					      P2P_FLAG_MACADDR_REQ |
					      P2P_FLAG_HMODEL_REQ);
		if (ret) {
490 491 492
			ath6kl_dbg(ATH6KL_DBG_TRC,
				   "failed to request P2P capabilities (%d) - assuming P2P not supported\n",
				   ret);
493
			ar->p2p = false;
494 495 496
		}
	}

497
	if (ar->p2p && (ar->vif_max == 1 || idx)) {
498
		/* Enable Probe Request reporting for P2P */
499
		ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
500
		if (ret) {
501 502 503
			ath6kl_dbg(ATH6KL_DBG_TRC,
				   "failed to enable Probe Request reporting (%d)\n",
				   ret);
504
		}
505 506
	}

507
	return ret;
K
Kalle Valo 已提交
508 509 510 511 512
}

int ath6kl_configure_target(struct ath6kl *ar)
{
	u32 param, ram_reserved_size;
513
	u8 fw_iftype, fw_mode = 0, fw_submode = 0;
514
	int i, status;
K
Kalle Valo 已提交
515

K
Kalle Valo 已提交
516
	param = !!(ar->conf_flags & ATH6KL_CONF_UART_DEBUG);
K
Kalle Valo 已提交
517
	if (ath6kl_bmi_write_hi32(ar, hi_serial_enable, param)) {
518 519 520 521
		ath6kl_err("bmi_write_memory for uart debug failed\n");
		return -EIO;
	}

522 523 524 525 526 527 528 529
	/*
	 * Note: Even though the firmware interface type is
	 * chosen as BSS_STA for all three interfaces, can
	 * be configured to IBSS/AP as long as the fw submode
	 * remains normal mode (0 - AP, STA and IBSS). But
	 * due to an target assert in firmware only one interface is
	 * configured for now.
	 */
530
	fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
K
Kalle Valo 已提交
531

532
	for (i = 0; i < ar->vif_max; i++)
533 534 535
		fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);

	/*
536 537
	 * Submodes when fw does not support dynamic interface
	 * switching:
538 539 540
	 *		vif[0] - AP/STA/IBSS
	 *		vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
	 *		vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
541
	 * Otherwise, All the interface are initialized to p2p dev.
542
	 */
543

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	if (test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
		     ar->fw_capabilities)) {
		for (i = 0; i < ar->vif_max; i++)
			fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
				(i * HI_OPTION_FW_SUBMODE_BITS);
	} else {
		for (i = 0; i < ar->max_norm_iface; i++)
			fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
				(i * HI_OPTION_FW_SUBMODE_BITS);

		for (i = ar->max_norm_iface; i < ar->vif_max; i++)
			fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
				(i * HI_OPTION_FW_SUBMODE_BITS);

		if (ar->p2p && ar->vif_max == 1)
			fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
	}
561

K
Kalle Valo 已提交
562 563
	if (ath6kl_bmi_write_hi32(ar, hi_app_host_interest,
				  HTC_PROTOCOL_VERSION) != 0) {
K
Kalle Valo 已提交
564 565 566 567 568 569 570
		ath6kl_err("bmi_write_memory for htc version failed\n");
		return -EIO;
	}

	/* set the firmware mode to STA/IBSS/AP */
	param = 0;

K
Kalle Valo 已提交
571
	if (ath6kl_bmi_read_hi32(ar, hi_option_flag, &param) != 0) {
K
Kalle Valo 已提交
572 573 574 575
		ath6kl_err("bmi_read_memory for setting fwmode failed\n");
		return -EIO;
	}

576
	param |= (ar->vif_max << HI_OPTION_NUM_DEV_SHIFT);
577 578 579
	param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
	param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;

K
Kalle Valo 已提交
580 581 582
	param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
	param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);

K
Kalle Valo 已提交
583
	if (ath6kl_bmi_write_hi32(ar, hi_option_flag, param) != 0) {
K
Kalle Valo 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
		ath6kl_err("bmi_write_memory for setting fwmode failed\n");
		return -EIO;
	}

	ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");

	/*
	 * Hardcode the address use for the extended board data
	 * Ideally this should be pre-allocate by the OS at boot time
	 * But since it is a new feature and board data is loaded
	 * at init time, we have to workaround this from host.
	 * It is difficult to patch the firmware boot code,
	 * but possible in theory.
	 */

599 600 601
	if (ar->target_type == TARGET_TYPE_AR6003) {
		param = ar->hw.board_ext_data_addr;
		ram_reserved_size = ar->hw.reserved_ram_size;
K
Kalle Valo 已提交
602

603 604 605 606
		if (ath6kl_bmi_write_hi32(ar, hi_board_ext_data, param) != 0) {
			ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
			return -EIO;
		}
607

608 609 610 611 612
		if (ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz,
					  ram_reserved_size) != 0) {
			ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
			return -EIO;
		}
K
Kalle Valo 已提交
613 614 615 616 617 618 619
	}

	/* set the block size for the target */
	if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
		/* use default number of control buffers */
		return -EIO;

620
	/* Configure GPIO AR600x UART */
K
Kalle Valo 已提交
621 622
	status = ath6kl_bmi_write_hi32(ar, hi_dbg_uart_txpin,
				       ar->hw.uarttx_pin);
623 624 625 626
	if (status)
		return status;

	/* Configure target refclk_hz */
K
Kalle Valo 已提交
627
	status = ath6kl_bmi_write_hi32(ar, hi_refclk_hz, ar->hw.refclk_hz);
628 629 630
	if (status)
		return status;

K
Kalle Valo 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
	return 0;
}

/* firmware upload */
static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
			 u8 **fw, size_t *fw_len)
{
	const struct firmware *fw_entry;
	int ret;

	ret = request_firmware(&fw_entry, filename, ar->dev);
	if (ret)
		return ret;

	*fw_len = fw_entry->size;
	*fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);

	if (*fw == NULL)
		ret = -ENOMEM;

	release_firmware(fw_entry);

	return ret;
}

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
#ifdef CONFIG_OF
/*
 * Check the device tree for a board-id and use it to construct
 * the pathname to the firmware file.  Used (for now) to find a
 * fallback to the "bdata.bin" file--typically a symlink to the
 * appropriate board-specific file.
 */
static bool check_device_tree(struct ath6kl *ar)
{
	static const char *board_id_prop = "atheros,board-id";
	struct device_node *node;
	char board_filename[64];
	const char *board_id;
	int ret;

	for_each_compatible_node(node, NULL, "atheros,ath6kl") {
		board_id = of_get_property(node, board_id_prop, NULL);
		if (board_id == NULL) {
			ath6kl_warn("No \"%s\" property on %s node.\n",
				    board_id_prop, node->name);
			continue;
		}
		snprintf(board_filename, sizeof(board_filename),
679
			 "%s/bdata.%s.bin", ar->hw.fw.dir, board_id);
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

		ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
				    &ar->fw_board_len);
		if (ret) {
			ath6kl_err("Failed to get DT board file %s: %d\n",
				   board_filename, ret);
			continue;
		}
		return true;
	}
	return false;
}
#else
static bool check_device_tree(struct ath6kl *ar)
{
	return false;
}
#endif /* CONFIG_OF */

K
Kalle Valo 已提交
699 700 701 702 703
static int ath6kl_fetch_board_file(struct ath6kl *ar)
{
	const char *filename;
	int ret;

704 705 706
	if (ar->fw_board != NULL)
		return 0;

707 708 709 710
	if (WARN_ON(ar->hw.fw_board == NULL))
		return -EINVAL;

	filename = ar->hw.fw_board;
K
Kalle Valo 已提交
711 712 713 714 715 716 717 718

	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
			    &ar->fw_board_len);
	if (ret == 0) {
		/* managed to get proper board file */
		return 0;
	}

719 720 721 722 723
	if (check_device_tree(ar)) {
		/* got board file from device tree */
		return 0;
	}

K
Kalle Valo 已提交
724 725 726 727
	/* there was no proper board file, try to use default instead */
	ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
		    filename, ret);

728
	filename = ar->hw.fw_default_board;
K
Kalle Valo 已提交
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
			    &ar->fw_board_len);
	if (ret) {
		ath6kl_err("Failed to get default board file %s: %d\n",
			   filename, ret);
		return ret;
	}

	ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
	ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");

	return 0;
}

744 745
static int ath6kl_fetch_otp_file(struct ath6kl *ar)
{
746
	char filename[100];
747 748 749 750 751
	int ret;

	if (ar->fw_otp != NULL)
		return 0;

752
	if (ar->hw.fw.otp == NULL) {
753 754
		ath6kl_dbg(ATH6KL_DBG_BOOT,
			   "no OTP file configured for this hw\n");
755 756 757
		return 0;
	}

758 759
	snprintf(filename, sizeof(filename), "%s/%s",
		 ar->hw.fw.dir, ar->hw.fw.otp);
760

761 762 763 764 765 766 767 768 769 770 771
	ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
			    &ar->fw_otp_len);
	if (ret) {
		ath6kl_err("Failed to get OTP file %s: %d\n",
			   filename, ret);
		return ret;
	}

	return 0;
}

772
static int ath6kl_fetch_testmode_file(struct ath6kl *ar)
773
{
774
	char filename[100];
775 776
	int ret;

777
	if (ar->testmode == 0)
778 779
		return 0;

780
	ath6kl_dbg(ATH6KL_DBG_BOOT, "testmode %d\n", ar->testmode);
781

782 783 784 785 786
	if (ar->testmode == 2) {
		if (ar->hw.fw.utf == NULL) {
			ath6kl_warn("testmode 2 not supported\n");
			return -EOPNOTSUPP;
		}
787

788 789 790 791 792 793
		snprintf(filename, sizeof(filename), "%s/%s",
			 ar->hw.fw.dir, ar->hw.fw.utf);
	} else {
		if (ar->hw.fw.tcmd == NULL) {
			ath6kl_warn("testmode 1 not supported\n");
			return -EOPNOTSUPP;
A
Alex Yang 已提交
794
		}
795

796 797
		snprintf(filename, sizeof(filename), "%s/%s",
			 ar->hw.fw.dir, ar->hw.fw.tcmd);
798 799
	}

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
	set_bit(TESTMODE, &ar->flag);

	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
	if (ret) {
		ath6kl_err("Failed to get testmode %d firmware file %s: %d\n",
			   ar->testmode, filename, ret);
		return ret;
	}

	return 0;
}

static int ath6kl_fetch_fw_file(struct ath6kl *ar)
{
	char filename[100];
	int ret;

	if (ar->fw != NULL)
		return 0;

820 821
	/* FIXME: remove WARN_ON() as we won't support FW API 1 for long */
	if (WARN_ON(ar->hw.fw.fw == NULL))
822 823
		return -EINVAL;

824 825
	snprintf(filename, sizeof(filename), "%s/%s",
		 ar->hw.fw.dir, ar->hw.fw.fw);
826 827 828 829 830 831 832 833 834 835 836 837 838

	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
	if (ret) {
		ath6kl_err("Failed to get firmware file %s: %d\n",
			   filename, ret);
		return ret;
	}

	return 0;
}

static int ath6kl_fetch_patch_file(struct ath6kl *ar)
{
839
	char filename[100];
840 841
	int ret;

842
	if (ar->fw_patch != NULL)
843 844
		return 0;

845
	if (ar->hw.fw.patch == NULL)
846 847
		return 0;

848 849
	snprintf(filename, sizeof(filename), "%s/%s",
		 ar->hw.fw.dir, ar->hw.fw.patch);
850 851 852 853 854 855 856

	ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
			    &ar->fw_patch_len);
	if (ret) {
		ath6kl_err("Failed to get patch file %s: %d\n",
			   filename, ret);
		return ret;
857 858 859 860 861
	}

	return 0;
}

A
Alex Yang 已提交
862 863 864 865 866
static int ath6kl_fetch_testscript_file(struct ath6kl *ar)
{
	char filename[100];
	int ret;

867
	if (ar->testmode != 2)
A
Alex Yang 已提交
868 869 870 871 872 873 874 875 876
		return 0;

	if (ar->fw_testscript != NULL)
		return 0;

	if (ar->hw.fw.testscript == NULL)
		return 0;

	snprintf(filename, sizeof(filename), "%s/%s",
877
		 ar->hw.fw.dir, ar->hw.fw.testscript);
A
Alex Yang 已提交
878 879 880 881 882

	ret = ath6kl_get_fw(ar, filename, &ar->fw_testscript,
				&ar->fw_testscript_len);
	if (ret) {
		ath6kl_err("Failed to get testscript file %s: %d\n",
883
			   filename, ret);
A
Alex Yang 已提交
884 885 886 887 888 889
		return ret;
	}

	return 0;
}

890
static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
{
	int ret;

	ret = ath6kl_fetch_otp_file(ar);
	if (ret)
		return ret;

	ret = ath6kl_fetch_fw_file(ar);
	if (ret)
		return ret;

	ret = ath6kl_fetch_patch_file(ar);
	if (ret)
		return ret;

A
Alex Yang 已提交
906 907 908 909
	ret = ath6kl_fetch_testscript_file(ar);
	if (ret)
		return ret;

910 911
	return 0;
}
K
Kalle Valo 已提交
912

K
Kalle Valo 已提交
913
static int ath6kl_fetch_fw_apin(struct ath6kl *ar, const char *name)
914 915 916 917
{
	size_t magic_len, len, ie_len;
	const struct firmware *fw;
	struct ath6kl_fw_ie *hdr;
918
	char filename[100];
919
	const u8 *data;
920
	int ret, ie_id, i, index, bit;
921
	__le32 *val;
922

K
Kalle Valo 已提交
923
	snprintf(filename, sizeof(filename), "%s/%s", ar->hw.fw.dir, name);
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

	ret = request_firmware(&fw, filename, ar->dev);
	if (ret)
		return ret;

	data = fw->data;
	len = fw->size;

	/* magic also includes the null byte, check that as well */
	magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;

	if (len < magic_len) {
		ret = -EINVAL;
		goto out;
	}

	if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
		ret = -EINVAL;
		goto out;
	}

	len -= magic_len;
	data += magic_len;

	/* loop elements */
	while (len > sizeof(struct ath6kl_fw_ie)) {
		/* hdr is unaligned! */
		hdr = (struct ath6kl_fw_ie *) data;

		ie_id = le32_to_cpup(&hdr->id);
		ie_len = le32_to_cpup(&hdr->len);

		len -= sizeof(*hdr);
		data += sizeof(*hdr);

		if (len < ie_len) {
			ret = -EINVAL;
			goto out;
		}

		switch (ie_id) {
965 966 967 968 969 970 971 972
		case ATH6KL_FW_IE_FW_VERSION:
			strlcpy(ar->wiphy->fw_version, data,
				sizeof(ar->wiphy->fw_version));

			ath6kl_dbg(ATH6KL_DBG_BOOT,
				   "found fw version %s\n",
				    ar->wiphy->fw_version);
			break;
973
		case ATH6KL_FW_IE_OTP_IMAGE:
K
Kalle Valo 已提交
974
			ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
975
				   ie_len);
K
Kalle Valo 已提交
976

977 978 979 980 981 982 983 984 985 986
			ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);

			if (ar->fw_otp == NULL) {
				ret = -ENOMEM;
				goto out;
			}

			ar->fw_otp_len = ie_len;
			break;
		case ATH6KL_FW_IE_FW_IMAGE:
K
Kalle Valo 已提交
987
			ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
988
				   ie_len);
K
Kalle Valo 已提交
989

990 991 992 993
			/* in testmode we already might have a fw file */
			if (ar->fw != NULL)
				break;

994
			ar->fw = vmalloc(ie_len);
995 996 997 998 999 1000

			if (ar->fw == NULL) {
				ret = -ENOMEM;
				goto out;
			}

1001
			memcpy(ar->fw, data, ie_len);
1002 1003 1004
			ar->fw_len = ie_len;
			break;
		case ATH6KL_FW_IE_PATCH_IMAGE:
K
Kalle Valo 已提交
1005
			ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
1006
				   ie_len);
K
Kalle Valo 已提交
1007

1008 1009 1010 1011 1012 1013 1014 1015 1016
			ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);

			if (ar->fw_patch == NULL) {
				ret = -ENOMEM;
				goto out;
			}

			ar->fw_patch_len = ie_len;
			break;
1017 1018 1019
		case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
			val = (__le32 *) data;
			ar->hw.reserved_ram_size = le32_to_cpup(val);
K
Kalle Valo 已提交
1020 1021 1022 1023

			ath6kl_dbg(ATH6KL_DBG_BOOT,
				   "found reserved ram size ie 0x%d\n",
				   ar->hw.reserved_ram_size);
1024
			break;
1025
		case ATH6KL_FW_IE_CAPABILITIES:
K
Kalle Valo 已提交
1026
			ath6kl_dbg(ATH6KL_DBG_BOOT,
K
Kalle Valo 已提交
1027
				   "found firmware capabilities ie (%zd B)\n",
K
Kalle Valo 已提交
1028 1029
				   ie_len);

1030
			for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
1031
				index = i / 8;
1032 1033
				bit = i % 8;

1034 1035 1036
				if (index == ie_len)
					break;

1037 1038 1039
				if (data[index] & (1 << bit))
					__set_bit(i, ar->fw_capabilities);
			}
K
Kalle Valo 已提交
1040 1041 1042 1043

			ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
					ar->fw_capabilities,
					sizeof(ar->fw_capabilities));
1044
			break;
1045 1046 1047 1048 1049 1050
		case ATH6KL_FW_IE_PATCH_ADDR:
			if (ie_len != sizeof(*val))
				break;

			val = (__le32 *) data;
			ar->hw.dataset_patch_addr = le32_to_cpup(val);
K
Kalle Valo 已提交
1051 1052

			ath6kl_dbg(ATH6KL_DBG_BOOT,
1053
				   "found patch address ie 0x%x\n",
K
Kalle Valo 已提交
1054
				   ar->hw.dataset_patch_addr);
1055
			break;
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		case ATH6KL_FW_IE_BOARD_ADDR:
			if (ie_len != sizeof(*val))
				break;

			val = (__le32 *) data;
			ar->hw.board_addr = le32_to_cpup(val);

			ath6kl_dbg(ATH6KL_DBG_BOOT,
				   "found board address ie 0x%x\n",
				   ar->hw.board_addr);
			break;
1067 1068 1069 1070 1071 1072 1073 1074
		case ATH6KL_FW_IE_VIF_MAX:
			if (ie_len != sizeof(*val))
				break;

			val = (__le32 *) data;
			ar->vif_max = min_t(unsigned int, le32_to_cpup(val),
					    ATH6KL_VIF_MAX);

1075 1076 1077
			if (ar->vif_max > 1 && !ar->p2p)
				ar->max_norm_iface = 2;

1078 1079 1080
			ath6kl_dbg(ATH6KL_DBG_BOOT,
				   "found vif max ie %d\n", ar->vif_max);
			break;
1081
		default:
K
Kalle Valo 已提交
1082
			ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
				   le32_to_cpup(&hdr->id));
			break;
		}

		len -= ie_len;
		data += ie_len;
	};

	ret = 0;
out:
	release_firmware(fw);

	return ret;
}

K
Kalle Valo 已提交
1098
int ath6kl_init_fetch_firmwares(struct ath6kl *ar)
1099 1100 1101 1102 1103 1104 1105
{
	int ret;

	ret = ath6kl_fetch_board_file(ar);
	if (ret)
		return ret;

1106 1107 1108 1109
	ret = ath6kl_fetch_testmode_file(ar);
	if (ret)
		return ret;

T
Thomas Pedersen 已提交
1110 1111 1112 1113 1114 1115
	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API4_FILE);
	if (ret == 0) {
		ar->fw_api = 4;
		goto out;
	}

K
Kalle Valo 已提交
1116
	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API3_FILE);
K
Kalle Valo 已提交
1117
	if (ret == 0) {
K
Kalle Valo 已提交
1118 1119 1120 1121 1122 1123 1124 1125
		ar->fw_api = 3;
		goto out;
	}

	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API2_FILE);
	if (ret == 0) {
		ar->fw_api = 2;
		goto out;
K
Kalle Valo 已提交
1126
	}
1127 1128 1129 1130 1131

	ret = ath6kl_fetch_fw_api1(ar);
	if (ret)
		return ret;

K
Kalle Valo 已提交
1132 1133 1134 1135
	ar->fw_api = 1;

out:
	ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api %d\n", ar->fw_api);
K
Kalle Valo 已提交
1136

1137 1138 1139
	return 0;
}

K
Kalle Valo 已提交
1140 1141 1142
static int ath6kl_upload_board_file(struct ath6kl *ar)
{
	u32 board_address, board_ext_address, param;
1143
	u32 board_data_size, board_ext_data_size;
K
Kalle Valo 已提交
1144 1145
	int ret;

1146 1147
	if (WARN_ON(ar->fw_board == NULL))
		return -ENOENT;
K
Kalle Valo 已提交
1148

1149 1150 1151 1152 1153
	/*
	 * Determine where in Target RAM to write Board Data.
	 * For AR6004, host determine Target RAM address for
	 * writing board data.
	 */
1154
	if (ar->hw.board_addr != 0) {
1155
		board_address = ar->hw.board_addr;
K
Kalle Valo 已提交
1156
		ath6kl_bmi_write_hi32(ar, hi_board_data,
1157
				      board_address);
1158
	} else {
1159 1160 1161 1162 1163
		ret = ath6kl_bmi_read_hi32(ar, hi_board_data, &board_address);
		if (ret) {
			ath6kl_err("Failed to get board file target address.\n");
			return ret;
		}
1164 1165
	}

K
Kalle Valo 已提交
1166
	/* determine where in target ram to write extended board data */
1167 1168 1169 1170 1171
	ret = ath6kl_bmi_read_hi32(ar, hi_board_ext_data, &board_ext_address);
	if (ret) {
		ath6kl_err("Failed to get extended board file target address.\n");
		return ret;
	}
K
Kalle Valo 已提交
1172

1173 1174
	if (ar->target_type == TARGET_TYPE_AR6003 &&
	    board_ext_address == 0) {
K
Kalle Valo 已提交
1175 1176 1177 1178
		ath6kl_err("Failed to get board file target address.\n");
		return -EINVAL;
	}

1179 1180 1181 1182
	switch (ar->target_type) {
	case TARGET_TYPE_AR6003:
		board_data_size = AR6003_BOARD_DATA_SZ;
		board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1183 1184
		if (ar->fw_board_len > (board_data_size + board_ext_data_size))
			board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ_V2;
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
		break;
	case TARGET_TYPE_AR6004:
		board_data_size = AR6004_BOARD_DATA_SZ;
		board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
		break;
	default:
		WARN_ON(1);
		return -EINVAL;
		break;
	}

1196 1197
	if (board_ext_address &&
	    ar->fw_board_len == (board_data_size + board_ext_data_size)) {
K
Kalle Valo 已提交
1198
		/* write extended board data */
K
Kalle Valo 已提交
1199 1200 1201 1202
		ath6kl_dbg(ATH6KL_DBG_BOOT,
			   "writing extended board data to 0x%x (%d B)\n",
			   board_ext_address, board_ext_data_size);

K
Kalle Valo 已提交
1203
		ret = ath6kl_bmi_write(ar, board_ext_address,
1204 1205
				       ar->fw_board + board_data_size,
				       board_ext_data_size);
K
Kalle Valo 已提交
1206 1207 1208 1209 1210 1211 1212
		if (ret) {
			ath6kl_err("Failed to write extended board data: %d\n",
				   ret);
			return ret;
		}

		/* record that extended board data is initialized */
1213 1214
		param = (board_ext_data_size << 16) | 1;

K
Kalle Valo 已提交
1215
		ath6kl_bmi_write_hi32(ar, hi_board_ext_data_config, param);
K
Kalle Valo 已提交
1216 1217
	}

1218
	if (ar->fw_board_len < board_data_size) {
K
Kalle Valo 已提交
1219 1220 1221 1222 1223
		ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
		ret = -EINVAL;
		return ret;
	}

K
Kalle Valo 已提交
1224 1225 1226
	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
		   board_address, board_data_size);

K
Kalle Valo 已提交
1227
	ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
1228
			       board_data_size);
K
Kalle Valo 已提交
1229 1230 1231 1232 1233 1234 1235

	if (ret) {
		ath6kl_err("Board file bmi write failed: %d\n", ret);
		return ret;
	}

	/* record the fact that Board Data IS initialized */
K
Kalle Valo 已提交
1236
	ath6kl_bmi_write_hi32(ar, hi_board_data_initialized, 1);
K
Kalle Valo 已提交
1237 1238 1239 1240 1241 1242 1243

	return ret;
}

static int ath6kl_upload_otp(struct ath6kl *ar)
{
	u32 address, param;
1244
	bool from_hw = false;
K
Kalle Valo 已提交
1245 1246
	int ret;

1247 1248
	if (ar->fw_otp == NULL)
		return 0;
K
Kalle Valo 已提交
1249

1250
	address = ar->hw.app_load_addr;
K
Kalle Valo 已提交
1251

K
Kalle Valo 已提交
1252
	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
K
Kalle Valo 已提交
1253 1254
		   ar->fw_otp_len);

K
Kalle Valo 已提交
1255 1256 1257 1258 1259 1260 1261
	ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
				       ar->fw_otp_len);
	if (ret) {
		ath6kl_err("Failed to upload OTP file: %d\n", ret);
		return ret;
	}

1262
	/* read firmware start address */
K
Kalle Valo 已提交
1263
	ret = ath6kl_bmi_read_hi32(ar, hi_app_start, &address);
1264 1265 1266 1267 1268 1269

	if (ret) {
		ath6kl_err("Failed to read hi_app_start: %d\n", ret);
		return ret;
	}

1270 1271 1272 1273
	if (ar->hw.app_start_override_addr == 0) {
		ar->hw.app_start_override_addr = address;
		from_hw = true;
	}
1274

1275 1276
	ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
		   from_hw ? " (from hw)" : "",
K
Kalle Valo 已提交
1277 1278
		   ar->hw.app_start_override_addr);

K
Kalle Valo 已提交
1279
	/* execute the OTP code */
1280 1281
	ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
		   ar->hw.app_start_override_addr);
K
Kalle Valo 已提交
1282
	param = 0;
1283
	ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
K
Kalle Valo 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292

	return ret;
}

static int ath6kl_upload_firmware(struct ath6kl *ar)
{
	u32 address;
	int ret;

1293
	if (WARN_ON(ar->fw == NULL))
1294
		return 0;
K
Kalle Valo 已提交
1295

1296
	address = ar->hw.app_load_addr;
K
Kalle Valo 已提交
1297

K
Kalle Valo 已提交
1298
	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
K
Kalle Valo 已提交
1299 1300
		   address, ar->fw_len);

K
Kalle Valo 已提交
1301 1302 1303 1304 1305 1306 1307
	ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);

	if (ret) {
		ath6kl_err("Failed to write firmware: %d\n", ret);
		return ret;
	}

1308 1309 1310 1311 1312
	/*
	 * Set starting address for firmware
	 * Don't need to setup app_start override addr on AR6004
	 */
	if (ar->target_type != TARGET_TYPE_AR6004) {
1313
		address = ar->hw.app_start_override_addr;
1314 1315
		ath6kl_bmi_set_app_start(ar, address);
	}
K
Kalle Valo 已提交
1316 1317 1318 1319 1320
	return ret;
}

static int ath6kl_upload_patch(struct ath6kl *ar)
{
K
Kalle Valo 已提交
1321
	u32 address;
K
Kalle Valo 已提交
1322 1323
	int ret;

1324 1325
	if (ar->fw_patch == NULL)
		return 0;
K
Kalle Valo 已提交
1326

1327
	address = ar->hw.dataset_patch_addr;
K
Kalle Valo 已提交
1328

K
Kalle Valo 已提交
1329
	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
K
Kalle Valo 已提交
1330 1331
		   address, ar->fw_patch_len);

K
Kalle Valo 已提交
1332 1333 1334 1335 1336 1337
	ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
	if (ret) {
		ath6kl_err("Failed to write patch file: %d\n", ret);
		return ret;
	}

K
Kalle Valo 已提交
1338
	ath6kl_bmi_write_hi32(ar, hi_dset_list_head, address);
K
Kalle Valo 已提交
1339 1340 1341 1342

	return 0;
}

A
Alex Yang 已提交
1343 1344
static int ath6kl_upload_testscript(struct ath6kl *ar)
{
K
Kalle Valo 已提交
1345
	u32 address;
A
Alex Yang 已提交
1346 1347
	int ret;

1348
	if (ar->testmode != 2)
A
Alex Yang 已提交
1349 1350 1351 1352 1353 1354 1355 1356
		return 0;

	if (ar->fw_testscript == NULL)
		return 0;

	address = ar->hw.testscript_addr;

	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing testscript to 0x%x (%zd B)\n",
1357
		   address, ar->fw_testscript_len);
A
Alex Yang 已提交
1358 1359 1360 1361 1362 1363 1364 1365

	ret = ath6kl_bmi_write(ar, address, ar->fw_testscript,
		ar->fw_testscript_len);
	if (ret) {
		ath6kl_err("Failed to write testscript file: %d\n", ret);
		return ret;
	}

K
Kalle Valo 已提交
1366 1367 1368
	ath6kl_bmi_write_hi32(ar, hi_ota_testscript, address);
	ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz, 4096);
	ath6kl_bmi_write_hi32(ar, hi_test_apps_related, 1);
A
Alex Yang 已提交
1369 1370 1371 1372

	return 0;
}

K
Kalle Valo 已提交
1373 1374 1375 1376 1377
static int ath6kl_init_upload(struct ath6kl *ar)
{
	u32 param, options, sleep, address;
	int status = 0;

1378
	if (ar->target_type != TARGET_TYPE_AR6003 &&
1379
	    ar->target_type != TARGET_TYPE_AR6004)
K
Kalle Valo 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
		return -EINVAL;

	/* temporarily disable system sleep */
	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
	status = ath6kl_bmi_reg_read(ar, address, &param);
	if (status)
		return status;

	options = param;

	param |= ATH6KL_OPTION_SLEEP_DISABLE;
	status = ath6kl_bmi_reg_write(ar, address, param);
	if (status)
		return status;

	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
	status = ath6kl_bmi_reg_read(ar, address, &param);
	if (status)
		return status;

	sleep = param;

	param |= SM(SYSTEM_SLEEP_DISABLE, 1);
	status = ath6kl_bmi_reg_write(ar, address, param);
	if (status)
		return status;

	ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
		   options, sleep);

	/* program analog PLL register */
1411 1412 1413 1414
	/* no need to control 40/44MHz clock on AR6004 */
	if (ar->target_type != TARGET_TYPE_AR6004) {
		status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
					      0xF9104001);
K
Kalle Valo 已提交
1415

1416 1417
		if (status)
			return status;
K
Kalle Valo 已提交
1418

1419 1420 1421 1422 1423 1424 1425 1426
		/* Run at 80/88MHz by default */
		param = SM(CPU_CLOCK_STANDARD, 1);

		address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
		status = ath6kl_bmi_reg_write(ar, address, param);
		if (status)
			return status;
	}
K
Kalle Valo 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435

	param = 0;
	address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
	param = SM(LPO_CAL_ENABLE, 1);
	status = ath6kl_bmi_reg_write(ar, address, param);
	if (status)
		return status;

	/* WAR to avoid SDIO CRC err */
1436
	if (ar->hw.flags & ATH6KL_HW_SDIO_CRC_ERROR_WAR) {
K
Kalle Valo 已提交
1437 1438
		ath6kl_err("temporary war to avoid sdio crc error\n");

1439 1440 1441 1442 1443 1444
		param = 0x28;
		address = GPIO_BASE_ADDRESS + GPIO_PIN9_ADDRESS;
		status = ath6kl_bmi_reg_write(ar, address, param);
		if (status)
			return status;

K
Kalle Valo 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
		param = 0x20;

		address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
		status = ath6kl_bmi_reg_write(ar, address, param);
		if (status)
			return status;

		address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
		status = ath6kl_bmi_reg_write(ar, address, param);
		if (status)
			return status;

		address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
		status = ath6kl_bmi_reg_write(ar, address, param);
		if (status)
			return status;

		address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
		status = ath6kl_bmi_reg_write(ar, address, param);
		if (status)
			return status;
	}

	/* write EEPROM data to Target RAM */
	status = ath6kl_upload_board_file(ar);
	if (status)
		return status;

	/* transfer One time Programmable data */
	status = ath6kl_upload_otp(ar);
	if (status)
		return status;

	/* Download Target firmware */
	status = ath6kl_upload_firmware(ar);
	if (status)
		return status;

	status = ath6kl_upload_patch(ar);
	if (status)
		return status;

A
Alex Yang 已提交
1487 1488 1489 1490 1491
	/* Download the test script */
	status = ath6kl_upload_testscript(ar);
	if (status)
		return status;

K
Kalle Valo 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	/* Restore system sleep */
	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
	status = ath6kl_bmi_reg_write(ar, address, sleep);
	if (status)
		return status;

	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
	param = options | 0x20;
	status = ath6kl_bmi_reg_write(ar, address, param);
	if (status)
		return status;

	return status;
}

K
Kalle Valo 已提交
1507
int ath6kl_init_hw_params(struct ath6kl *ar)
1508
{
1509
	const struct ath6kl_hw *uninitialized_var(hw);
1510
	int i;
1511

1512 1513
	for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
		hw = &hw_list[i];
1514

1515 1516 1517 1518 1519
		if (hw->id == ar->version.target_ver)
			break;
	}

	if (i == ARRAY_SIZE(hw_list)) {
1520 1521 1522 1523 1524
		ath6kl_err("Unsupported hardware version: 0x%x\n",
			   ar->version.target_ver);
		return -EINVAL;
	}

1525 1526
	ar->hw = *hw;

K
Kalle Valo 已提交
1527 1528 1529 1530 1531 1532 1533 1534
	ath6kl_dbg(ATH6KL_DBG_BOOT,
		   "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
		   ar->version.target_ver, ar->target_type,
		   ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
	ath6kl_dbg(ATH6KL_DBG_BOOT,
		   "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
		   ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
		   ar->hw.reserved_ram_size);
1535 1536 1537
	ath6kl_dbg(ATH6KL_DBG_BOOT,
		   "refclk_hz %d uarttx_pin %d",
		   ar->hw.refclk_hz, ar->hw.uarttx_pin);
K
Kalle Valo 已提交
1538

1539 1540 1541
	return 0;
}

1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
static const char *ath6kl_init_get_hif_name(enum ath6kl_hif_type type)
{
	switch (type) {
	case ATH6KL_HIF_TYPE_SDIO:
		return "sdio";
	case ATH6KL_HIF_TYPE_USB:
		return "usb";
	}

	return NULL;
}

K
Kalle Valo 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572

static const struct fw_capa_str_map {
	int id;
	const char *name;
} fw_capa_map[] = {
	{ ATH6KL_FW_CAPABILITY_HOST_P2P, "host-p2p" },
	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN, "sched-scan" },
	{ ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX, "sta-p2pdev-duplex" },
	{ ATH6KL_FW_CAPABILITY_INACTIVITY_TIMEOUT, "inactivity-timeout" },
	{ ATH6KL_FW_CAPABILITY_RSN_CAP_OVERRIDE, "rsn-cap-override" },
	{ ATH6KL_FW_CAPABILITY_WOW_MULTICAST_FILTER, "wow-mc-filter" },
	{ ATH6KL_FW_CAPABILITY_BMISS_ENHANCE, "bmiss-enhance" },
	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN_MATCH_LIST, "sscan-match-list" },
	{ ATH6KL_FW_CAPABILITY_RSSI_SCAN_THOLD, "rssi-scan-thold" },
	{ ATH6KL_FW_CAPABILITY_CUSTOM_MAC_ADDR, "custom-mac-addr" },
	{ ATH6KL_FW_CAPABILITY_TX_ERR_NOTIFY, "tx-err-notify" },
	{ ATH6KL_FW_CAPABILITY_REGDOMAIN, "regdomain" },
	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN_V2, "sched-scan-v2" },
	{ ATH6KL_FW_CAPABILITY_HEART_BEAT_POLL, "hb-poll" },
1573 1574 1575
	{ ATH6KL_FW_CAPABILITY_64BIT_RATES, "64bit-rates" },
	{ ATH6KL_FW_CAPABILITY_AP_INACTIVITY_MINS, "ap-inactivity-mins" },
	{ ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT, "map-lp-endpoint" },
K
Kalle Valo 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
};

static const char *ath6kl_init_get_fw_capa_name(unsigned int id)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(fw_capa_map); i++) {
		if (fw_capa_map[i].id == id)
			return fw_capa_map[i].name;
	}

	return "<unknown>";
}

static void ath6kl_init_get_fwcaps(struct ath6kl *ar, char *buf, size_t buf_len)
{
	u8 *data = (u8 *) ar->fw_capabilities;
	size_t trunc_len, len = 0;
	int i, index, bit;
	char *trunc = "...";

	for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
		index = i / 8;
		bit = i % 8;

		if (index >= sizeof(ar->fw_capabilities) * 4)
			break;

		if (buf_len - len < 4) {
			ath6kl_warn("firmware capability buffer too small!\n");

			/* add "..." to the end of string */
			trunc_len = strlen(trunc) + 1;
			strncpy(buf + buf_len - trunc_len, trunc, trunc_len);

			return;
		}

		if (data[index] & (1 << bit)) {
			len += scnprintf(buf + len, buf_len - len, "%s,",
					    ath6kl_init_get_fw_capa_name(i));
		}
	}

	/* overwrite the last comma */
	if (len > 0)
		len--;

	buf[len] = '\0';
}

1627 1628 1629 1630 1631 1632 1633 1634
static int ath6kl_init_hw_reset(struct ath6kl *ar)
{
	ath6kl_dbg(ATH6KL_DBG_BOOT, "cold resetting the device");

	return ath6kl_diag_write32(ar, RESET_CONTROL_ADDRESS,
				   cpu_to_le32(RESET_CONTROL_COLD_RST));
}

1635
static int __ath6kl_init_hw_start(struct ath6kl *ar)
1636 1637 1638
{
	long timeleft;
	int ret, i;
K
Kalle Valo 已提交
1639
	char buf[200];
1640

1641 1642
	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
	ret = ath6kl_hif_power_on(ar);
	if (ret)
		return ret;

	ret = ath6kl_configure_target(ar);
	if (ret)
		goto err_power_off;

	ret = ath6kl_init_upload(ar);
	if (ret)
		goto err_power_off;

	/* Do we need to finish the BMI phase */
1656 1657
	ret = ath6kl_bmi_done(ar);
	if (ret)
1658 1659 1660 1661 1662 1663 1664
		goto err_power_off;

	/*
	 * The reason we have to wait for the target here is that the
	 * driver layer has to init BMI in order to set the host block
	 * size.
	 */
1665
	ret = ath6kl_htc_wait_target(ar->htc_target);
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677

	if (ret == -ETIMEDOUT) {
		/*
		 * Most likely USB target is in odd state after reboot and
		 * needs a reset. A cold reset makes the whole device
		 * disappear from USB bus and initialisation starts from
		 * beginning.
		 */
		ath6kl_warn("htc wait target timed out, resetting device\n");
		ath6kl_init_hw_reset(ar);
		goto err_power_off;
	} else if (ret) {
1678
		ath6kl_err("htc wait target failed: %d\n", ret);
1679 1680 1681
		goto err_power_off;
	}

1682 1683 1684
	ret = ath6kl_init_service_ep(ar);
	if (ret) {
		ath6kl_err("Endpoint service initilisation failed: %d\n", ret);
1685 1686 1687 1688
		goto err_cleanup_scatter;
	}

	/* setup credit distribution */
K
Kalle Valo 已提交
1689
	ath6kl_htc_credit_setup(ar->htc_target, &ar->credit_state_info);
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703

	/* start HTC */
	ret = ath6kl_htc_start(ar->htc_target);
	if (ret) {
		/* FIXME: call this */
		ath6kl_cookie_cleanup(ar);
		goto err_cleanup_scatter;
	}

	/* Wait for Wmi event to be ready */
	timeleft = wait_event_interruptible_timeout(ar->event_wq,
						    test_bit(WMI_READY,
							     &ar->flag),
						    WMI_TIMEOUT);
1704 1705 1706 1707 1708 1709 1710
	if (timeleft <= 0) {
		clear_bit(WMI_READY, &ar->flag);
		ath6kl_err("wmi is not ready or wait was interrupted: %ld\n",
			   timeleft);
		ret = -EIO;
		goto err_htc_stop;
	}
1711 1712 1713

	ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");

1714
	if (test_and_clear_bit(FIRST_BOOT, &ar->flag)) {
K
Kalle Valo 已提交
1715
		ath6kl_info("%s %s fw %s api %d%s\n",
1716 1717 1718
			    ar->hw.name,
			    ath6kl_init_get_hif_name(ar->hif_type),
			    ar->wiphy->fw_version,
K
Kalle Valo 已提交
1719
			    ar->fw_api,
1720
			    test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
K
Kalle Valo 已提交
1721 1722
		ath6kl_init_get_fwcaps(ar, buf, sizeof(buf));
		ath6kl_info("firmware supports: %s\n", buf);
1723 1724
	}

1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
	if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
		ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
			   ATH6KL_ABI_VERSION, ar->version.abi_ver);
		ret = -EIO;
		goto err_htc_stop;
	}

	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);

	/* communicate the wmi protocol verision to the target */
	/* FIXME: return error */
	if ((ath6kl_set_host_app_area(ar)) != 0)
		ath6kl_err("unable to set the host app area\n");

1739
	for (i = 0; i < ar->vif_max; i++) {
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
		ret = ath6kl_target_config_wlan_params(ar, i);
		if (ret)
			goto err_htc_stop;
	}

	return 0;

err_htc_stop:
	ath6kl_htc_stop(ar->htc_target);
err_cleanup_scatter:
	ath6kl_hif_cleanup_scatter(ar);
err_power_off:
	ath6kl_hif_power_off(ar);

	return ret;
}

1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
int ath6kl_init_hw_start(struct ath6kl *ar)
{
	int err;

	err = __ath6kl_init_hw_start(ar);
	if (err)
		return err;
	ar->state = ATH6KL_STATE_ON;
	return 0;
}

static int __ath6kl_init_hw_stop(struct ath6kl *ar)
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
{
	int ret;

	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");

	ath6kl_htc_stop(ar->htc_target);

	ath6kl_hif_stop(ar);

	ath6kl_bmi_reset(ar);

	ret = ath6kl_hif_power_off(ar);
	if (ret)
		ath6kl_warn("failed to power off hif: %d\n", ret);

1784 1785
	return 0;
}
1786

1787 1788 1789 1790 1791 1792 1793 1794
int ath6kl_init_hw_stop(struct ath6kl *ar)
{
	int err;

	err = __ath6kl_init_hw_stop(ar);
	if (err)
		return err;
	ar->state = ATH6KL_STATE_OFF;
1795 1796 1797
	return 0;
}

1798 1799
void ath6kl_init_hw_restart(struct ath6kl *ar)
{
1800 1801
	clear_bit(WMI_READY, &ar->flag);

1802 1803
	ath6kl_cfg80211_stop_all(ar);

1804 1805
	if (__ath6kl_init_hw_stop(ar)) {
		ath6kl_dbg(ATH6KL_DBG_RECOVERY, "Failed to stop during fw error recovery\n");
1806
		return;
1807
	}
1808 1809 1810 1811 1812 1813 1814

	if (__ath6kl_init_hw_start(ar)) {
		ath6kl_dbg(ATH6KL_DBG_RECOVERY, "Failed to restart during fw error recovery\n");
		return;
	}
}

K
Kalle Valo 已提交
1815 1816
void ath6kl_stop_txrx(struct ath6kl *ar)
{
1817
	struct ath6kl_vif *vif, *tmp_vif;
1818
	int i;
K
Kalle Valo 已提交
1819 1820 1821 1822 1823 1824 1825 1826

	set_bit(DESTROY_IN_PROGRESS, &ar->flag);

	if (down_interruptible(&ar->sem)) {
		ath6kl_err("down_interruptible failed\n");
		return;
	}

1827 1828 1829
	for (i = 0; i < AP_MAX_NUM_STA; i++)
		aggr_reset_state(ar->sta_list[i].aggr_conn);

1830
	spin_lock_bh(&ar->list_lock);
1831 1832
	list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
		list_del(&vif->list);
1833
		spin_unlock_bh(&ar->list_lock);
1834
		ath6kl_cfg80211_vif_stop(vif, test_bit(WMI_READY, &ar->flag));
1835
		rtnl_lock();
1836
		ath6kl_cfg80211_vif_cleanup(vif);
1837
		rtnl_unlock();
1838
		spin_lock_bh(&ar->list_lock);
1839
	}
1840
	spin_unlock_bh(&ar->list_lock);
K
Kalle Valo 已提交
1841

1842
	clear_bit(WMI_READY, &ar->flag);
K
Kalle Valo 已提交
1843

1844 1845 1846
	if (ar->fw_recovery.enable)
		del_timer_sync(&ar->fw_recovery.hb_timer);

1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
	/*
	 * After wmi_shudown all WMI events will be dropped. We
	 * need to cleanup the buffers allocated in AP mode and
	 * give disconnect notification to stack, which usually
	 * happens in the disconnect_event. Simulate the disconnect
	 * event by calling the function directly. Sometimes
	 * disconnect_event will be received when the debug logs
	 * are collected.
	 */
	ath6kl_wmi_shutdown(ar->wmi);
K
Kalle Valo 已提交
1857

1858 1859 1860 1861
	clear_bit(WMI_ENABLED, &ar->flag);
	if (ar->htc_target) {
		ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
		ath6kl_htc_stop(ar->htc_target);
K
Kalle Valo 已提交
1862 1863
	}

1864 1865 1866 1867
	/*
	 * Try to reset the device if we can. The driver may have been
	 * configure NOT to reset the target during a debug session.
	 */
1868
	ath6kl_init_hw_reset(ar);
1869

1870
	up(&ar->sem);
K
Kalle Valo 已提交
1871
}
K
Kalle Valo 已提交
1872
EXPORT_SYMBOL(ath6kl_stop_txrx);