1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
63 #include <linux/init.h>
64 #include <linux/kernel.h>
65 #include <linux/module.h>
66 #include <linux/dma-mapping.h>
67 #include <net/net_namespace.h>
68 #include <linux/netdevice.h>
69 #include <net/cfg80211.h>
70 #include <net/mac80211.h>
71 #include <net/netlink.h>
76 #include "iwl-debug.h"
79 #include "iwl-testmode.h"
80 #include "iwl-trans.h"
83 /* The TLVs used in the gnl message policy between the kernel module and
84 * user space application. iwl_testmode_gnl_msg_policy is to be carried
85 * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
89 struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
90 [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
92 [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
93 [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
95 [IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
96 [IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
97 [IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },
99 [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
100 [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
102 [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
104 [IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
105 [IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
106 [IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
108 [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
110 [IWL_TM_ATTR_UCODE_OWNER] = { .type = NLA_U8, },
112 [IWL_TM_ATTR_SRAM_ADDR] = { .type = NLA_U32, },
113 [IWL_TM_ATTR_SRAM_SIZE] = { .type = NLA_U32, },
114 [IWL_TM_ATTR_SRAM_DUMP] = { .type = NLA_UNSPEC, },
116 [IWL_TM_ATTR_FW_VERSION] = { .type = NLA_U32, },
117 [IWL_TM_ATTR_DEVICE_ID] = { .type = NLA_U32, },
121 * See the struct iwl_rx_packet in iwl-commands.h for the format of the
122 * received events from the device
124 static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
126 struct iwl_rx_packet *pkt = rxb_addr(rxb);
128 return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
135 * This function multicasts the spontaneous messages from the device to the
136 * user space. It is invoked whenever there is a received messages
137 * from the device. This function is called within the ISR of the rx handlers
140 * The parsing of the message content is left to the user space application,
141 * The message content is treated as unattacked raw data and is encapsulated
142 * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
144 * @priv: the instance of iwlwifi device
145 * @rxb: pointer to rx data content received by the ISR
147 * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
148 * For the messages multicasting to the user application, the mandatory
150 * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
151 * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
154 static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
155 struct iwl_rx_mem_buffer *rxb)
157 struct ieee80211_hw *hw = priv->hw;
162 data = (void *)rxb_addr(rxb);
163 length = get_event_length(rxb);
165 if (!data || length == 0)
168 skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
172 "Run out of memory for messages to user space ?\n");
175 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT);
176 NLA_PUT(skb, IWL_TM_ATTR_UCODE_RX_PKT, length, data);
177 cfg80211_testmode_event(skb, GFP_ATOMIC);
182 IWL_DEBUG_INFO(priv, "Ouch, overran buffer, check allocation!\n");
185 void iwl_testmode_init(struct iwl_priv *priv)
187 priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
188 priv->testmode_trace.trace_enabled = false;
189 priv->testmode_sram.sram_readed = false;
192 static void iwl_sram_cleanup(struct iwl_priv *priv)
194 if (priv->testmode_sram.sram_readed) {
195 kfree(priv->testmode_sram.buff_addr);
196 priv->testmode_sram.buff_addr = NULL;
197 priv->testmode_sram.buff_size = 0;
198 priv->testmode_sram.num_chunks = 0;
199 priv->testmode_sram.sram_readed = false;
203 static void iwl_trace_cleanup(struct iwl_priv *priv)
205 if (priv->testmode_trace.trace_enabled) {
206 if (priv->testmode_trace.cpu_addr &&
207 priv->testmode_trace.dma_addr)
208 dma_free_coherent(bus(priv)->dev,
209 priv->testmode_trace.total_size,
210 priv->testmode_trace.cpu_addr,
211 priv->testmode_trace.dma_addr);
212 priv->testmode_trace.trace_enabled = false;
213 priv->testmode_trace.cpu_addr = NULL;
214 priv->testmode_trace.trace_addr = NULL;
215 priv->testmode_trace.dma_addr = 0;
216 priv->testmode_trace.buff_size = 0;
217 priv->testmode_trace.total_size = 0;
222 void iwl_testmode_cleanup(struct iwl_priv *priv)
224 iwl_trace_cleanup(priv);
225 iwl_sram_cleanup(priv);
229 * This function handles the user application commands to the ucode.
231 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
232 * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
233 * host command to the ucode.
235 * If any mandatory field is missing, -ENOMSG is replied to the user space
236 * application; otherwise, the actual execution result of the host command to
239 * @hw: ieee80211_hw object that represents the device
240 * @tb: gnl message fields from the user space
242 static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
244 struct iwl_priv *priv = hw->priv;
245 struct iwl_host_cmd cmd;
247 memset(&cmd, 0, sizeof(struct iwl_host_cmd));
249 if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
250 !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
252 "Error finding ucode command mandatory fields\n");
256 cmd.flags = CMD_ON_DEMAND;
257 cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
258 cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
259 cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
260 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
261 IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
262 " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
263 /* ok, let's submit the command to ucode */
264 return iwl_trans_send_cmd(trans(priv), &cmd);
269 * This function handles the user application commands for register access.
271 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
272 * handlers respectively.
274 * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
275 * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
276 * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
277 * the success of the command execution.
279 * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
280 * value is returned with IWL_TM_ATTR_REG_VALUE32.
282 * @hw: ieee80211_hw object that represents the device
283 * @tb: gnl message fields from the user space
285 static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
287 struct iwl_priv *priv = hw->priv;
293 if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
294 IWL_DEBUG_INFO(priv, "Error finding register offset\n");
297 ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
298 IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
300 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
301 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
302 val32 = iwl_read32(bus(priv), ofs);
303 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
305 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
307 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
310 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
311 status = cfg80211_testmode_reply(skb);
314 "Error sending msg : %d\n", status);
316 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
317 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
319 "Error finding value to write\n");
322 val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
323 IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
324 iwl_write32(bus(priv), ofs, val32);
327 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
328 if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
329 IWL_DEBUG_INFO(priv, "Error finding value to write\n");
332 val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
333 IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
334 iwl_write8(bus(priv), ofs, val8);
337 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32:
338 val32 = iwl_read_prph(bus(priv), ofs);
339 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
341 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
343 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
346 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
347 status = cfg80211_testmode_reply(skb);
350 "Error sending msg : %d\n", status);
352 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32:
353 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
355 "Error finding value to write\n");
358 val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
359 IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
360 iwl_write_prph(bus(priv), ofs, val32);
364 IWL_DEBUG_INFO(priv, "Unknown testmode register command ID\n");
376 static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
378 struct iwl_notification_wait calib_wait;
381 iwl_init_notification_wait(priv->shrd, &calib_wait,
382 CALIBRATION_COMPLETE_NOTIFICATION,
384 ret = iwl_init_alive_start(trans(priv));
387 "Error configuring init calibration: %d\n", ret);
388 goto cfg_init_calib_error;
391 ret = iwl_wait_notification(priv->shrd, &calib_wait, 2 * HZ);
393 IWL_DEBUG_INFO(priv, "Error detecting"
394 " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
397 cfg_init_calib_error:
398 iwl_remove_notification(priv->shrd, &calib_wait);
403 * This function handles the user application commands for driver.
405 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
406 * handlers respectively.
408 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
409 * value of the actual command execution is replied to the user application.
411 * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
412 * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
413 * IWL_TM_CMD_DEV2APP_SYNC_RSP.
415 * @hw: ieee80211_hw object that represents the device
416 * @tb: gnl message fields from the user space
418 static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
420 struct iwl_priv *priv = hw->priv;
421 struct iwl_trans *trans = trans(priv);
423 unsigned char *rsp_data_ptr = NULL;
424 int status = 0, rsp_data_len = 0;
425 char buf[32], *ptr = NULL;
426 unsigned int num, devid;
428 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
429 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
430 rsp_data_ptr = (unsigned char *)cfg(priv)->name;
431 rsp_data_len = strlen(cfg(priv)->name);
432 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
436 "Error allocating memory\n");
439 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
440 IWL_TM_CMD_DEV2APP_SYNC_RSP);
441 NLA_PUT(skb, IWL_TM_ATTR_SYNC_RSP,
442 rsp_data_len, rsp_data_ptr);
443 status = cfg80211_testmode_reply(skb);
445 IWL_DEBUG_INFO(priv, "Error sending msg : %d\n",
449 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
450 status = iwl_load_ucode_wait_alive(trans, IWL_UCODE_INIT);
453 "Error loading init ucode: %d\n", status);
456 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
457 iwl_testmode_cfg_init_calib(priv);
458 iwl_trans_stop_device(trans);
461 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
462 status = iwl_load_ucode_wait_alive(trans, IWL_UCODE_REGULAR);
465 "Error loading runtime ucode: %d\n", status);
468 status = iwl_alive_start(priv);
471 "Error starting the device: %d\n", status);
474 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
475 iwl_scan_cancel_timeout(priv, 200);
476 iwl_trans_stop_device(trans);
477 status = iwl_load_ucode_wait_alive(trans, IWL_UCODE_WOWLAN);
480 "Error loading WOWLAN ucode: %d\n", status);
483 status = iwl_alive_start(priv);
486 "Error starting the device: %d\n", status);
489 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
490 if (priv->shrd->eeprom) {
491 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
492 cfg(priv)->base_params->eeprom_size + 20);
495 "Error allocating memory\n");
498 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
499 IWL_TM_CMD_DEV2APP_EEPROM_RSP);
500 NLA_PUT(skb, IWL_TM_ATTR_EEPROM,
501 cfg(priv)->base_params->eeprom_size,
503 status = cfg80211_testmode_reply(skb);
506 "Error sending msg : %d\n",
512 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
513 if (!tb[IWL_TM_ATTR_FIXRATE]) {
515 "Error finding fixrate setting\n");
518 priv->tm_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
521 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
522 IWL_INFO(priv, "uCode version raw: 0x%x\n", priv->ucode_ver);
524 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
526 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
529 NLA_PUT_U32(skb, IWL_TM_ATTR_FW_VERSION, priv->ucode_ver);
530 status = cfg80211_testmode_reply(skb);
533 "Error sending msg : %d\n", status);
536 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
537 bus_get_hw_id(bus(priv), buf, sizeof(buf));
540 sscanf(strsep(&ptr, ":"), "%x", &num);
541 sscanf(strsep(&ptr, ":"), "%x", &devid);
542 IWL_INFO(priv, "Device ID = 0x%04x, SubDevice ID= 0x%04x\n",
544 devid |= (num << 16);
546 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
548 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
551 NLA_PUT_U32(skb, IWL_TM_ATTR_DEVICE_ID, devid);
552 status = cfg80211_testmode_reply(skb);
555 "Error sending msg : %d\n", status);
559 IWL_DEBUG_INFO(priv, "Unknown testmode driver command ID\n");
571 * This function handles the user application commands for uCode trace
573 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
574 * handlers respectively.
576 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
577 * value of the actual command execution is replied to the user application.
579 * @hw: ieee80211_hw object that represents the device
580 * @tb: gnl message fields from the user space
582 static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
584 struct iwl_priv *priv = hw->priv;
587 struct device *dev = bus(priv)->dev;
589 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
590 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
591 if (priv->testmode_trace.trace_enabled)
594 if (!tb[IWL_TM_ATTR_TRACE_SIZE])
595 priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
597 priv->testmode_trace.buff_size =
598 nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
599 if (!priv->testmode_trace.buff_size)
601 if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
602 priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
605 priv->testmode_trace.total_size =
606 priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
607 priv->testmode_trace.cpu_addr =
608 dma_alloc_coherent(dev,
609 priv->testmode_trace.total_size,
610 &priv->testmode_trace.dma_addr,
612 if (!priv->testmode_trace.cpu_addr)
614 priv->testmode_trace.trace_enabled = true;
615 priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
616 priv->testmode_trace.cpu_addr, 0x100);
617 memset(priv->testmode_trace.trace_addr, 0x03B,
618 priv->testmode_trace.buff_size);
619 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
620 sizeof(priv->testmode_trace.dma_addr) + 20);
623 "Error allocating memory\n");
624 iwl_trace_cleanup(priv);
627 NLA_PUT(skb, IWL_TM_ATTR_TRACE_ADDR,
628 sizeof(priv->testmode_trace.dma_addr),
629 (u64 *)&priv->testmode_trace.dma_addr);
630 status = cfg80211_testmode_reply(skb);
633 "Error sending msg : %d\n",
636 priv->testmode_trace.num_chunks =
637 DIV_ROUND_UP(priv->testmode_trace.buff_size,
641 case IWL_TM_CMD_APP2DEV_END_TRACE:
642 iwl_trace_cleanup(priv);
645 IWL_DEBUG_INFO(priv, "Unknown testmode mem command ID\n");
652 if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
653 IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
654 iwl_trace_cleanup(priv);
658 static int iwl_testmode_trace_dump(struct ieee80211_hw *hw, struct nlattr **tb,
660 struct netlink_callback *cb)
662 struct iwl_priv *priv = hw->priv;
665 if (priv->testmode_trace.trace_enabled &&
666 priv->testmode_trace.trace_addr) {
668 if (idx >= priv->testmode_trace.num_chunks)
670 length = DUMP_CHUNK_SIZE;
671 if (((idx + 1) == priv->testmode_trace.num_chunks) &&
672 (priv->testmode_trace.buff_size % DUMP_CHUNK_SIZE))
673 length = priv->testmode_trace.buff_size %
676 NLA_PUT(skb, IWL_TM_ATTR_TRACE_DUMP, length,
677 priv->testmode_trace.trace_addr +
678 (DUMP_CHUNK_SIZE * idx));
690 * This function handles the user application switch ucode ownership.
692 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
693 * decide who the current owner of the uCode
695 * If the current owner is OWNERSHIP_TM, then the only host command
696 * can deliver to uCode is from testmode, all the other host commands
699 * default driver is the owner of uCode in normal operational mode
701 * @hw: ieee80211_hw object that represents the device
702 * @tb: gnl message fields from the user space
704 static int iwl_testmode_ownership(struct ieee80211_hw *hw, struct nlattr **tb)
706 struct iwl_priv *priv = hw->priv;
709 if (!tb[IWL_TM_ATTR_UCODE_OWNER]) {
710 IWL_DEBUG_INFO(priv, "Error finding ucode owner\n");
714 owner = nla_get_u8(tb[IWL_TM_ATTR_UCODE_OWNER]);
715 if ((owner == IWL_OWNERSHIP_DRIVER) || (owner == IWL_OWNERSHIP_TM))
716 priv->shrd->ucode_owner = owner;
718 IWL_DEBUG_INFO(priv, "Invalid owner\n");
725 * This function handles the user application commands for SRAM data dump
727 * It retrieves the mandatory fields IWL_TM_ATTR_SRAM_ADDR and
728 * IWL_TM_ATTR_SRAM_SIZE to decide the memory area for SRAM data reading
730 * Several error will be retured, -EBUSY if the SRAM data retrieved by
731 * previous command has not been delivered to userspace, or -ENOMSG if
732 * the mandatory fields (IWL_TM_ATTR_SRAM_ADDR,IWL_TM_ATTR_SRAM_SIZE)
733 * are missing, or -ENOMEM if the buffer allocation fails.
735 * Otherwise 0 is replied indicating the success of the SRAM reading.
737 * @hw: ieee80211_hw object that represents the device
738 * @tb: gnl message fields from the user space
740 static int iwl_testmode_sram(struct ieee80211_hw *hw, struct nlattr **tb)
742 struct iwl_priv *priv = hw->priv;
743 u32 base, ofs, size, maxsize;
745 if (priv->testmode_sram.sram_readed)
748 if (!tb[IWL_TM_ATTR_SRAM_ADDR]) {
749 IWL_DEBUG_INFO(priv, "Error finding SRAM offset address\n");
752 ofs = nla_get_u32(tb[IWL_TM_ATTR_SRAM_ADDR]);
753 if (!tb[IWL_TM_ATTR_SRAM_SIZE]) {
754 IWL_DEBUG_INFO(priv, "Error finding size for SRAM reading\n");
757 size = nla_get_u32(tb[IWL_TM_ATTR_SRAM_SIZE]);
758 switch (priv->shrd->ucode_type) {
759 case IWL_UCODE_REGULAR:
760 maxsize = trans(priv)->ucode_rt.data.len;
763 maxsize = trans(priv)->ucode_init.data.len;
765 case IWL_UCODE_WOWLAN:
766 maxsize = trans(priv)->ucode_wowlan.data.len;
769 IWL_DEBUG_INFO(priv, "Error, uCode does not been loaded\n");
772 IWL_DEBUG_INFO(priv, "Error, unsupported uCode type\n");
775 if ((ofs + size) > maxsize) {
776 IWL_DEBUG_INFO(priv, "Invalid offset/size: out of range\n");
779 priv->testmode_sram.buff_size = (size / 4) * 4;
780 priv->testmode_sram.buff_addr =
781 kmalloc(priv->testmode_sram.buff_size, GFP_KERNEL);
782 if (priv->testmode_sram.buff_addr == NULL) {
783 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
787 _iwl_read_targ_mem_words(bus(priv), base + ofs,
788 priv->testmode_sram.buff_addr,
789 priv->testmode_sram.buff_size / 4);
790 priv->testmode_sram.num_chunks =
791 DIV_ROUND_UP(priv->testmode_sram.buff_size, DUMP_CHUNK_SIZE);
792 priv->testmode_sram.sram_readed = true;
796 static int iwl_testmode_sram_dump(struct ieee80211_hw *hw, struct nlattr **tb,
798 struct netlink_callback *cb)
800 struct iwl_priv *priv = hw->priv;
803 if (priv->testmode_sram.sram_readed) {
805 if (idx >= priv->testmode_sram.num_chunks) {
806 iwl_sram_cleanup(priv);
809 length = DUMP_CHUNK_SIZE;
810 if (((idx + 1) == priv->testmode_sram.num_chunks) &&
811 (priv->testmode_sram.buff_size % DUMP_CHUNK_SIZE))
812 length = priv->testmode_sram.buff_size %
815 NLA_PUT(skb, IWL_TM_ATTR_SRAM_DUMP, length,
816 priv->testmode_sram.buff_addr +
817 (DUMP_CHUNK_SIZE * idx));
829 /* The testmode gnl message handler that takes the gnl message from the
830 * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
831 * invoke the corresponding handlers.
833 * This function is invoked when there is user space application sending
834 * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
837 * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
838 * dispatching it to the corresponding handler.
840 * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
841 * -ENOSYS is replied to the user application if the command is unknown;
842 * Otherwise, the command is dispatched to the respective handler.
844 * @hw: ieee80211_hw object that represents the device
845 * @data: pointer to user space message
846 * @len: length in byte of @data
848 int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
850 struct nlattr *tb[IWL_TM_ATTR_MAX];
851 struct iwl_priv *priv = hw->priv;
854 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
855 iwl_testmode_gnl_msg_policy);
858 "Error parsing the gnl message : %d\n", result);
862 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
863 if (!tb[IWL_TM_ATTR_COMMAND]) {
864 IWL_DEBUG_INFO(priv, "Error finding testmode command type\n");
867 /* in case multiple accesses to the device happens */
868 mutex_lock(&priv->shrd->mutex);
870 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
871 case IWL_TM_CMD_APP2DEV_UCODE:
872 IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
873 result = iwl_testmode_ucode(hw, tb);
875 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
876 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
877 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
878 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32:
879 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32:
880 IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
881 result = iwl_testmode_reg(hw, tb);
883 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
884 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
885 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
886 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
887 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
888 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
889 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
890 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
891 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
892 IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
893 result = iwl_testmode_driver(hw, tb);
896 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
897 case IWL_TM_CMD_APP2DEV_END_TRACE:
898 case IWL_TM_CMD_APP2DEV_READ_TRACE:
899 IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
900 result = iwl_testmode_trace(hw, tb);
903 case IWL_TM_CMD_APP2DEV_OWNERSHIP:
904 IWL_DEBUG_INFO(priv, "testmode change uCode ownership\n");
905 result = iwl_testmode_ownership(hw, tb);
908 case IWL_TM_CMD_APP2DEV_READ_SRAM:
909 IWL_DEBUG_INFO(priv, "testmode sram read cmd to driver\n");
910 result = iwl_testmode_sram(hw, tb);
914 IWL_DEBUG_INFO(priv, "Unknown testmode command\n");
919 mutex_unlock(&priv->shrd->mutex);
923 int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
924 struct netlink_callback *cb,
927 struct nlattr *tb[IWL_TM_ATTR_MAX];
928 struct iwl_priv *priv = hw->priv;
933 /* offset by 1 since commands start at 0 */
934 cmd = cb->args[3] - 1;
936 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
937 iwl_testmode_gnl_msg_policy);
940 "Error parsing the gnl message : %d\n", result);
944 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
945 if (!tb[IWL_TM_ATTR_COMMAND]) {
947 "Error finding testmode command type\n");
950 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
951 cb->args[3] = cmd + 1;
954 /* in case multiple accesses to the device happens */
955 mutex_lock(&priv->shrd->mutex);
957 case IWL_TM_CMD_APP2DEV_READ_TRACE:
958 IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
959 result = iwl_testmode_trace_dump(hw, tb, skb, cb);
961 case IWL_TM_CMD_APP2DEV_DUMP_SRAM:
962 IWL_DEBUG_INFO(priv, "testmode sram dump cmd to driver\n");
963 result = iwl_testmode_sram_dump(hw, tb, skb, cb);
970 mutex_unlock(&priv->shrd->mutex);