blob: e42e0768880b1a52967cffa694600604c992f3cf [file] [log] [blame]
Cindy H. Kao4613e722011-05-06 10:40:15 -07001/******************************************************************************
2 *
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.
5 *
6 * GPL LICENSE SUMMARY
7 *
Wey-Yi Guy4e318262011-12-27 11:21:32 -08008 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
Cindy H. Kao4613e722011-05-06 10:40:15 -07009 *
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.
13 *
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.
18 *
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,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Wey-Yi Guy4e318262011-12-27 11:21:32 -080033 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
Cindy H. Kao4613e722011-05-06 10:40:15 -070034 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
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
45 * distribution.
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.
49 *
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.
61 *
62 *****************************************************************************/
63#include <linux/init.h>
64#include <linux/kernel.h>
65#include <linux/module.h>
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070066#include <linux/dma-mapping.h>
Cindy H. Kao4613e722011-05-06 10:40:15 -070067#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>
72
Don Fry69a679b2011-12-07 08:50:46 -080073#include "iwl-wifi.h"
Cindy H. Kao4613e722011-05-06 10:40:15 -070074#include "iwl-dev.h"
75#include "iwl-core.h"
76#include "iwl-debug.h"
Cindy H. Kao4613e722011-05-06 10:40:15 -070077#include "iwl-io.h"
78#include "iwl-agn.h"
79#include "iwl-testmode.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070080#include "iwl-trans.h"
Hsu, Kenny0bec12b2011-12-01 01:12:50 -080081#include "iwl-bus.h"
Amit Beka6fe7dd02012-01-25 09:19:24 +020082#include "iwl-fh.h"
Cindy H. Kao4613e722011-05-06 10:40:15 -070083
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.
87 * See iwl-testmode.h
88 */
89static
90struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
91 [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
92
93 [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
94 [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
95
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, },
99
100 [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
101 [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700102
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700103 [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
104
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700105 [IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700106 [IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
Wey-Yi Guy49b72102011-05-30 10:29:37 -0700107 [IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700108
Wey-Yi Guy64898542011-05-03 18:05:13 -0700109 [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
Wey-Yi Guy49b72102011-05-30 10:29:37 -0700110
Wey-Yi Guye98a1932011-07-08 08:46:26 -0700111 [IWL_TM_ATTR_UCODE_OWNER] = { .type = NLA_U8, },
Hsu, Kenny306713f2011-11-22 23:03:39 -0800112
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, },
Hsu, Kennye1a38fe2011-11-28 00:55:38 -0800116
117 [IWL_TM_ATTR_FW_VERSION] = { .type = NLA_U32, },
Hsu, Kenny0bec12b2011-12-01 01:12:50 -0800118 [IWL_TM_ATTR_DEVICE_ID] = { .type = NLA_U32, },
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800119 [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, },
Cindy H. Kao4613e722011-05-06 10:40:15 -0700122};
123
124/*
125 * See the struct iwl_rx_packet in iwl-commands.h for the format of the
126 * received events from the device
127 */
128static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
129{
130 struct iwl_rx_packet *pkt = rxb_addr(rxb);
131 if (pkt)
132 return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
133 else
134 return 0;
135}
136
137
138/*
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
142 * in iwlagn driver.
143 *
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.
147 *
148 * @priv: the instance of iwlwifi device
149 * @rxb: pointer to rx data content received by the ISR
150 *
151 * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
152 * For the messages multicasting to the user application, the mandatory
153 * TLV fields are :
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
156 */
157
158static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
159 struct iwl_rx_mem_buffer *rxb)
160{
161 struct ieee80211_hw *hw = priv->hw;
162 struct sk_buff *skb;
163 void *data;
164 int length;
165
166 data = (void *)rxb_addr(rxb);
167 length = get_event_length(rxb);
168
169 if (!data || length == 0)
170 return;
171
172 skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
173 GFP_ATOMIC);
174 if (skb == NULL) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800175 IWL_ERR(priv,
Cindy H. Kao4613e722011-05-06 10:40:15 -0700176 "Run out of memory for messages to user space ?\n");
177 return;
178 }
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);
182 return;
183
184nla_put_failure:
185 kfree_skb(skb);
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800186 IWL_ERR(priv, "Ouch, overran buffer, check allocation!\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700187}
188
189void iwl_testmode_init(struct iwl_priv *priv)
190{
191 priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700192 priv->testmode_trace.trace_enabled = false;
Hsu, Kenny306713f2011-11-22 23:03:39 -0800193 priv->testmode_sram.sram_readed = false;
194}
195
Wey-Yi Guy6b5a26f52011-11-23 07:08:22 -0800196static void iwl_sram_cleanup(struct iwl_priv *priv)
Hsu, Kenny306713f2011-11-22 23:03:39 -0800197{
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;
204 }
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700205}
206
207static void iwl_trace_cleanup(struct iwl_priv *priv)
208{
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700209 if (priv->testmode_trace.trace_enabled) {
210 if (priv->testmode_trace.cpu_addr &&
211 priv->testmode_trace.dma_addr)
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200212 dma_free_coherent(trans(priv)->dev,
Wey-Yi Guy49b72102011-05-30 10:29:37 -0700213 priv->testmode_trace.total_size,
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700214 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;
Wey-Yi Guy49b72102011-05-30 10:29:37 -0700220 priv->testmode_trace.buff_size = 0;
221 priv->testmode_trace.total_size = 0;
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700222 }
223}
224
225
226void iwl_testmode_cleanup(struct iwl_priv *priv)
227{
228 iwl_trace_cleanup(priv);
Hsu, Kenny306713f2011-11-22 23:03:39 -0800229 iwl_sram_cleanup(priv);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700230}
231
232/*
233 * This function handles the user application commands to the ucode.
234 *
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.
238 *
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
241 * ucode is replied.
242 *
243 * @hw: ieee80211_hw object that represents the device
244 * @tb: gnl message fields from the user space
245 */
246static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
247{
248 struct iwl_priv *priv = hw->priv;
249 struct iwl_host_cmd cmd;
250
251 memset(&cmd, 0, sizeof(struct iwl_host_cmd));
252
253 if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
254 !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800255 IWL_ERR(priv, "Missing ucode command mandatory fields\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700256 return -ENOMSG;
257 }
258
Wey-Yi Guyc7c11152011-07-08 08:46:25 -0700259 cmd.flags = CMD_ON_DEMAND;
Cindy H. Kao4613e722011-05-06 10:40:15 -0700260 cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
Johannes Berg3fa50732011-05-04 07:50:38 -0700261 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]);
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700263 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
Cindy H. Kao4613e722011-05-06 10:40:15 -0700264 IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
Johannes Berg3fa50732011-05-04 07:50:38 -0700265 " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700266 /* ok, let's submit the command to ucode */
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700267 return iwl_trans_send_cmd(trans(priv), &cmd);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700268}
269
270
271/*
272 * This function handles the user application commands for register access.
273 *
274 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
275 * handlers respectively.
276 *
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.
281 *
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.
284 *
285 * @hw: ieee80211_hw object that represents the device
286 * @tb: gnl message fields from the user space
287 */
288static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
289{
290 struct iwl_priv *priv = hw->priv;
Amit Beka6fe7dd02012-01-25 09:19:24 +0200291 u32 ofs, val32, cmd;
Cindy H. Kao4613e722011-05-06 10:40:15 -0700292 u8 val8;
293 struct sk_buff *skb;
294 int status = 0;
295
296 if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800297 IWL_ERR(priv, "Missing register offset\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700298 return -ENOMSG;
299 }
300 ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
301 IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
302
Amit Beka6fe7dd02012-01-25 09:19:24 +0200303 /* 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)) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800311 IWL_ERR(priv, "offset out of segment (0x0 - 0x%x)\n",
Amit Beka6fe7dd02012-01-25 09:19:24 +0200312 FH_MEM_UPPER_BOUND);
313 return -EINVAL;
314 }
315
316 switch (cmd) {
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800317 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200318 val32 = iwl_read_direct32(trans(priv), ofs);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700319 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
320
321 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
322 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800323 IWL_ERR(priv, "Memory allocation fail\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700324 return -ENOMEM;
325 }
326 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
327 status = cfg80211_testmode_reply(skb);
328 if (status < 0)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800329 IWL_ERR(priv, "Error sending msg : %d\n", status);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700330 break;
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800331 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
Cindy H. Kao4613e722011-05-06 10:40:15 -0700332 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800333 IWL_ERR(priv, "Missing value to write\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700334 return -ENOMSG;
335 } else {
336 val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
337 IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200338 iwl_write_direct32(trans(priv), ofs, val32);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700339 }
340 break;
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800341 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
Cindy H. Kao4613e722011-05-06 10:40:15 -0700342 if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800343 IWL_ERR(priv, "Missing value to write\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700344 return -ENOMSG;
345 } else {
346 val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
347 IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200348 iwl_write8(trans(priv), ofs, val8);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700349 }
350 break;
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800351 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32:
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200352 val32 = iwl_read_prph(trans(priv), ofs);
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800353 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
354
355 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
356 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800357 IWL_ERR(priv, "Memory allocation fail\n");
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800358 return -ENOMEM;
359 }
360 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
361 status = cfg80211_testmode_reply(skb);
362 if (status < 0)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800363 IWL_ERR(priv, "Error sending msg : %d\n", status);
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800364 break;
365 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32:
366 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800367 IWL_ERR(priv, "Missing value to write\n");
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800368 return -ENOMSG;
369 } else {
370 val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
371 IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200372 iwl_write_prph(trans(priv), ofs, val32);
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800373 }
374 break;
Cindy H. Kao4613e722011-05-06 10:40:15 -0700375 default:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800376 IWL_ERR(priv, "Unknown testmode register command ID\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700377 return -ENOSYS;
378 }
379
380 return status;
381
382nla_put_failure:
383 kfree_skb(skb);
384 return -EMSGSIZE;
385}
386
387
388static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
389{
390 struct iwl_notification_wait calib_wait;
391 int ret;
392
Don Frydd5fe102011-11-28 16:13:19 -0800393 iwl_init_notification_wait(priv->shrd, &calib_wait,
Cindy H. Kao4613e722011-05-06 10:40:15 -0700394 CALIBRATION_COMPLETE_NOTIFICATION,
395 NULL, NULL);
Don Fry69a679b2011-12-07 08:50:46 -0800396 ret = iwl_init_alive_start(trans(priv));
Cindy H. Kao4613e722011-05-06 10:40:15 -0700397 if (ret) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800398 IWL_ERR(priv, "Fail init calibration: %d\n", ret);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700399 goto cfg_init_calib_error;
400 }
401
Don Frydd5fe102011-11-28 16:13:19 -0800402 ret = iwl_wait_notification(priv->shrd, &calib_wait, 2 * HZ);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700403 if (ret)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800404 IWL_ERR(priv, "Error detecting"
Cindy H. Kao4613e722011-05-06 10:40:15 -0700405 " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
406 return ret;
407
408cfg_init_calib_error:
Don Frydd5fe102011-11-28 16:13:19 -0800409 iwl_remove_notification(priv->shrd, &calib_wait);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700410 return ret;
411}
412
413/*
414 * This function handles the user application commands for driver.
415 *
416 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
417 * handlers respectively.
418 *
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.
421 *
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.
425 *
426 * @hw: ieee80211_hw object that represents the device
427 * @tb: gnl message fields from the user space
428 */
429static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
430{
431 struct iwl_priv *priv = hw->priv;
Don Fry69a679b2011-12-07 08:50:46 -0800432 struct iwl_trans *trans = trans(priv);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700433 struct sk_buff *skb;
434 unsigned char *rsp_data_ptr = NULL;
435 int status = 0, rsp_data_len = 0;
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800436 u32 devid, inst_size = 0, data_size = 0;
Cindy H. Kao4613e722011-05-06 10:40:15 -0700437
438 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
439 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
Don Fry38622412011-12-16 07:07:36 -0800440 rsp_data_ptr = (unsigned char *)cfg(priv)->name;
441 rsp_data_len = strlen(cfg(priv)->name);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700442 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
443 rsp_data_len + 20);
444 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800445 IWL_ERR(priv, "Memory allocation fail\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700446 return -ENOMEM;
447 }
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);
453 if (status < 0)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800454 IWL_ERR(priv, "Error sending msg : %d\n", status);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700455 break;
456
457 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
Don Fry69a679b2011-12-07 08:50:46 -0800458 status = iwl_load_ucode_wait_alive(trans, IWL_UCODE_INIT);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700459 if (status)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800460 IWL_ERR(priv, "Error loading init ucode: %d\n", status);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700461 break;
462
463 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
464 iwl_testmode_cfg_init_calib(priv);
Don Fry69a679b2011-12-07 08:50:46 -0800465 iwl_trans_stop_device(trans);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700466 break;
467
468 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
Don Fry69a679b2011-12-07 08:50:46 -0800469 status = iwl_load_ucode_wait_alive(trans, IWL_UCODE_REGULAR);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700470 if (status) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800471 IWL_ERR(priv,
Cindy H. Kao4613e722011-05-06 10:40:15 -0700472 "Error loading runtime ucode: %d\n", status);
473 break;
474 }
475 status = iwl_alive_start(priv);
476 if (status)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800477 IWL_ERR(priv,
Cindy H. Kao4613e722011-05-06 10:40:15 -0700478 "Error starting the device: %d\n", status);
479 break;
480
Hsu, Kennyd4af19f2011-11-24 22:26:53 -0800481 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
482 iwl_scan_cancel_timeout(priv, 200);
Don Fry69a679b2011-12-07 08:50:46 -0800483 iwl_trans_stop_device(trans);
484 status = iwl_load_ucode_wait_alive(trans, IWL_UCODE_WOWLAN);
Hsu, Kennyd4af19f2011-11-24 22:26:53 -0800485 if (status) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800486 IWL_ERR(priv,
Hsu, Kennyd4af19f2011-11-24 22:26:53 -0800487 "Error loading WOWLAN ucode: %d\n", status);
488 break;
489 }
490 status = iwl_alive_start(priv);
491 if (status)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800492 IWL_ERR(priv,
Hsu, Kennyd4af19f2011-11-24 22:26:53 -0800493 "Error starting the device: %d\n", status);
494 break;
495
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700496 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
Don Fryab36eab2011-11-30 15:37:32 -0800497 if (priv->shrd->eeprom) {
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700498 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
Don Fry38622412011-12-16 07:07:36 -0800499 cfg(priv)->base_params->eeprom_size + 20);
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700500 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800501 IWL_ERR(priv, "Memory allocation fail\n");
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700502 return -ENOMEM;
503 }
504 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
505 IWL_TM_CMD_DEV2APP_EEPROM_RSP);
506 NLA_PUT(skb, IWL_TM_ATTR_EEPROM,
Don Fry38622412011-12-16 07:07:36 -0800507 cfg(priv)->base_params->eeprom_size,
Don Fryab36eab2011-11-30 15:37:32 -0800508 priv->shrd->eeprom);
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700509 status = cfg80211_testmode_reply(skb);
510 if (status < 0)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800511 IWL_ERR(priv, "Error sending msg : %d\n",
512 status);
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700513 } else
514 return -EFAULT;
515 break;
516
Wey-Yi Guy64898542011-05-03 18:05:13 -0700517 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
518 if (!tb[IWL_TM_ATTR_FIXRATE]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800519 IWL_ERR(priv, "Missing fixrate setting\n");
Wey-Yi Guy64898542011-05-03 18:05:13 -0700520 return -ENOMSG;
521 }
Wey-Yi Guy4e308112011-07-08 08:46:28 -0700522 priv->tm_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
Wey-Yi Guy64898542011-05-03 18:05:13 -0700523 break;
524
Hsu, Kennye1a38fe2011-11-28 00:55:38 -0800525 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
526 IWL_INFO(priv, "uCode version raw: 0x%x\n", priv->ucode_ver);
527
528 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
529 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800530 IWL_ERR(priv, "Memory allocation fail\n");
Hsu, Kennye1a38fe2011-11-28 00:55:38 -0800531 return -ENOMEM;
532 }
533 NLA_PUT_U32(skb, IWL_TM_ATTR_FW_VERSION, priv->ucode_ver);
534 status = cfg80211_testmode_reply(skb);
535 if (status < 0)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800536 IWL_ERR(priv, "Error sending msg : %d\n", status);
Hsu, Kennye1a38fe2011-11-28 00:55:38 -0800537 break;
538
Hsu, Kenny0bec12b2011-12-01 01:12:50 -0800539 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
Emmanuel Grumbach99673ee2012-01-08 21:19:45 +0200540 devid = trans(priv)->hw_id;
Wey-Yi Guyfb6c1c62011-12-10 11:33:53 -0800541 IWL_INFO(priv, "hw version: 0x%x\n", devid);
Hsu, Kenny0bec12b2011-12-01 01:12:50 -0800542
543 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
544 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800545 IWL_ERR(priv, "Memory allocation fail\n");
Hsu, Kenny0bec12b2011-12-01 01:12:50 -0800546 return -ENOMEM;
547 }
548 NLA_PUT_U32(skb, IWL_TM_ATTR_DEVICE_ID, devid);
549 status = cfg80211_testmode_reply(skb);
550 if (status < 0)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800551 IWL_ERR(priv, "Error sending msg : %d\n", status);
Hsu, Kenny0bec12b2011-12-01 01:12:50 -0800552 break;
553
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800554 case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
555 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20 + 8);
556 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800557 IWL_ERR(priv, "Memory allocation fail\n");
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800558 return -ENOMEM;
559 }
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;
564 break;
565 case IWL_UCODE_INIT:
566 inst_size = trans(priv)->ucode_init.code.len;
567 data_size = trans(priv)->ucode_init.data.len;
568 break;
569 case IWL_UCODE_WOWLAN:
570 inst_size = trans(priv)->ucode_wowlan.code.len;
571 data_size = trans(priv)->ucode_wowlan.data.len;
572 break;
573 case IWL_UCODE_NONE:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800574 IWL_ERR(priv, "No uCode has not been loaded\n");
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800575 break;
576 default:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800577 IWL_ERR(priv, "Unsupported uCode type\n");
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800578 break;
579 }
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);
584 if (status < 0)
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800585 IWL_ERR(priv, "Error sending msg : %d\n", status);
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800586 break;
587
Cindy H. Kao4613e722011-05-06 10:40:15 -0700588 default:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800589 IWL_ERR(priv, "Unknown testmode driver command ID\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700590 return -ENOSYS;
591 }
592 return status;
593
594nla_put_failure:
595 kfree_skb(skb);
596 return -EMSGSIZE;
597}
598
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700599
600/*
601 * This function handles the user application commands for uCode trace
602 *
603 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
604 * handlers respectively.
605 *
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.
608 *
609 * @hw: ieee80211_hw object that represents the device
610 * @tb: gnl message fields from the user space
611 */
612static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
613{
614 struct iwl_priv *priv = hw->priv;
615 struct sk_buff *skb;
616 int status = 0;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200617 struct device *dev = trans(priv)->dev;
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700618
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)
622 return -EBUSY;
623
Wey-Yi Guy49b72102011-05-30 10:29:37 -0700624 if (!tb[IWL_TM_ATTR_TRACE_SIZE])
625 priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
626 else
627 priv->testmode_trace.buff_size =
628 nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
629 if (!priv->testmode_trace.buff_size)
630 return -EINVAL;
631 if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
632 priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
633 return -EINVAL;
634
635 priv->testmode_trace.total_size =
636 priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700637 priv->testmode_trace.cpu_addr =
638 dma_alloc_coherent(dev,
Wey-Yi Guy49b72102011-05-30 10:29:37 -0700639 priv->testmode_trace.total_size,
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700640 &priv->testmode_trace.dma_addr,
641 GFP_KERNEL);
642 if (!priv->testmode_trace.cpu_addr)
643 return -ENOMEM;
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,
Wey-Yi Guy49b72102011-05-30 10:29:37 -0700648 priv->testmode_trace.buff_size);
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700649 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
650 sizeof(priv->testmode_trace.dma_addr) + 20);
651 if (!skb) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800652 IWL_ERR(priv, "Memory allocation fail\n");
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700653 iwl_trace_cleanup(priv);
654 return -ENOMEM;
655 }
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);
660 if (status < 0) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800661 IWL_ERR(priv, "Error sending msg : %d\n", status);
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700662 }
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700663 priv->testmode_trace.num_chunks =
664 DIV_ROUND_UP(priv->testmode_trace.buff_size,
Hsu, Kennye056a652011-11-22 23:05:02 -0800665 DUMP_CHUNK_SIZE);
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700666 break;
667
668 case IWL_TM_CMD_APP2DEV_END_TRACE:
669 iwl_trace_cleanup(priv);
670 break;
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700671 default:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800672 IWL_ERR(priv, "Unknown testmode mem command ID\n");
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700673 return -ENOSYS;
674 }
675 return status;
676
677nla_put_failure:
678 kfree_skb(skb);
679 if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
680 IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
681 iwl_trace_cleanup(priv);
682 return -EMSGSIZE;
683}
684
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700685static int iwl_testmode_trace_dump(struct ieee80211_hw *hw, struct nlattr **tb,
686 struct sk_buff *skb,
687 struct netlink_callback *cb)
688{
689 struct iwl_priv *priv = hw->priv;
690 int idx, length;
691
692 if (priv->testmode_trace.trace_enabled &&
693 priv->testmode_trace.trace_addr) {
694 idx = cb->args[4];
695 if (idx >= priv->testmode_trace.num_chunks)
696 return -ENOENT;
Hsu, Kennye056a652011-11-22 23:05:02 -0800697 length = DUMP_CHUNK_SIZE;
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700698 if (((idx + 1) == priv->testmode_trace.num_chunks) &&
Hsu, Kennye056a652011-11-22 23:05:02 -0800699 (priv->testmode_trace.buff_size % DUMP_CHUNK_SIZE))
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700700 length = priv->testmode_trace.buff_size %
Hsu, Kennye056a652011-11-22 23:05:02 -0800701 DUMP_CHUNK_SIZE;
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700702
703 NLA_PUT(skb, IWL_TM_ATTR_TRACE_DUMP, length,
704 priv->testmode_trace.trace_addr +
Hsu, Kennye056a652011-11-22 23:05:02 -0800705 (DUMP_CHUNK_SIZE * idx));
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700706 idx++;
707 cb->args[4] = idx;
708 return 0;
709 } else
710 return -EFAULT;
711
712 nla_put_failure:
713 return -ENOBUFS;
714}
715
Wey-Yi Guye98a1932011-07-08 08:46:26 -0700716/*
717 * This function handles the user application switch ucode ownership.
718 *
719 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
720 * decide who the current owner of the uCode
721 *
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
724 * will dropped.
725 *
726 * default driver is the owner of uCode in normal operational mode
727 *
728 * @hw: ieee80211_hw object that represents the device
729 * @tb: gnl message fields from the user space
730 */
731static int iwl_testmode_ownership(struct ieee80211_hw *hw, struct nlattr **tb)
732{
733 struct iwl_priv *priv = hw->priv;
734 u8 owner;
735
736 if (!tb[IWL_TM_ATTR_UCODE_OWNER]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800737 IWL_ERR(priv, "Missing ucode owner\n");
Wey-Yi Guye98a1932011-07-08 08:46:26 -0700738 return -ENOMSG;
739 }
740
741 owner = nla_get_u8(tb[IWL_TM_ATTR_UCODE_OWNER]);
742 if ((owner == IWL_OWNERSHIP_DRIVER) || (owner == IWL_OWNERSHIP_TM))
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700743 priv->shrd->ucode_owner = owner;
Wey-Yi Guye98a1932011-07-08 08:46:26 -0700744 else {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800745 IWL_ERR(priv, "Invalid owner\n");
Wey-Yi Guye98a1932011-07-08 08:46:26 -0700746 return -EINVAL;
747 }
748 return 0;
749}
750
Hsu, Kenny306713f2011-11-22 23:03:39 -0800751/*
752 * This function handles the user application commands for SRAM data dump
753 *
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
756 *
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.
761 *
762 * Otherwise 0 is replied indicating the success of the SRAM reading.
763 *
764 * @hw: ieee80211_hw object that represents the device
765 * @tb: gnl message fields from the user space
766 */
767static int iwl_testmode_sram(struct ieee80211_hw *hw, struct nlattr **tb)
768{
769 struct iwl_priv *priv = hw->priv;
Kenny Hsu25324ca2011-12-27 08:18:31 -0800770 u32 ofs, size, maxsize;
Hsu, Kenny306713f2011-11-22 23:03:39 -0800771
772 if (priv->testmode_sram.sram_readed)
773 return -EBUSY;
774
775 if (!tb[IWL_TM_ATTR_SRAM_ADDR]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800776 IWL_ERR(priv, "Missing SRAM offset address\n");
Hsu, Kenny306713f2011-11-22 23:03:39 -0800777 return -ENOMSG;
778 }
779 ofs = nla_get_u32(tb[IWL_TM_ATTR_SRAM_ADDR]);
780 if (!tb[IWL_TM_ATTR_SRAM_SIZE]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800781 IWL_ERR(priv, "Missing size for SRAM reading\n");
Hsu, Kenny306713f2011-11-22 23:03:39 -0800782 return -ENOMSG;
783 }
784 size = nla_get_u32(tb[IWL_TM_ATTR_SRAM_SIZE]);
Don Fry3d6acef2011-11-28 17:05:01 -0800785 switch (priv->shrd->ucode_type) {
Kenny Hsu76de2f22011-11-23 06:57:22 -0800786 case IWL_UCODE_REGULAR:
787 maxsize = trans(priv)->ucode_rt.data.len;
788 break;
789 case IWL_UCODE_INIT:
790 maxsize = trans(priv)->ucode_init.data.len;
791 break;
792 case IWL_UCODE_WOWLAN:
793 maxsize = trans(priv)->ucode_wowlan.data.len;
794 break;
795 case IWL_UCODE_NONE:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800796 IWL_ERR(priv, "uCode does not been loaded\n");
Kenny Hsu76de2f22011-11-23 06:57:22 -0800797 return -ENOSYS;
798 default:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800799 IWL_ERR(priv, "unsupported uCode type\n");
Kenny Hsu76de2f22011-11-23 06:57:22 -0800800 return -ENOSYS;
801 }
Kenny Hsu25324ca2011-12-27 08:18:31 -0800802 if ((ofs + size) > (maxsize + SRAM_DATA_SEG_OFFSET)) {
Kenny Hsu22b48082011-12-27 08:27:52 -0800803 IWL_ERR(priv, "Invalid offset/size: out of range\n");
Kenny Hsu76de2f22011-11-23 06:57:22 -0800804 return -EINVAL;
805 }
Hsu, Kenny306713f2011-11-22 23:03:39 -0800806 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) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800810 IWL_ERR(priv, "Memory allocation fail\n");
Hsu, Kenny306713f2011-11-22 23:03:39 -0800811 return -ENOMEM;
812 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200813 _iwl_read_targ_mem_words(trans(priv), ofs,
Hsu, Kenny306713f2011-11-22 23:03:39 -0800814 priv->testmode_sram.buff_addr,
815 priv->testmode_sram.buff_size / 4);
816 priv->testmode_sram.num_chunks =
Hsu, Kennye056a652011-11-22 23:05:02 -0800817 DIV_ROUND_UP(priv->testmode_sram.buff_size, DUMP_CHUNK_SIZE);
Hsu, Kenny306713f2011-11-22 23:03:39 -0800818 priv->testmode_sram.sram_readed = true;
819 return 0;
820}
821
822static int iwl_testmode_sram_dump(struct ieee80211_hw *hw, struct nlattr **tb,
823 struct sk_buff *skb,
824 struct netlink_callback *cb)
825{
826 struct iwl_priv *priv = hw->priv;
827 int idx, length;
828
829 if (priv->testmode_sram.sram_readed) {
830 idx = cb->args[4];
831 if (idx >= priv->testmode_sram.num_chunks) {
832 iwl_sram_cleanup(priv);
833 return -ENOENT;
834 }
Hsu, Kennye056a652011-11-22 23:05:02 -0800835 length = DUMP_CHUNK_SIZE;
Hsu, Kenny306713f2011-11-22 23:03:39 -0800836 if (((idx + 1) == priv->testmode_sram.num_chunks) &&
Hsu, Kennye056a652011-11-22 23:05:02 -0800837 (priv->testmode_sram.buff_size % DUMP_CHUNK_SIZE))
Hsu, Kenny306713f2011-11-22 23:03:39 -0800838 length = priv->testmode_sram.buff_size %
Hsu, Kennye056a652011-11-22 23:05:02 -0800839 DUMP_CHUNK_SIZE;
Hsu, Kenny306713f2011-11-22 23:03:39 -0800840
841 NLA_PUT(skb, IWL_TM_ATTR_SRAM_DUMP, length,
842 priv->testmode_sram.buff_addr +
Hsu, Kennye056a652011-11-22 23:05:02 -0800843 (DUMP_CHUNK_SIZE * idx));
Hsu, Kenny306713f2011-11-22 23:03:39 -0800844 idx++;
845 cb->args[4] = idx;
846 return 0;
847 } else
848 return -EFAULT;
849
850 nla_put_failure:
851 return -ENOBUFS;
852}
853
Wey-Yi Guye98a1932011-07-08 08:46:26 -0700854
Cindy H. Kao4613e722011-05-06 10:40:15 -0700855/* 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.
858 *
859 * This function is invoked when there is user space application sending
860 * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
861 * by nl80211.
862 *
863 * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
864 * dispatching it to the corresponding handler.
865 *
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.
869 *
870 * @hw: ieee80211_hw object that represents the device
871 * @data: pointer to user space message
872 * @len: length in byte of @data
873 */
Wey-Yi Guyade4c642011-10-10 07:27:11 -0700874int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
Cindy H. Kao4613e722011-05-06 10:40:15 -0700875{
Wey-Yi Guya6779272011-07-11 10:47:39 -0700876 struct nlattr *tb[IWL_TM_ATTR_MAX];
Cindy H. Kao4613e722011-05-06 10:40:15 -0700877 struct iwl_priv *priv = hw->priv;
878 int result;
879
880 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
881 iwl_testmode_gnl_msg_policy);
882 if (result != 0) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800883 IWL_ERR(priv, "Error parsing the gnl message : %d\n", result);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700884 return result;
885 }
886
887 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
888 if (!tb[IWL_TM_ATTR_COMMAND]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800889 IWL_ERR(priv, "Missing testmode command type\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700890 return -ENOMSG;
891 }
892 /* in case multiple accesses to the device happens */
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700893 mutex_lock(&priv->shrd->mutex);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700894
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);
899 break;
Hsu, Kenny72bcacc2011-11-15 19:24:32 -0800900 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:
Cindy H. Kao4613e722011-05-06 10:40:15 -0700905 IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
906 result = iwl_testmode_reg(hw, tb);
907 break;
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:
Wey-Yi Guy4babc352011-05-03 18:05:12 -0700912 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
Wey-Yi Guy64898542011-05-03 18:05:13 -0700913 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
Hsu, Kennyd4af19f2011-11-24 22:26:53 -0800914 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
Hsu, Kennye1a38fe2011-11-28 00:55:38 -0800915 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
Hsu, Kenny0bec12b2011-12-01 01:12:50 -0800916 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
Kenny Hsuaca9b5f2011-12-24 12:12:12 +0800917 case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
Cindy H. Kao4613e722011-05-06 10:40:15 -0700918 IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
919 result = iwl_testmode_driver(hw, tb);
920 break;
Wey-Yi Guy7a4e5282011-05-06 10:21:28 -0700921
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);
927 break;
928
Wey-Yi Guye98a1932011-07-08 08:46:26 -0700929 case IWL_TM_CMD_APP2DEV_OWNERSHIP:
930 IWL_DEBUG_INFO(priv, "testmode change uCode ownership\n");
931 result = iwl_testmode_ownership(hw, tb);
932 break;
933
Hsu, Kenny306713f2011-11-22 23:03:39 -0800934 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);
937 break;
938
Cindy H. Kao4613e722011-05-06 10:40:15 -0700939 default:
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800940 IWL_ERR(priv, "Unknown testmode command\n");
Cindy H. Kao4613e722011-05-06 10:40:15 -0700941 result = -ENOSYS;
942 break;
943 }
944
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700945 mutex_unlock(&priv->shrd->mutex);
Cindy H. Kao4613e722011-05-06 10:40:15 -0700946 return result;
947}
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700948
Wey-Yi Guyade4c642011-10-10 07:27:11 -0700949int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700950 struct netlink_callback *cb,
951 void *data, int len)
952{
953 struct nlattr *tb[IWL_TM_ATTR_MAX];
954 struct iwl_priv *priv = hw->priv;
955 int result;
956 u32 cmd;
957
958 if (cb->args[3]) {
959 /* offset by 1 since commands start at 0 */
960 cmd = cb->args[3] - 1;
961 } else {
962 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
963 iwl_testmode_gnl_msg_policy);
964 if (result) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800965 IWL_ERR(priv,
966 "Error parsing the gnl message : %d\n", result);
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700967 return result;
968 }
969
970 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
971 if (!tb[IWL_TM_ATTR_COMMAND]) {
Wey-Yi Guy2f73d7c2012-02-03 13:06:57 -0800972 IWL_ERR(priv, "Missing testmode command type\n");
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700973 return -ENOMSG;
974 }
975 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
976 cb->args[3] = cmd + 1;
977 }
978
979 /* in case multiple accesses to the device happens */
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700980 mutex_lock(&priv->shrd->mutex);
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700981 switch (cmd) {
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);
985 break;
Hsu, Kenny306713f2011-11-22 23:03:39 -0800986 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);
989 break;
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700990 default:
991 result = -EINVAL;
992 break;
993 }
994
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700995 mutex_unlock(&priv->shrd->mutex);
Wey-Yi Guyeb64dca2011-05-31 08:03:02 -0700996 return result;
997}