testmode.c 33.4 KB
Newer Older
1 2 3 4 5 6 7
/******************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
W
Wey-Yi Guy 已提交
8
 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 * USA
 *
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.GPL.
 *
 * Contact Information:
 *  Intel Linux Wireless <ilw@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 * BSD LICENSE
 *
W
Wey-Yi Guy 已提交
33
 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
66
#include <linux/dma-mapping.h>
67 68 69 70 71 72 73
#include <net/net_namespace.h>
#include <linux/netdevice.h>
#include <net/cfg80211.h>
#include <net/mac80211.h>
#include <net/netlink.h>
#include "iwl-debug.h"
#include "iwl-io.h"
74
#include "iwl-trans.h"
75
#include "iwl-fh.h"
76
#include "iwl-prph.h"
77 78 79
#include "dev.h"
#include "agn.h"
#include "testmode.h"
80

81 82 83 84 85 86 87

/* Periphery registers absolute lower bound. This is used in order to
 * differentiate registery access through HBUS_TARG_PRPH_* and
 * HBUS_TARG_MEM_* accesses.
 */
#define IWL_TM_ABS_PRPH_START (0xA00000)

88 89 90
/* The TLVs used in the gnl message policy between the kernel module and
 * user space application. iwl_testmode_gnl_msg_policy is to be carried
 * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
91
 * See testmode.h
92 93 94 95 96 97 98 99 100 101 102 103 104 105
 */
static
struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
	[IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },

	[IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
	[IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },

	[IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
	[IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
	[IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },

	[IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
	[IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
W
Wey-Yi Guy 已提交
106

107 108
	[IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },

W
Wey-Yi Guy 已提交
109
	[IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
110
	[IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
111
	[IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
W
Wey-Yi Guy 已提交
112

113
	[IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
114

115
	[IWL_TM_ATTR_UCODE_OWNER] = { .type = NLA_U8, },
116

117 118 119
	[IWL_TM_ATTR_MEM_ADDR] = { .type = NLA_U32, },
	[IWL_TM_ATTR_BUFFER_SIZE] = { .type = NLA_U32, },
	[IWL_TM_ATTR_BUFFER_DUMP] = { .type = NLA_UNSPEC, },
120 121

	[IWL_TM_ATTR_FW_VERSION] = { .type = NLA_U32, },
122
	[IWL_TM_ATTR_DEVICE_ID] = { .type = NLA_U32, },
123 124 125
	[IWL_TM_ATTR_FW_TYPE] = { .type = NLA_U32, },
	[IWL_TM_ATTR_FW_INST_SIZE] = { .type = NLA_U32, },
	[IWL_TM_ATTR_FW_DATA_SIZE] = { .type = NLA_U32, },
126 127

	[IWL_TM_ATTR_ENABLE_NOTIFICATION] = {.type = NLA_FLAG, },
128 129 130
};

/*
131
 * See the struct iwl_rx_packet in commands.h for the format of the
132 133
 * received events from the device
 */
134
static inline int get_event_length(struct iwl_rx_cmd_buffer *rxb)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
	if (pkt)
		return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
	else
		return 0;
}


/*
 * This function multicasts the spontaneous messages from the device to the
 * user space. It is invoked whenever there is a received messages
 * from the device. This function is called within the ISR of the rx handlers
 * in iwlagn driver.
 *
 * The parsing of the message content is left to the user space application,
 * The message content is treated as unattacked raw data and is encapsulated
 * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
 *
 * @priv: the instance of iwlwifi device
 * @rxb: pointer to rx data content received by the ISR
 *
 * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
 * For the messages multicasting to the user application, the mandatory
 * TLV fields are :
 *	IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
 *	IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
 */

static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
165
				      struct iwl_rx_cmd_buffer *rxb)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
{
	struct ieee80211_hw *hw = priv->hw;
	struct sk_buff *skb;
	void *data;
	int length;

	data = (void *)rxb_addr(rxb);
	length = get_event_length(rxb);

	if (!data || length == 0)
		return;

	skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
								GFP_ATOMIC);
	if (skb == NULL) {
181
		IWL_ERR(priv,
182 183 184
			 "Run out of memory for messages to user space ?\n");
		return;
	}
185 186 187 188
	if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT) ||
	    /* the length doesn't include len_n_flags field, so add it manually */
	    nla_put(skb, IWL_TM_ATTR_UCODE_RX_PKT, length + sizeof(__le32), data))
		goto nla_put_failure;
189 190 191 192 193
	cfg80211_testmode_event(skb, GFP_ATOMIC);
	return;

nla_put_failure:
	kfree_skb(skb);
194
	IWL_ERR(priv, "Ouch, overran buffer, check allocation!\n");
195 196 197 198
}

void iwl_testmode_init(struct iwl_priv *priv)
{
199
	priv->pre_rx_handler = NULL;
W
Wey-Yi Guy 已提交
200
	priv->testmode_trace.trace_enabled = false;
201
	priv->testmode_mem.read_in_progress = false;
202 203
}

204
static void iwl_mem_cleanup(struct iwl_priv *priv)
205
{
206 207 208 209 210 211
	if (priv->testmode_mem.read_in_progress) {
		kfree(priv->testmode_mem.buff_addr);
		priv->testmode_mem.buff_addr = NULL;
		priv->testmode_mem.buff_size = 0;
		priv->testmode_mem.num_chunks = 0;
		priv->testmode_mem.read_in_progress = false;
212
	}
W
Wey-Yi Guy 已提交
213 214 215 216 217 218 219
}

static void iwl_trace_cleanup(struct iwl_priv *priv)
{
	if (priv->testmode_trace.trace_enabled) {
		if (priv->testmode_trace.cpu_addr &&
		    priv->testmode_trace.dma_addr)
220
			dma_free_coherent(priv->trans->dev,
221
					priv->testmode_trace.total_size,
W
Wey-Yi Guy 已提交
222 223 224 225 226 227
					priv->testmode_trace.cpu_addr,
					priv->testmode_trace.dma_addr);
		priv->testmode_trace.trace_enabled = false;
		priv->testmode_trace.cpu_addr = NULL;
		priv->testmode_trace.trace_addr = NULL;
		priv->testmode_trace.dma_addr = 0;
228 229
		priv->testmode_trace.buff_size = 0;
		priv->testmode_trace.total_size = 0;
W
Wey-Yi Guy 已提交
230 231 232 233 234 235 236
	}
}


void iwl_testmode_cleanup(struct iwl_priv *priv)
{
	iwl_trace_cleanup(priv);
237
	iwl_mem_cleanup(priv);
238 239
}

240

241 242 243 244 245 246 247 248
/*
 * This function handles the user application commands to the ucode.
 *
 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
 * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
 * host command to the ucode.
 *
 * If any mandatory field is missing, -ENOMSG is replied to the user space
249 250 251 252
 * application; otherwise, waits for the host command to be sent and checks
 * the return code. In case or error, it is returned, otherwise a reply is
 * allocated and the reply RX packet
 * is returned.
253 254 255 256 257 258
 *
 * @hw: ieee80211_hw object that represents the device
 * @tb: gnl message fields from the user space
 */
static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
{
259
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
260
	struct iwl_host_cmd cmd;
261 262 263 264 265 266
	struct iwl_rx_packet *pkt;
	struct sk_buff *skb;
	void *reply_buf;
	u32 reply_len;
	int ret;
	bool cmd_want_skb;
267 268 269 270 271

	memset(&cmd, 0, sizeof(struct iwl_host_cmd));

	if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
	    !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
272
		IWL_ERR(priv, "Missing ucode command mandatory fields\n");
273 274 275
		return -ENOMSG;
	}

276 277 278 279 280
	cmd.flags = CMD_ON_DEMAND | CMD_SYNC;
	cmd_want_skb = nla_get_flag(tb[IWL_TM_ATTR_UCODE_CMD_SKB]);
	if (cmd_want_skb)
		cmd.flags |= CMD_WANT_SKB;

281
	cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
282 283
	cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
	cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
284
	cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
285
	IWL_DEBUG_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
286
				" len %d\n", cmd.id, cmd.flags, cmd.len[0]);
287

288
	ret = iwl_dvm_send_cmd(priv, &cmd);
289 290 291 292 293 294 295 296
	if (ret) {
		IWL_ERR(priv, "Failed to send hcmd\n");
		return ret;
	}
	if (!cmd_want_skb)
		return ret;

	/* Handling return of SKB to the user */
297
	pkt = cmd.resp_pkt;
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	if (!pkt) {
		IWL_ERR(priv, "HCMD received a null response packet\n");
		return ret;
	}

	reply_len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
	skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, reply_len + 20);
	reply_buf = kmalloc(reply_len, GFP_KERNEL);
	if (!skb || !reply_buf) {
		kfree_skb(skb);
		kfree(reply_buf);
		return -ENOMEM;
	}

	/* The reply is in a page, that we cannot send to user space. */
313
	memcpy(reply_buf, &(pkt->hdr), reply_len);
314
	iwl_free_resp(&cmd);
315

316 317 318
	if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT) ||
	    nla_put(skb, IWL_TM_ATTR_UCODE_RX_PKT, reply_len, reply_buf))
		goto nla_put_failure;
319 320 321 322 323
	return cfg80211_testmode_reply(skb);

nla_put_failure:
	IWL_DEBUG_INFO(priv, "Failed creating NL attributes\n");
	return -ENOMSG;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
}


/*
 * This function handles the user application commands for register access.
 *
 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
 * handlers respectively.
 *
 * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
 * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
 * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
 * the success of the command execution.
 *
 * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
 * value is returned with IWL_TM_ATTR_REG_VALUE32.
 *
 * @hw: ieee80211_hw object that represents the device
 * @tb: gnl message fields from the user space
 */
static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
{
346
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
347
	u32 ofs, val32, cmd;
348 349 350 351 352
	u8 val8;
	struct sk_buff *skb;
	int status = 0;

	if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
353
		IWL_ERR(priv, "Missing register offset\n");
354 355 356 357 358
		return -ENOMSG;
	}
	ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
	IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);

359 360 361 362 363 364 365 366
	/* Allow access only to FH/CSR/HBUS in direct mode.
	Since we don't have the upper bounds for the CSR and HBUS segments,
	we will use only the upper bound of FH for sanity check. */
	cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
	if ((cmd == IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32 ||
		cmd == IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32 ||
		cmd == IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8) &&
		(ofs >= FH_MEM_UPPER_BOUND)) {
367
		IWL_ERR(priv, "offset out of segment (0x0 - 0x%x)\n",
368 369 370 371 372
			FH_MEM_UPPER_BOUND);
		return -EINVAL;
	}

	switch (cmd) {
373
	case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
374
		val32 = iwl_read_direct32(priv->trans, ofs);
375 376 377 378
		IWL_INFO(priv, "32bit value to read 0x%x\n", val32);

		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
		if (!skb) {
379
			IWL_ERR(priv, "Memory allocation fail\n");
380 381
			return -ENOMEM;
		}
382 383
		if (nla_put_u32(skb, IWL_TM_ATTR_REG_VALUE32, val32))
			goto nla_put_failure;
384 385
		status = cfg80211_testmode_reply(skb);
		if (status < 0)
386
			IWL_ERR(priv, "Error sending msg : %d\n", status);
387
		break;
388
	case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
389
		if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
390
			IWL_ERR(priv, "Missing value to write\n");
391 392 393 394
			return -ENOMSG;
		} else {
			val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
			IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
395
			iwl_write_direct32(priv->trans, ofs, val32);
396 397
		}
		break;
398
	case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
399
		if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
400
			IWL_ERR(priv, "Missing value to write\n");
401 402 403 404
			return -ENOMSG;
		} else {
			val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
			IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
405
			iwl_write8(priv->trans, ofs, val8);
406 407 408
		}
		break;
	default:
409
		IWL_ERR(priv, "Unknown testmode register command ID\n");
410 411 412 413 414 415 416 417 418 419 420 421 422 423
		return -ENOSYS;
	}

	return status;

nla_put_failure:
	kfree_skb(skb);
	return -EMSGSIZE;
}


static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
{
	struct iwl_notification_wait calib_wait;
424 425 426
	static const u8 calib_complete[] = {
		CALIBRATION_COMPLETE_NOTIFICATION
	};
427 428
	int ret;

429
	iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
430
				   calib_complete, ARRAY_SIZE(calib_complete),
431
				   NULL, NULL);
432
	ret = iwl_init_alive_start(priv);
433
	if (ret) {
434
		IWL_ERR(priv, "Fail init calibration: %d\n", ret);
435 436 437
		goto cfg_init_calib_error;
	}

438
	ret = iwl_wait_notification(&priv->notif_wait, &calib_wait, 2 * HZ);
439
	if (ret)
440
		IWL_ERR(priv, "Error detecting"
441 442 443 444
			" CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
	return ret;

cfg_init_calib_error:
445
	iwl_remove_notification(&priv->notif_wait, &calib_wait);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	return ret;
}

/*
 * This function handles the user application commands for driver.
 *
 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
 * handlers respectively.
 *
 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
 * value of the actual command execution is replied to the user application.
 *
 * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
 * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
 * IWL_TM_CMD_DEV2APP_SYNC_RSP.
 *
 * @hw: ieee80211_hw object that represents the device
 * @tb: gnl message fields from the user space
 */
static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
{
467
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
468
	struct iwl_trans *trans = priv->trans;
469 470 471
	struct sk_buff *skb;
	unsigned char *rsp_data_ptr = NULL;
	int status = 0, rsp_data_len = 0;
472
	u32 devid, inst_size = 0, data_size = 0;
D
David Spinadel 已提交
473
	const struct fw_img *img;
474 475 476

	switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
	case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
477 478
		rsp_data_ptr = (unsigned char *)priv->cfg->name;
		rsp_data_len = strlen(priv->cfg->name);
479 480 481
		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
							rsp_data_len + 20);
		if (!skb) {
482
			IWL_ERR(priv, "Memory allocation fail\n");
483 484
			return -ENOMEM;
		}
485 486 487 488 489
		if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND,
				IWL_TM_CMD_DEV2APP_SYNC_RSP) ||
		    nla_put(skb, IWL_TM_ATTR_SYNC_RSP,
			    rsp_data_len, rsp_data_ptr))
			goto nla_put_failure;
490 491
		status = cfg80211_testmode_reply(skb);
		if (status < 0)
492
			IWL_ERR(priv, "Error sending msg : %d\n", status);
493 494 495
		break;

	case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
496
		status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
497
		if (status)
498
			IWL_ERR(priv, "Error loading init ucode: %d\n", status);
499 500 501 502
		break;

	case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
		iwl_testmode_cfg_init_calib(priv);
503
		priv->ucode_loaded = false;
504
		iwl_trans_stop_device(trans);
505 506 507
		break;

	case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
508
		status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
509
		if (status) {
510
			IWL_ERR(priv,
511 512 513 514 515
				"Error loading runtime ucode: %d\n", status);
			break;
		}
		status = iwl_alive_start(priv);
		if (status)
516
			IWL_ERR(priv,
517 518 519
				"Error starting the device: %d\n", status);
		break;

520 521
	case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
		iwl_scan_cancel_timeout(priv, 200);
522
		priv->ucode_loaded = false;
523
		iwl_trans_stop_device(trans);
524
		status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
525
		if (status) {
526
			IWL_ERR(priv,
527 528 529 530 531
				"Error loading WOWLAN ucode: %d\n", status);
			break;
		}
		status = iwl_alive_start(priv);
		if (status)
532
			IWL_ERR(priv,
533 534 535
				"Error starting the device: %d\n", status);
		break;

536
	case IWL_TM_CMD_APP2DEV_GET_EEPROM:
537
		if (priv->eeprom_blob) {
538
			skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
539
				priv->eeprom_blob_size + 20);
540
			if (!skb) {
541
				IWL_ERR(priv, "Memory allocation fail\n");
542 543
				return -ENOMEM;
			}
544 545 546
			if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND,
					IWL_TM_CMD_DEV2APP_EEPROM_RSP) ||
			    nla_put(skb, IWL_TM_ATTR_EEPROM,
547 548
				    priv->eeprom_blob_size,
				    priv->eeprom_blob))
549
				goto nla_put_failure;
550 551
			status = cfg80211_testmode_reply(skb);
			if (status < 0)
552 553
				IWL_ERR(priv, "Error sending msg : %d\n",
					status);
554
		} else
555
			return -ENODATA;
556 557
		break;

558 559
	case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
		if (!tb[IWL_TM_ATTR_FIXRATE]) {
560
			IWL_ERR(priv, "Missing fixrate setting\n");
561 562
			return -ENOMSG;
		}
563
		priv->tm_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
564 565
		break;

566
	case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
D
Don Fry 已提交
567
		IWL_INFO(priv, "uCode version raw: 0x%x\n",
568
			 priv->fw->ucode_ver);
569 570 571

		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
		if (!skb) {
572
			IWL_ERR(priv, "Memory allocation fail\n");
573 574
			return -ENOMEM;
		}
575 576 577
		if (nla_put_u32(skb, IWL_TM_ATTR_FW_VERSION,
				priv->fw->ucode_ver))
			goto nla_put_failure;
578 579
		status = cfg80211_testmode_reply(skb);
		if (status < 0)
580
			IWL_ERR(priv, "Error sending msg : %d\n", status);
581 582
		break;

583
	case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
584
		devid = priv->trans->hw_id;
585
		IWL_INFO(priv, "hw version: 0x%x\n", devid);
586 587 588

		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
		if (!skb) {
589
			IWL_ERR(priv, "Memory allocation fail\n");
590 591
			return -ENOMEM;
		}
592 593
		if (nla_put_u32(skb, IWL_TM_ATTR_DEVICE_ID, devid))
			goto nla_put_failure;
594 595
		status = cfg80211_testmode_reply(skb);
		if (status < 0)
596
			IWL_ERR(priv, "Error sending msg : %d\n", status);
597 598
		break;

599 600 601
	case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20 + 8);
		if (!skb) {
602
			IWL_ERR(priv, "Memory allocation fail\n");
603 604
			return -ENOMEM;
		}
605
		if (!priv->ucode_loaded) {
606
			IWL_ERR(priv, "No uCode has not been loaded\n");
607 608
			return -EINVAL;
		} else {
609
			img = &priv->fw->img[priv->cur_ucode];
D
David Spinadel 已提交
610 611
			inst_size = img->sec[IWL_UCODE_SECTION_INST].len;
			data_size = img->sec[IWL_UCODE_SECTION_DATA].len;
612
		}
613
		if (nla_put_u32(skb, IWL_TM_ATTR_FW_TYPE, priv->cur_ucode) ||
614 615 616
		    nla_put_u32(skb, IWL_TM_ATTR_FW_INST_SIZE, inst_size) ||
		    nla_put_u32(skb, IWL_TM_ATTR_FW_DATA_SIZE, data_size))
			goto nla_put_failure;
617 618
		status = cfg80211_testmode_reply(skb);
		if (status < 0)
619
			IWL_ERR(priv, "Error sending msg : %d\n", status);
620 621
		break;

622
	default:
623
		IWL_ERR(priv, "Unknown testmode driver command ID\n");
624 625 626 627 628 629 630 631 632
		return -ENOSYS;
	}
	return status;

nla_put_failure:
	kfree_skb(skb);
	return -EMSGSIZE;
}

W
Wey-Yi Guy 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647

/*
 * This function handles the user application commands for uCode trace
 *
 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
 * handlers respectively.
 *
 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
 * value of the actual command execution is replied to the user application.
 *
 * @hw: ieee80211_hw object that represents the device
 * @tb: gnl message fields from the user space
 */
static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
{
648
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
W
Wey-Yi Guy 已提交
649 650
	struct sk_buff *skb;
	int status = 0;
651
	struct device *dev = priv->trans->dev;
W
Wey-Yi Guy 已提交
652 653 654 655 656 657

	switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
	case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
		if (priv->testmode_trace.trace_enabled)
			return -EBUSY;

658 659 660 661 662 663 664 665 666 667 668 669 670
		if (!tb[IWL_TM_ATTR_TRACE_SIZE])
			priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
		else
			priv->testmode_trace.buff_size =
				nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
		if (!priv->testmode_trace.buff_size)
			return -EINVAL;
		if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
		    priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
			return -EINVAL;

		priv->testmode_trace.total_size =
			priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
W
Wey-Yi Guy 已提交
671 672
		priv->testmode_trace.cpu_addr =
			dma_alloc_coherent(dev,
673
					   priv->testmode_trace.total_size,
W
Wey-Yi Guy 已提交
674 675 676 677 678 679 680 681
					   &priv->testmode_trace.dma_addr,
					   GFP_KERNEL);
		if (!priv->testmode_trace.cpu_addr)
			return -ENOMEM;
		priv->testmode_trace.trace_enabled = true;
		priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
			priv->testmode_trace.cpu_addr, 0x100);
		memset(priv->testmode_trace.trace_addr, 0x03B,
682
			priv->testmode_trace.buff_size);
W
Wey-Yi Guy 已提交
683 684 685
		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
			sizeof(priv->testmode_trace.dma_addr) + 20);
		if (!skb) {
686
			IWL_ERR(priv, "Memory allocation fail\n");
W
Wey-Yi Guy 已提交
687 688 689
			iwl_trace_cleanup(priv);
			return -ENOMEM;
		}
690 691 692 693
		if (nla_put(skb, IWL_TM_ATTR_TRACE_ADDR,
			    sizeof(priv->testmode_trace.dma_addr),
			    (u64 *)&priv->testmode_trace.dma_addr))
			goto nla_put_failure;
W
Wey-Yi Guy 已提交
694 695
		status = cfg80211_testmode_reply(skb);
		if (status < 0) {
696
			IWL_ERR(priv, "Error sending msg : %d\n", status);
W
Wey-Yi Guy 已提交
697
		}
698 699
		priv->testmode_trace.num_chunks =
			DIV_ROUND_UP(priv->testmode_trace.buff_size,
700
				     DUMP_CHUNK_SIZE);
W
Wey-Yi Guy 已提交
701 702 703 704 705 706
		break;

	case IWL_TM_CMD_APP2DEV_END_TRACE:
		iwl_trace_cleanup(priv);
		break;
	default:
707
		IWL_ERR(priv, "Unknown testmode mem command ID\n");
W
Wey-Yi Guy 已提交
708 709 710 711 712 713 714 715 716 717 718 719
		return -ENOSYS;
	}
	return status;

nla_put_failure:
	kfree_skb(skb);
	if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
	    IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
		iwl_trace_cleanup(priv);
	return -EMSGSIZE;
}

720
static int iwl_testmode_trace_dump(struct ieee80211_hw *hw,
721 722 723
				   struct sk_buff *skb,
				   struct netlink_callback *cb)
{
724
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
725 726 727 728 729 730 731
	int idx, length;

	if (priv->testmode_trace.trace_enabled &&
	    priv->testmode_trace.trace_addr) {
		idx = cb->args[4];
		if (idx >= priv->testmode_trace.num_chunks)
			return -ENOENT;
732
		length = DUMP_CHUNK_SIZE;
733
		if (((idx + 1) == priv->testmode_trace.num_chunks) &&
734
		    (priv->testmode_trace.buff_size % DUMP_CHUNK_SIZE))
735
			length = priv->testmode_trace.buff_size %
736
				DUMP_CHUNK_SIZE;
737

738 739 740 741
		if (nla_put(skb, IWL_TM_ATTR_TRACE_DUMP, length,
			    priv->testmode_trace.trace_addr +
			    (DUMP_CHUNK_SIZE * idx)))
			goto nla_put_failure;
742 743 744 745 746 747 748 749 750 751
		idx++;
		cb->args[4] = idx;
		return 0;
	} else
		return -EFAULT;

 nla_put_failure:
	return -ENOBUFS;
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
/*
 * This function handles the user application switch ucode ownership.
 *
 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
 * decide who the current owner of the uCode
 *
 * If the current owner is OWNERSHIP_TM, then the only host command
 * can deliver to uCode is from testmode, all the other host commands
 * will dropped.
 *
 * default driver is the owner of uCode in normal operational mode
 *
 * @hw: ieee80211_hw object that represents the device
 * @tb: gnl message fields from the user space
 */
static int iwl_testmode_ownership(struct ieee80211_hw *hw, struct nlattr **tb)
{
769
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
770 771 772
	u8 owner;

	if (!tb[IWL_TM_ATTR_UCODE_OWNER]) {
773
		IWL_ERR(priv, "Missing ucode owner\n");
774 775 776 777
		return -ENOMSG;
	}

	owner = nla_get_u8(tb[IWL_TM_ATTR_UCODE_OWNER]);
778
	if (owner == IWL_OWNERSHIP_DRIVER) {
779
		priv->ucode_owner = owner;
780 781 782 783 784
		priv->pre_rx_handler = NULL;
	} else if (owner == IWL_OWNERSHIP_TM) {
		priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
		priv->ucode_owner = owner;
	} else {
785
		IWL_ERR(priv, "Invalid owner\n");
786 787 788 789 790
		return -EINVAL;
	}
	return 0;
}

791 792
static int iwl_testmode_indirect_read(struct iwl_priv *priv, u32 addr, u32 size)
{
793
	struct iwl_trans *trans = priv->trans;
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
	unsigned long flags;
	int i;

	if (size & 0x3)
		return -EINVAL;
	priv->testmode_mem.buff_size = size;
	priv->testmode_mem.buff_addr =
		kmalloc(priv->testmode_mem.buff_size, GFP_KERNEL);
	if (priv->testmode_mem.buff_addr == NULL)
		return -ENOMEM;

	/* Hard-coded periphery absolute address */
	if (IWL_TM_ABS_PRPH_START <= addr &&
		addr < IWL_TM_ABS_PRPH_START + PRPH_END) {
			spin_lock_irqsave(&trans->reg_lock, flags);
			iwl_grab_nic_access(trans);
810 811
			iwl_write32(trans, HBUS_TARG_PRPH_RADDR,
				addr | (3 << 24));
812
			for (i = 0; i < size; i += 4)
813
				*(u32 *)(priv->testmode_mem.buff_addr + i) =
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
					iwl_read32(trans, HBUS_TARG_PRPH_RDAT);
			iwl_release_nic_access(trans);
			spin_unlock_irqrestore(&trans->reg_lock, flags);
	} else { /* target memory (SRAM) */
		_iwl_read_targ_mem_words(trans, addr,
			priv->testmode_mem.buff_addr,
			priv->testmode_mem.buff_size / 4);
	}

	priv->testmode_mem.num_chunks =
		DIV_ROUND_UP(priv->testmode_mem.buff_size, DUMP_CHUNK_SIZE);
	priv->testmode_mem.read_in_progress = true;
	return 0;

}

static int iwl_testmode_indirect_write(struct iwl_priv *priv, u32 addr,
	u32 size, unsigned char *buf)
{
833
	struct iwl_trans *trans = priv->trans;
834 835 836 837 838 839 840 841 842 843 844
	u32 val, i;
	unsigned long flags;

	if (IWL_TM_ABS_PRPH_START <= addr &&
		addr < IWL_TM_ABS_PRPH_START + PRPH_END) {
			/* Periphery writes can be 1-3 bytes long, or DWORDs */
			if (size < 4) {
				memcpy(&val, buf, size);
				spin_lock_irqsave(&trans->reg_lock, flags);
				iwl_grab_nic_access(trans);
				iwl_write32(trans, HBUS_TARG_PRPH_WADDR,
845 846
					    (addr & 0x0000FFFF) |
					    ((size - 1) << 24));
847 848 849 850 851 852 853 854 855 856
				iwl_write32(trans, HBUS_TARG_PRPH_WDAT, val);
				iwl_release_nic_access(trans);
				/* needed after consecutive writes w/o read */
				mmiowb();
				spin_unlock_irqrestore(&trans->reg_lock, flags);
			} else {
				if (size % 4)
					return -EINVAL;
				for (i = 0; i < size; i += 4)
					iwl_write_prph(trans, addr+i,
857
						*(u32 *)(buf+i));
858 859 860 861 862 863 864 865 866 867
			}
	} else if (iwlagn_hw_valid_rtc_data_addr(addr) ||
		(IWLAGN_RTC_INST_LOWER_BOUND <= addr &&
		addr < IWLAGN_RTC_INST_UPPER_BOUND)) {
			_iwl_write_targ_mem_words(trans, addr, buf, size/4);
	} else
		return -EINVAL;
	return 0;
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
/*
 * This function handles the user application commands for SRAM data dump
 *
 * It retrieves the mandatory fields IWL_TM_ATTR_SRAM_ADDR and
 * IWL_TM_ATTR_SRAM_SIZE to decide the memory area for SRAM data reading
 *
 * Several error will be retured, -EBUSY if the SRAM data retrieved by
 * previous command has not been delivered to userspace, or -ENOMSG if
 * the mandatory fields (IWL_TM_ATTR_SRAM_ADDR,IWL_TM_ATTR_SRAM_SIZE)
 * are missing, or -ENOMEM if the buffer allocation fails.
 *
 * Otherwise 0 is replied indicating the success of the SRAM reading.
 *
 * @hw: ieee80211_hw object that represents the device
 * @tb: gnl message fields from the user space
 */
884 885
static int iwl_testmode_indirect_mem(struct ieee80211_hw *hw,
	struct nlattr **tb)
886
{
887
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
888 889
	u32 addr, size, cmd;
	unsigned char *buf;
890

891 892
	/* Both read and write should be blocked, for atomicity */
	if (priv->testmode_mem.read_in_progress)
893 894
		return -EBUSY;

895 896 897
	cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
	if (!tb[IWL_TM_ATTR_MEM_ADDR]) {
		IWL_ERR(priv, "Error finding memory offset address\n");
898 899
		return -ENOMSG;
	}
900 901 902
	addr = nla_get_u32(tb[IWL_TM_ATTR_MEM_ADDR]);
	if (!tb[IWL_TM_ATTR_BUFFER_SIZE]) {
		IWL_ERR(priv, "Error finding size for memory reading\n");
903 904
		return -ENOMSG;
	}
905 906 907 908 909 910 911 912 913
	size = nla_get_u32(tb[IWL_TM_ATTR_BUFFER_SIZE]);

	if (cmd == IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ)
		return iwl_testmode_indirect_read(priv, addr,  size);
	else {
		if (!tb[IWL_TM_ATTR_BUFFER_DUMP])
			return -EINVAL;
		buf = (unsigned char *) nla_data(tb[IWL_TM_ATTR_BUFFER_DUMP]);
		return iwl_testmode_indirect_write(priv, addr, size, buf);
914 915 916
	}
}

917 918 919
static int iwl_testmode_buffer_dump(struct ieee80211_hw *hw,
				    struct sk_buff *skb,
				    struct netlink_callback *cb)
920
{
921
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
922 923
	int idx, length;

924
	if (priv->testmode_mem.read_in_progress) {
925
		idx = cb->args[4];
926 927
		if (idx >= priv->testmode_mem.num_chunks) {
			iwl_mem_cleanup(priv);
928 929
			return -ENOENT;
		}
930
		length = DUMP_CHUNK_SIZE;
931 932 933
		if (((idx + 1) == priv->testmode_mem.num_chunks) &&
		    (priv->testmode_mem.buff_size % DUMP_CHUNK_SIZE))
			length = priv->testmode_mem.buff_size %
934
				DUMP_CHUNK_SIZE;
935

936 937 938 939
		if (nla_put(skb, IWL_TM_ATTR_BUFFER_DUMP, length,
			    priv->testmode_mem.buff_addr +
			    (DUMP_CHUNK_SIZE * idx)))
			goto nla_put_failure;
940 941 942 943 944 945 946 947 948 949
		idx++;
		cb->args[4] = idx;
		return 0;
	} else
		return -EFAULT;

 nla_put_failure:
	return -ENOBUFS;
}

950 951 952 953 954 955 956 957 958 959 960 961 962 963
static int iwl_testmode_notifications(struct ieee80211_hw *hw,
	struct nlattr **tb)
{
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
	bool enable;

	enable = nla_get_flag(tb[IWL_TM_ATTR_ENABLE_NOTIFICATION]);
	if (enable)
		priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
	else
		priv->pre_rx_handler = NULL;
	return 0;
}

964

965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
/* The testmode gnl message handler that takes the gnl message from the
 * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
 * invoke the corresponding handlers.
 *
 * This function is invoked when there is user space application sending
 * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
 * by nl80211.
 *
 * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
 * dispatching it to the corresponding handler.
 *
 * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
 * -ENOSYS is replied to the user application if the command is unknown;
 * Otherwise, the command is dispatched to the respective handler.
 *
 * @hw: ieee80211_hw object that represents the device
 * @data: pointer to user space message
 * @len: length in byte of @data
 */
984
int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
985
{
986
	struct nlattr *tb[IWL_TM_ATTR_MAX];
987
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
988 989 990 991 992
	int result;

	result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
			iwl_testmode_gnl_msg_policy);
	if (result != 0) {
993
		IWL_ERR(priv, "Error parsing the gnl message : %d\n", result);
994 995 996 997 998
		return result;
	}

	/* IWL_TM_ATTR_COMMAND is absolutely mandatory */
	if (!tb[IWL_TM_ATTR_COMMAND]) {
999
		IWL_ERR(priv, "Missing testmode command type\n");
1000 1001 1002
		return -ENOMSG;
	}
	/* in case multiple accesses to the device happens */
1003
	mutex_lock(&priv->mutex);
1004 1005 1006 1007 1008 1009

	switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
	case IWL_TM_CMD_APP2DEV_UCODE:
		IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
		result = iwl_testmode_ucode(hw, tb);
		break;
1010 1011 1012
	case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
	case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
	case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
1013 1014 1015 1016 1017 1018 1019
		IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
		result = iwl_testmode_reg(hw, tb);
		break;
	case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
	case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
	case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
	case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
1020
	case IWL_TM_CMD_APP2DEV_GET_EEPROM:
1021
	case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
1022
	case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
1023
	case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
1024
	case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
1025
	case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
1026 1027 1028
		IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
		result = iwl_testmode_driver(hw, tb);
		break;
W
Wey-Yi Guy 已提交
1029 1030 1031 1032 1033 1034 1035 1036

	case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
	case IWL_TM_CMD_APP2DEV_END_TRACE:
	case IWL_TM_CMD_APP2DEV_READ_TRACE:
		IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
		result = iwl_testmode_trace(hw, tb);
		break;

1037 1038 1039 1040 1041
	case IWL_TM_CMD_APP2DEV_OWNERSHIP:
		IWL_DEBUG_INFO(priv, "testmode change uCode ownership\n");
		result = iwl_testmode_ownership(hw, tb);
		break;

1042 1043 1044 1045 1046
	case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ:
	case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_WRITE:
		IWL_DEBUG_INFO(priv, "testmode indirect memory cmd "
			"to driver\n");
		result = iwl_testmode_indirect_mem(hw, tb);
1047 1048
		break;

1049 1050 1051 1052 1053 1054
	case IWL_TM_CMD_APP2DEV_NOTIFICATIONS:
		IWL_DEBUG_INFO(priv, "testmode notifications cmd "
			"to driver\n");
		result = iwl_testmode_notifications(hw, tb);
		break;

1055
	default:
1056
		IWL_ERR(priv, "Unknown testmode command\n");
1057 1058 1059 1060
		result = -ENOSYS;
		break;
	}

1061
	mutex_unlock(&priv->mutex);
1062 1063
	return result;
}
1064

1065
int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
1066 1067 1068 1069
		      struct netlink_callback *cb,
		      void *data, int len)
{
	struct nlattr *tb[IWL_TM_ATTR_MAX];
1070
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	int result;
	u32 cmd;

	if (cb->args[3]) {
		/* offset by 1 since commands start at 0 */
		cmd = cb->args[3] - 1;
	} else {
		result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
				iwl_testmode_gnl_msg_policy);
		if (result) {
1081 1082
			IWL_ERR(priv,
				"Error parsing the gnl message : %d\n", result);
1083 1084 1085 1086 1087
			return result;
		}

		/* IWL_TM_ATTR_COMMAND is absolutely mandatory */
		if (!tb[IWL_TM_ATTR_COMMAND]) {
1088
			IWL_ERR(priv, "Missing testmode command type\n");
1089 1090 1091 1092 1093 1094 1095
			return -ENOMSG;
		}
		cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
		cb->args[3] = cmd + 1;
	}

	/* in case multiple accesses to the device happens */
1096
	mutex_lock(&priv->mutex);
1097 1098 1099
	switch (cmd) {
	case IWL_TM_CMD_APP2DEV_READ_TRACE:
		IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
1100
		result = iwl_testmode_trace_dump(hw, skb, cb);
1101
		break;
1102
	case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_DUMP:
1103
		IWL_DEBUG_INFO(priv, "testmode sram dump cmd to driver\n");
1104
		result = iwl_testmode_buffer_dump(hw, skb, cb);
1105
		break;
1106 1107 1108 1109 1110
	default:
		result = -EINVAL;
		break;
	}

1111
	mutex_unlock(&priv->mutex);
1112 1113
	return result;
}