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 - 2012 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 - 2012 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"
84 /* The TLVs used in the gnl message policy between the kernel module and
85 * user space application. iwl_testmode_gnl_msg_policy is to be carried
86 * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
90 struct nla_policy iwl_testmode_gnl_msg_policy
[IWL_TM_ATTR_MAX
] = {
91 [IWL_TM_ATTR_COMMAND
] = { .type
= NLA_U32
, },
93 [IWL_TM_ATTR_UCODE_CMD_ID
] = { .type
= NLA_U8
, },
94 [IWL_TM_ATTR_UCODE_CMD_DATA
] = { .type
= NLA_UNSPEC
, },
96 [IWL_TM_ATTR_REG_OFFSET
] = { .type
= NLA_U32
, },
97 [IWL_TM_ATTR_REG_VALUE8
] = { .type
= NLA_U8
, },
98 [IWL_TM_ATTR_REG_VALUE32
] = { .type
= NLA_U32
, },
100 [IWL_TM_ATTR_SYNC_RSP
] = { .type
= NLA_UNSPEC
, },
101 [IWL_TM_ATTR_UCODE_RX_PKT
] = { .type
= NLA_UNSPEC
, },
103 [IWL_TM_ATTR_EEPROM
] = { .type
= NLA_UNSPEC
, },
105 [IWL_TM_ATTR_TRACE_ADDR
] = { .type
= NLA_UNSPEC
, },
106 [IWL_TM_ATTR_TRACE_DUMP
] = { .type
= NLA_UNSPEC
, },
107 [IWL_TM_ATTR_TRACE_SIZE
] = { .type
= NLA_U32
, },
109 [IWL_TM_ATTR_FIXRATE
] = { .type
= NLA_U32
, },
111 [IWL_TM_ATTR_UCODE_OWNER
] = { .type
= NLA_U8
, },
113 [IWL_TM_ATTR_SRAM_ADDR
] = { .type
= NLA_U32
, },
114 [IWL_TM_ATTR_SRAM_SIZE
] = { .type
= NLA_U32
, },
115 [IWL_TM_ATTR_SRAM_DUMP
] = { .type
= NLA_UNSPEC
, },
117 [IWL_TM_ATTR_FW_VERSION
] = { .type
= NLA_U32
, },
118 [IWL_TM_ATTR_DEVICE_ID
] = { .type
= NLA_U32
, },
119 [IWL_TM_ATTR_FW_TYPE
] = { .type
= NLA_U32
, },
120 [IWL_TM_ATTR_FW_INST_SIZE
] = { .type
= NLA_U32
, },
121 [IWL_TM_ATTR_FW_DATA_SIZE
] = { .type
= NLA_U32
, },
125 * See the struct iwl_rx_packet in iwl-commands.h for the format of the
126 * received events from the device
128 static inline int get_event_length(struct iwl_rx_mem_buffer
*rxb
)
130 struct iwl_rx_packet
*pkt
= rxb_addr(rxb
);
132 return le32_to_cpu(pkt
->len_n_flags
) & FH_RSCSR_FRAME_SIZE_MSK
;
139 * This function multicasts the spontaneous messages from the device to the
140 * user space. It is invoked whenever there is a received messages
141 * from the device. This function is called within the ISR of the rx handlers
144 * The parsing of the message content is left to the user space application,
145 * The message content is treated as unattacked raw data and is encapsulated
146 * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
148 * @priv: the instance of iwlwifi device
149 * @rxb: pointer to rx data content received by the ISR
151 * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
152 * For the messages multicasting to the user application, the mandatory
154 * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
155 * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
158 static void iwl_testmode_ucode_rx_pkt(struct iwl_priv
*priv
,
159 struct iwl_rx_mem_buffer
*rxb
)
161 struct ieee80211_hw
*hw
= priv
->hw
;
166 data
= (void *)rxb_addr(rxb
);
167 length
= get_event_length(rxb
);
169 if (!data
|| length
== 0)
172 skb
= cfg80211_testmode_alloc_event_skb(hw
->wiphy
, 20 + length
,
176 "Run out of memory for messages to user space ?\n");
179 NLA_PUT_U32(skb
, IWL_TM_ATTR_COMMAND
, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
);
180 NLA_PUT(skb
, IWL_TM_ATTR_UCODE_RX_PKT
, length
, data
);
181 cfg80211_testmode_event(skb
, GFP_ATOMIC
);
186 IWL_ERR(priv
, "Ouch, overran buffer, check allocation!\n");
189 void iwl_testmode_init(struct iwl_priv
*priv
)
191 priv
->pre_rx_handler
= iwl_testmode_ucode_rx_pkt
;
192 priv
->testmode_trace
.trace_enabled
= false;
193 priv
->testmode_sram
.sram_readed
= false;
196 static void iwl_sram_cleanup(struct iwl_priv
*priv
)
198 if (priv
->testmode_sram
.sram_readed
) {
199 kfree(priv
->testmode_sram
.buff_addr
);
200 priv
->testmode_sram
.buff_addr
= NULL
;
201 priv
->testmode_sram
.buff_size
= 0;
202 priv
->testmode_sram
.num_chunks
= 0;
203 priv
->testmode_sram
.sram_readed
= false;
207 static void iwl_trace_cleanup(struct iwl_priv
*priv
)
209 if (priv
->testmode_trace
.trace_enabled
) {
210 if (priv
->testmode_trace
.cpu_addr
&&
211 priv
->testmode_trace
.dma_addr
)
212 dma_free_coherent(trans(priv
)->dev
,
213 priv
->testmode_trace
.total_size
,
214 priv
->testmode_trace
.cpu_addr
,
215 priv
->testmode_trace
.dma_addr
);
216 priv
->testmode_trace
.trace_enabled
= false;
217 priv
->testmode_trace
.cpu_addr
= NULL
;
218 priv
->testmode_trace
.trace_addr
= NULL
;
219 priv
->testmode_trace
.dma_addr
= 0;
220 priv
->testmode_trace
.buff_size
= 0;
221 priv
->testmode_trace
.total_size
= 0;
226 void iwl_testmode_cleanup(struct iwl_priv
*priv
)
228 iwl_trace_cleanup(priv
);
229 iwl_sram_cleanup(priv
);
233 * This function handles the user application commands to the ucode.
235 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
236 * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
237 * host command to the ucode.
239 * If any mandatory field is missing, -ENOMSG is replied to the user space
240 * application; otherwise, the actual execution result of the host command to
243 * @hw: ieee80211_hw object that represents the device
244 * @tb: gnl message fields from the user space
246 static int iwl_testmode_ucode(struct ieee80211_hw
*hw
, struct nlattr
**tb
)
248 struct iwl_priv
*priv
= hw
->priv
;
249 struct iwl_host_cmd cmd
;
251 memset(&cmd
, 0, sizeof(struct iwl_host_cmd
));
253 if (!tb
[IWL_TM_ATTR_UCODE_CMD_ID
] ||
254 !tb
[IWL_TM_ATTR_UCODE_CMD_DATA
]) {
255 IWL_ERR(priv
, "Missing ucode command mandatory fields\n");
259 cmd
.flags
= CMD_ON_DEMAND
;
260 cmd
.id
= nla_get_u8(tb
[IWL_TM_ATTR_UCODE_CMD_ID
]);
261 cmd
.data
[0] = nla_data(tb
[IWL_TM_ATTR_UCODE_CMD_DATA
]);
262 cmd
.len
[0] = nla_len(tb
[IWL_TM_ATTR_UCODE_CMD_DATA
]);
263 cmd
.dataflags
[0] = IWL_HCMD_DFL_NOCOPY
;
264 IWL_INFO(priv
, "testmode ucode command ID 0x%x, flags 0x%x,"
265 " len %d\n", cmd
.id
, cmd
.flags
, cmd
.len
[0]);
266 /* ok, let's submit the command to ucode */
267 return iwl_trans_send_cmd(trans(priv
), &cmd
);
272 * This function handles the user application commands for register access.
274 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
275 * handlers respectively.
277 * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
278 * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
279 * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
280 * the success of the command execution.
282 * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
283 * value is returned with IWL_TM_ATTR_REG_VALUE32.
285 * @hw: ieee80211_hw object that represents the device
286 * @tb: gnl message fields from the user space
288 static int iwl_testmode_reg(struct ieee80211_hw
*hw
, struct nlattr
**tb
)
290 struct iwl_priv
*priv
= hw
->priv
;
296 if (!tb
[IWL_TM_ATTR_REG_OFFSET
]) {
297 IWL_ERR(priv
, "Missing register offset\n");
300 ofs
= nla_get_u32(tb
[IWL_TM_ATTR_REG_OFFSET
]);
301 IWL_INFO(priv
, "testmode register access command offset 0x%x\n", ofs
);
303 /* Allow access only to FH/CSR/HBUS in direct mode.
304 Since we don't have the upper bounds for the CSR and HBUS segments,
305 we will use only the upper bound of FH for sanity check. */
306 cmd
= nla_get_u32(tb
[IWL_TM_ATTR_COMMAND
]);
307 if ((cmd
== IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32
||
308 cmd
== IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32
||
309 cmd
== IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8
) &&
310 (ofs
>= FH_MEM_UPPER_BOUND
)) {
311 IWL_ERR(priv
, "offset out of segment (0x0 - 0x%x)\n",
317 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32
:
318 val32
= iwl_read_direct32(trans(priv
), ofs
);
319 IWL_INFO(priv
, "32bit value to read 0x%x\n", val32
);
321 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
, 20);
323 IWL_ERR(priv
, "Memory allocation fail\n");
326 NLA_PUT_U32(skb
, IWL_TM_ATTR_REG_VALUE32
, val32
);
327 status
= cfg80211_testmode_reply(skb
);
329 IWL_ERR(priv
, "Error sending msg : %d\n", status
);
331 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32
:
332 if (!tb
[IWL_TM_ATTR_REG_VALUE32
]) {
333 IWL_ERR(priv
, "Missing value to write\n");
336 val32
= nla_get_u32(tb
[IWL_TM_ATTR_REG_VALUE32
]);
337 IWL_INFO(priv
, "32bit value to write 0x%x\n", val32
);
338 iwl_write_direct32(trans(priv
), ofs
, val32
);
341 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8
:
342 if (!tb
[IWL_TM_ATTR_REG_VALUE8
]) {
343 IWL_ERR(priv
, "Missing value to write\n");
346 val8
= nla_get_u8(tb
[IWL_TM_ATTR_REG_VALUE8
]);
347 IWL_INFO(priv
, "8bit value to write 0x%x\n", val8
);
348 iwl_write8(trans(priv
), ofs
, val8
);
351 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32
:
352 val32
= iwl_read_prph(trans(priv
), ofs
);
353 IWL_INFO(priv
, "32bit value to read 0x%x\n", val32
);
355 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
, 20);
357 IWL_ERR(priv
, "Memory allocation fail\n");
360 NLA_PUT_U32(skb
, IWL_TM_ATTR_REG_VALUE32
, val32
);
361 status
= cfg80211_testmode_reply(skb
);
363 IWL_ERR(priv
, "Error sending msg : %d\n", status
);
365 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32
:
366 if (!tb
[IWL_TM_ATTR_REG_VALUE32
]) {
367 IWL_ERR(priv
, "Missing value to write\n");
370 val32
= nla_get_u32(tb
[IWL_TM_ATTR_REG_VALUE32
]);
371 IWL_INFO(priv
, "32bit value to write 0x%x\n", val32
);
372 iwl_write_prph(trans(priv
), ofs
, val32
);
376 IWL_ERR(priv
, "Unknown testmode register command ID\n");
388 static int iwl_testmode_cfg_init_calib(struct iwl_priv
*priv
)
390 struct iwl_notification_wait calib_wait
;
393 iwl_init_notification_wait(priv
->shrd
, &calib_wait
,
394 CALIBRATION_COMPLETE_NOTIFICATION
,
396 ret
= iwl_init_alive_start(trans(priv
));
398 IWL_ERR(priv
, "Fail init calibration: %d\n", ret
);
399 goto cfg_init_calib_error
;
402 ret
= iwl_wait_notification(priv
->shrd
, &calib_wait
, 2 * HZ
);
404 IWL_ERR(priv
, "Error detecting"
405 " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret
);
408 cfg_init_calib_error
:
409 iwl_remove_notification(priv
->shrd
, &calib_wait
);
414 * This function handles the user application commands for driver.
416 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
417 * handlers respectively.
419 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
420 * value of the actual command execution is replied to the user application.
422 * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
423 * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
424 * IWL_TM_CMD_DEV2APP_SYNC_RSP.
426 * @hw: ieee80211_hw object that represents the device
427 * @tb: gnl message fields from the user space
429 static int iwl_testmode_driver(struct ieee80211_hw
*hw
, struct nlattr
**tb
)
431 struct iwl_priv
*priv
= hw
->priv
;
432 struct iwl_trans
*trans
= trans(priv
);
434 unsigned char *rsp_data_ptr
= NULL
;
435 int status
= 0, rsp_data_len
= 0;
436 u32 devid
, inst_size
= 0, data_size
= 0;
438 switch (nla_get_u32(tb
[IWL_TM_ATTR_COMMAND
])) {
439 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME
:
440 rsp_data_ptr
= (unsigned char *)cfg(priv
)->name
;
441 rsp_data_len
= strlen(cfg(priv
)->name
);
442 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
,
445 IWL_ERR(priv
, "Memory allocation fail\n");
448 NLA_PUT_U32(skb
, IWL_TM_ATTR_COMMAND
,
449 IWL_TM_CMD_DEV2APP_SYNC_RSP
);
450 NLA_PUT(skb
, IWL_TM_ATTR_SYNC_RSP
,
451 rsp_data_len
, rsp_data_ptr
);
452 status
= cfg80211_testmode_reply(skb
);
454 IWL_ERR(priv
, "Error sending msg : %d\n", status
);
457 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW
:
458 status
= iwl_load_ucode_wait_alive(trans
, IWL_UCODE_INIT
);
460 IWL_ERR(priv
, "Error loading init ucode: %d\n", status
);
463 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB
:
464 iwl_testmode_cfg_init_calib(priv
);
465 iwl_trans_stop_device(trans
);
468 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW
:
469 status
= iwl_load_ucode_wait_alive(trans
, IWL_UCODE_REGULAR
);
472 "Error loading runtime ucode: %d\n", status
);
475 status
= iwl_alive_start(priv
);
478 "Error starting the device: %d\n", status
);
481 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW
:
482 iwl_scan_cancel_timeout(priv
, 200);
483 iwl_trans_stop_device(trans
);
484 status
= iwl_load_ucode_wait_alive(trans
, IWL_UCODE_WOWLAN
);
487 "Error loading WOWLAN ucode: %d\n", status
);
490 status
= iwl_alive_start(priv
);
493 "Error starting the device: %d\n", status
);
496 case IWL_TM_CMD_APP2DEV_GET_EEPROM
:
497 if (priv
->shrd
->eeprom
) {
498 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
,
499 cfg(priv
)->base_params
->eeprom_size
+ 20);
501 IWL_ERR(priv
, "Memory allocation fail\n");
504 NLA_PUT_U32(skb
, IWL_TM_ATTR_COMMAND
,
505 IWL_TM_CMD_DEV2APP_EEPROM_RSP
);
506 NLA_PUT(skb
, IWL_TM_ATTR_EEPROM
,
507 cfg(priv
)->base_params
->eeprom_size
,
509 status
= cfg80211_testmode_reply(skb
);
511 IWL_ERR(priv
, "Error sending msg : %d\n",
517 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ
:
518 if (!tb
[IWL_TM_ATTR_FIXRATE
]) {
519 IWL_ERR(priv
, "Missing fixrate setting\n");
522 priv
->tm_fixed_rate
= nla_get_u32(tb
[IWL_TM_ATTR_FIXRATE
]);
525 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION
:
526 IWL_INFO(priv
, "uCode version raw: 0x%x\n", priv
->ucode_ver
);
528 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
, 20);
530 IWL_ERR(priv
, "Memory allocation fail\n");
533 NLA_PUT_U32(skb
, IWL_TM_ATTR_FW_VERSION
, priv
->ucode_ver
);
534 status
= cfg80211_testmode_reply(skb
);
536 IWL_ERR(priv
, "Error sending msg : %d\n", status
);
539 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID
:
540 devid
= trans(priv
)->hw_id
;
541 IWL_INFO(priv
, "hw version: 0x%x\n", devid
);
543 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
, 20);
545 IWL_ERR(priv
, "Memory allocation fail\n");
548 NLA_PUT_U32(skb
, IWL_TM_ATTR_DEVICE_ID
, devid
);
549 status
= cfg80211_testmode_reply(skb
);
551 IWL_ERR(priv
, "Error sending msg : %d\n", status
);
554 case IWL_TM_CMD_APP2DEV_GET_FW_INFO
:
555 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
, 20 + 8);
557 IWL_ERR(priv
, "Memory allocation fail\n");
560 switch (priv
->shrd
->ucode_type
) {
561 case IWL_UCODE_REGULAR
:
562 inst_size
= trans(priv
)->ucode_rt
.code
.len
;
563 data_size
= trans(priv
)->ucode_rt
.data
.len
;
566 inst_size
= trans(priv
)->ucode_init
.code
.len
;
567 data_size
= trans(priv
)->ucode_init
.data
.len
;
569 case IWL_UCODE_WOWLAN
:
570 inst_size
= trans(priv
)->ucode_wowlan
.code
.len
;
571 data_size
= trans(priv
)->ucode_wowlan
.data
.len
;
574 IWL_ERR(priv
, "No uCode has not been loaded\n");
577 IWL_ERR(priv
, "Unsupported uCode type\n");
580 NLA_PUT_U32(skb
, IWL_TM_ATTR_FW_TYPE
, priv
->shrd
->ucode_type
);
581 NLA_PUT_U32(skb
, IWL_TM_ATTR_FW_INST_SIZE
, inst_size
);
582 NLA_PUT_U32(skb
, IWL_TM_ATTR_FW_DATA_SIZE
, data_size
);
583 status
= cfg80211_testmode_reply(skb
);
585 IWL_ERR(priv
, "Error sending msg : %d\n", status
);
589 IWL_ERR(priv
, "Unknown testmode driver command ID\n");
601 * This function handles the user application commands for uCode trace
603 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
604 * handlers respectively.
606 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
607 * value of the actual command execution is replied to the user application.
609 * @hw: ieee80211_hw object that represents the device
610 * @tb: gnl message fields from the user space
612 static int iwl_testmode_trace(struct ieee80211_hw
*hw
, struct nlattr
**tb
)
614 struct iwl_priv
*priv
= hw
->priv
;
617 struct device
*dev
= trans(priv
)->dev
;
619 switch (nla_get_u32(tb
[IWL_TM_ATTR_COMMAND
])) {
620 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE
:
621 if (priv
->testmode_trace
.trace_enabled
)
624 if (!tb
[IWL_TM_ATTR_TRACE_SIZE
])
625 priv
->testmode_trace
.buff_size
= TRACE_BUFF_SIZE_DEF
;
627 priv
->testmode_trace
.buff_size
=
628 nla_get_u32(tb
[IWL_TM_ATTR_TRACE_SIZE
]);
629 if (!priv
->testmode_trace
.buff_size
)
631 if (priv
->testmode_trace
.buff_size
< TRACE_BUFF_SIZE_MIN
||
632 priv
->testmode_trace
.buff_size
> TRACE_BUFF_SIZE_MAX
)
635 priv
->testmode_trace
.total_size
=
636 priv
->testmode_trace
.buff_size
+ TRACE_BUFF_PADD
;
637 priv
->testmode_trace
.cpu_addr
=
638 dma_alloc_coherent(dev
,
639 priv
->testmode_trace
.total_size
,
640 &priv
->testmode_trace
.dma_addr
,
642 if (!priv
->testmode_trace
.cpu_addr
)
644 priv
->testmode_trace
.trace_enabled
= true;
645 priv
->testmode_trace
.trace_addr
= (u8
*)PTR_ALIGN(
646 priv
->testmode_trace
.cpu_addr
, 0x100);
647 memset(priv
->testmode_trace
.trace_addr
, 0x03B,
648 priv
->testmode_trace
.buff_size
);
649 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
,
650 sizeof(priv
->testmode_trace
.dma_addr
) + 20);
652 IWL_ERR(priv
, "Memory allocation fail\n");
653 iwl_trace_cleanup(priv
);
656 NLA_PUT(skb
, IWL_TM_ATTR_TRACE_ADDR
,
657 sizeof(priv
->testmode_trace
.dma_addr
),
658 (u64
*)&priv
->testmode_trace
.dma_addr
);
659 status
= cfg80211_testmode_reply(skb
);
661 IWL_ERR(priv
, "Error sending msg : %d\n", status
);
663 priv
->testmode_trace
.num_chunks
=
664 DIV_ROUND_UP(priv
->testmode_trace
.buff_size
,
668 case IWL_TM_CMD_APP2DEV_END_TRACE
:
669 iwl_trace_cleanup(priv
);
672 IWL_ERR(priv
, "Unknown testmode mem command ID\n");
679 if (nla_get_u32(tb
[IWL_TM_ATTR_COMMAND
]) ==
680 IWL_TM_CMD_APP2DEV_BEGIN_TRACE
)
681 iwl_trace_cleanup(priv
);
685 static int iwl_testmode_trace_dump(struct ieee80211_hw
*hw
, struct nlattr
**tb
,
687 struct netlink_callback
*cb
)
689 struct iwl_priv
*priv
= hw
->priv
;
692 if (priv
->testmode_trace
.trace_enabled
&&
693 priv
->testmode_trace
.trace_addr
) {
695 if (idx
>= priv
->testmode_trace
.num_chunks
)
697 length
= DUMP_CHUNK_SIZE
;
698 if (((idx
+ 1) == priv
->testmode_trace
.num_chunks
) &&
699 (priv
->testmode_trace
.buff_size
% DUMP_CHUNK_SIZE
))
700 length
= priv
->testmode_trace
.buff_size
%
703 NLA_PUT(skb
, IWL_TM_ATTR_TRACE_DUMP
, length
,
704 priv
->testmode_trace
.trace_addr
+
705 (DUMP_CHUNK_SIZE
* idx
));
717 * This function handles the user application switch ucode ownership.
719 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
720 * decide who the current owner of the uCode
722 * If the current owner is OWNERSHIP_TM, then the only host command
723 * can deliver to uCode is from testmode, all the other host commands
726 * default driver is the owner of uCode in normal operational mode
728 * @hw: ieee80211_hw object that represents the device
729 * @tb: gnl message fields from the user space
731 static int iwl_testmode_ownership(struct ieee80211_hw
*hw
, struct nlattr
**tb
)
733 struct iwl_priv
*priv
= hw
->priv
;
736 if (!tb
[IWL_TM_ATTR_UCODE_OWNER
]) {
737 IWL_ERR(priv
, "Missing ucode owner\n");
741 owner
= nla_get_u8(tb
[IWL_TM_ATTR_UCODE_OWNER
]);
742 if ((owner
== IWL_OWNERSHIP_DRIVER
) || (owner
== IWL_OWNERSHIP_TM
))
743 priv
->shrd
->ucode_owner
= owner
;
745 IWL_ERR(priv
, "Invalid owner\n");
752 * This function handles the user application commands for SRAM data dump
754 * It retrieves the mandatory fields IWL_TM_ATTR_SRAM_ADDR and
755 * IWL_TM_ATTR_SRAM_SIZE to decide the memory area for SRAM data reading
757 * Several error will be retured, -EBUSY if the SRAM data retrieved by
758 * previous command has not been delivered to userspace, or -ENOMSG if
759 * the mandatory fields (IWL_TM_ATTR_SRAM_ADDR,IWL_TM_ATTR_SRAM_SIZE)
760 * are missing, or -ENOMEM if the buffer allocation fails.
762 * Otherwise 0 is replied indicating the success of the SRAM reading.
764 * @hw: ieee80211_hw object that represents the device
765 * @tb: gnl message fields from the user space
767 static int iwl_testmode_sram(struct ieee80211_hw
*hw
, struct nlattr
**tb
)
769 struct iwl_priv
*priv
= hw
->priv
;
770 u32 ofs
, size
, maxsize
;
772 if (priv
->testmode_sram
.sram_readed
)
775 if (!tb
[IWL_TM_ATTR_SRAM_ADDR
]) {
776 IWL_ERR(priv
, "Missing SRAM offset address\n");
779 ofs
= nla_get_u32(tb
[IWL_TM_ATTR_SRAM_ADDR
]);
780 if (!tb
[IWL_TM_ATTR_SRAM_SIZE
]) {
781 IWL_ERR(priv
, "Missing size for SRAM reading\n");
784 size
= nla_get_u32(tb
[IWL_TM_ATTR_SRAM_SIZE
]);
785 switch (priv
->shrd
->ucode_type
) {
786 case IWL_UCODE_REGULAR
:
787 maxsize
= trans(priv
)->ucode_rt
.data
.len
;
790 maxsize
= trans(priv
)->ucode_init
.data
.len
;
792 case IWL_UCODE_WOWLAN
:
793 maxsize
= trans(priv
)->ucode_wowlan
.data
.len
;
796 IWL_ERR(priv
, "uCode does not been loaded\n");
799 IWL_ERR(priv
, "unsupported uCode type\n");
802 if ((ofs
+ size
) > (maxsize
+ SRAM_DATA_SEG_OFFSET
)) {
803 IWL_ERR(priv
, "Invalid offset/size: out of range\n");
806 priv
->testmode_sram
.buff_size
= (size
/ 4) * 4;
807 priv
->testmode_sram
.buff_addr
=
808 kmalloc(priv
->testmode_sram
.buff_size
, GFP_KERNEL
);
809 if (priv
->testmode_sram
.buff_addr
== NULL
) {
810 IWL_ERR(priv
, "Memory allocation fail\n");
813 _iwl_read_targ_mem_words(trans(priv
), ofs
,
814 priv
->testmode_sram
.buff_addr
,
815 priv
->testmode_sram
.buff_size
/ 4);
816 priv
->testmode_sram
.num_chunks
=
817 DIV_ROUND_UP(priv
->testmode_sram
.buff_size
, DUMP_CHUNK_SIZE
);
818 priv
->testmode_sram
.sram_readed
= true;
822 static int iwl_testmode_sram_dump(struct ieee80211_hw
*hw
, struct nlattr
**tb
,
824 struct netlink_callback
*cb
)
826 struct iwl_priv
*priv
= hw
->priv
;
829 if (priv
->testmode_sram
.sram_readed
) {
831 if (idx
>= priv
->testmode_sram
.num_chunks
) {
832 iwl_sram_cleanup(priv
);
835 length
= DUMP_CHUNK_SIZE
;
836 if (((idx
+ 1) == priv
->testmode_sram
.num_chunks
) &&
837 (priv
->testmode_sram
.buff_size
% DUMP_CHUNK_SIZE
))
838 length
= priv
->testmode_sram
.buff_size
%
841 NLA_PUT(skb
, IWL_TM_ATTR_SRAM_DUMP
, length
,
842 priv
->testmode_sram
.buff_addr
+
843 (DUMP_CHUNK_SIZE
* idx
));
855 /* The testmode gnl message handler that takes the gnl message from the
856 * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
857 * invoke the corresponding handlers.
859 * This function is invoked when there is user space application sending
860 * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
863 * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
864 * dispatching it to the corresponding handler.
866 * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
867 * -ENOSYS is replied to the user application if the command is unknown;
868 * Otherwise, the command is dispatched to the respective handler.
870 * @hw: ieee80211_hw object that represents the device
871 * @data: pointer to user space message
872 * @len: length in byte of @data
874 int iwlagn_mac_testmode_cmd(struct ieee80211_hw
*hw
, void *data
, int len
)
876 struct nlattr
*tb
[IWL_TM_ATTR_MAX
];
877 struct iwl_priv
*priv
= hw
->priv
;
880 result
= nla_parse(tb
, IWL_TM_ATTR_MAX
- 1, data
, len
,
881 iwl_testmode_gnl_msg_policy
);
883 IWL_ERR(priv
, "Error parsing the gnl message : %d\n", result
);
887 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
888 if (!tb
[IWL_TM_ATTR_COMMAND
]) {
889 IWL_ERR(priv
, "Missing testmode command type\n");
892 /* in case multiple accesses to the device happens */
893 mutex_lock(&priv
->shrd
->mutex
);
895 switch (nla_get_u32(tb
[IWL_TM_ATTR_COMMAND
])) {
896 case IWL_TM_CMD_APP2DEV_UCODE
:
897 IWL_DEBUG_INFO(priv
, "testmode cmd to uCode\n");
898 result
= iwl_testmode_ucode(hw
, tb
);
900 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32
:
901 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32
:
902 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8
:
903 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32
:
904 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32
:
905 IWL_DEBUG_INFO(priv
, "testmode cmd to register\n");
906 result
= iwl_testmode_reg(hw
, tb
);
908 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME
:
909 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW
:
910 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB
:
911 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW
:
912 case IWL_TM_CMD_APP2DEV_GET_EEPROM
:
913 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ
:
914 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW
:
915 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION
:
916 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID
:
917 case IWL_TM_CMD_APP2DEV_GET_FW_INFO
:
918 IWL_DEBUG_INFO(priv
, "testmode cmd to driver\n");
919 result
= iwl_testmode_driver(hw
, tb
);
922 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE
:
923 case IWL_TM_CMD_APP2DEV_END_TRACE
:
924 case IWL_TM_CMD_APP2DEV_READ_TRACE
:
925 IWL_DEBUG_INFO(priv
, "testmode uCode trace cmd to driver\n");
926 result
= iwl_testmode_trace(hw
, tb
);
929 case IWL_TM_CMD_APP2DEV_OWNERSHIP
:
930 IWL_DEBUG_INFO(priv
, "testmode change uCode ownership\n");
931 result
= iwl_testmode_ownership(hw
, tb
);
934 case IWL_TM_CMD_APP2DEV_READ_SRAM
:
935 IWL_DEBUG_INFO(priv
, "testmode sram read cmd to driver\n");
936 result
= iwl_testmode_sram(hw
, tb
);
940 IWL_ERR(priv
, "Unknown testmode command\n");
945 mutex_unlock(&priv
->shrd
->mutex
);
949 int iwlagn_mac_testmode_dump(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
950 struct netlink_callback
*cb
,
953 struct nlattr
*tb
[IWL_TM_ATTR_MAX
];
954 struct iwl_priv
*priv
= hw
->priv
;
959 /* offset by 1 since commands start at 0 */
960 cmd
= cb
->args
[3] - 1;
962 result
= nla_parse(tb
, IWL_TM_ATTR_MAX
- 1, data
, len
,
963 iwl_testmode_gnl_msg_policy
);
966 "Error parsing the gnl message : %d\n", result
);
970 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
971 if (!tb
[IWL_TM_ATTR_COMMAND
]) {
972 IWL_ERR(priv
, "Missing testmode command type\n");
975 cmd
= nla_get_u32(tb
[IWL_TM_ATTR_COMMAND
]);
976 cb
->args
[3] = cmd
+ 1;
979 /* in case multiple accesses to the device happens */
980 mutex_lock(&priv
->shrd
->mutex
);
982 case IWL_TM_CMD_APP2DEV_READ_TRACE
:
983 IWL_DEBUG_INFO(priv
, "uCode trace cmd to driver\n");
984 result
= iwl_testmode_trace_dump(hw
, tb
, skb
, cb
);
986 case IWL_TM_CMD_APP2DEV_DUMP_SRAM
:
987 IWL_DEBUG_INFO(priv
, "testmode sram dump cmd to driver\n");
988 result
= iwl_testmode_sram_dump(hw
, tb
, skb
, cb
);
995 mutex_unlock(&priv
->shrd
->mutex
);