iwlwifi: do not re-configure HT40 after associated
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-sv-open.c
CommitLineData
4613e72d
CK
1/******************************************************************************
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 *
8 * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
9 *
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 *
33 * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
34 * 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>
522376d2 66#include <linux/dma-mapping.h>
4613e72d
CK
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>
72
4613e72d
CK
73#include "iwl-dev.h"
74#include "iwl-core.h"
75#include "iwl-debug.h"
4613e72d
CK
76#include "iwl-io.h"
77#include "iwl-agn.h"
78#include "iwl-testmode.h"
bdfbf092 79#include "iwl-trans.h"
4613e72d
CK
80
81/* The TLVs used in the gnl message policy between the kernel module and
82 * user space application. iwl_testmode_gnl_msg_policy is to be carried
83 * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
84 * See iwl-testmode.h
85 */
86static
87struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
88 [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
89
90 [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
91 [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
92
93 [IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
94 [IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
95 [IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },
96
97 [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
98 [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
7a4e5281 99
4babc358
WYG
100 [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
101
7a4e5281 102 [IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
eb64dca0 103 [IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
49b72100 104 [IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
7a4e5281 105
6489854b 106 [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
49b72100 107
e98a1939 108 [IWL_TM_ATTR_UCODE_OWNER] = { .type = NLA_U8, },
4613e72d
CK
109};
110
111/*
112 * See the struct iwl_rx_packet in iwl-commands.h for the format of the
113 * received events from the device
114 */
115static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
116{
117 struct iwl_rx_packet *pkt = rxb_addr(rxb);
118 if (pkt)
119 return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
120 else
121 return 0;
122}
123
124
125/*
126 * This function multicasts the spontaneous messages from the device to the
127 * user space. It is invoked whenever there is a received messages
128 * from the device. This function is called within the ISR of the rx handlers
129 * in iwlagn driver.
130 *
131 * The parsing of the message content is left to the user space application,
132 * The message content is treated as unattacked raw data and is encapsulated
133 * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
134 *
135 * @priv: the instance of iwlwifi device
136 * @rxb: pointer to rx data content received by the ISR
137 *
138 * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
139 * For the messages multicasting to the user application, the mandatory
140 * TLV fields are :
141 * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
142 * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
143 */
144
145static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
146 struct iwl_rx_mem_buffer *rxb)
147{
148 struct ieee80211_hw *hw = priv->hw;
149 struct sk_buff *skb;
150 void *data;
151 int length;
152
153 data = (void *)rxb_addr(rxb);
154 length = get_event_length(rxb);
155
156 if (!data || length == 0)
157 return;
158
159 skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
160 GFP_ATOMIC);
161 if (skb == NULL) {
162 IWL_DEBUG_INFO(priv,
163 "Run out of memory for messages to user space ?\n");
164 return;
165 }
166 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT);
167 NLA_PUT(skb, IWL_TM_ATTR_UCODE_RX_PKT, length, data);
168 cfg80211_testmode_event(skb, GFP_ATOMIC);
169 return;
170
171nla_put_failure:
172 kfree_skb(skb);
173 IWL_DEBUG_INFO(priv, "Ouch, overran buffer, check allocation!\n");
174}
175
176void iwl_testmode_init(struct iwl_priv *priv)
177{
178 priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
7a4e5281
WYG
179 priv->testmode_trace.trace_enabled = false;
180}
181
182static void iwl_trace_cleanup(struct iwl_priv *priv)
183{
7a4e5281
WYG
184 if (priv->testmode_trace.trace_enabled) {
185 if (priv->testmode_trace.cpu_addr &&
186 priv->testmode_trace.dma_addr)
26bfc0cf 187 dma_free_coherent(bus(priv)->dev,
49b72100 188 priv->testmode_trace.total_size,
7a4e5281
WYG
189 priv->testmode_trace.cpu_addr,
190 priv->testmode_trace.dma_addr);
191 priv->testmode_trace.trace_enabled = false;
192 priv->testmode_trace.cpu_addr = NULL;
193 priv->testmode_trace.trace_addr = NULL;
194 priv->testmode_trace.dma_addr = 0;
49b72100
WYG
195 priv->testmode_trace.buff_size = 0;
196 priv->testmode_trace.total_size = 0;
7a4e5281
WYG
197 }
198}
199
200
201void iwl_testmode_cleanup(struct iwl_priv *priv)
202{
203 iwl_trace_cleanup(priv);
4613e72d
CK
204}
205
206/*
207 * This function handles the user application commands to the ucode.
208 *
209 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
210 * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
211 * host command to the ucode.
212 *
213 * If any mandatory field is missing, -ENOMSG is replied to the user space
214 * application; otherwise, the actual execution result of the host command to
215 * ucode is replied.
216 *
217 * @hw: ieee80211_hw object that represents the device
218 * @tb: gnl message fields from the user space
219 */
220static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
221{
222 struct iwl_priv *priv = hw->priv;
223 struct iwl_host_cmd cmd;
224
225 memset(&cmd, 0, sizeof(struct iwl_host_cmd));
226
227 if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
228 !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
229 IWL_DEBUG_INFO(priv,
230 "Error finding ucode command mandatory fields\n");
231 return -ENOMSG;
232 }
233
c7c1115b 234 cmd.flags = CMD_ON_DEMAND;
4613e72d 235 cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
3fa50738
JB
236 cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
237 cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
4ce7cc2b 238 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
4613e72d 239 IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
3fa50738 240 " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
4613e72d 241 /* ok, let's submit the command to ucode */
e6bb4c9c 242 return iwl_trans_send_cmd(trans(priv), &cmd);
4613e72d
CK
243}
244
245
246/*
247 * This function handles the user application commands for register access.
248 *
249 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
250 * handlers respectively.
251 *
252 * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
253 * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
254 * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
255 * the success of the command execution.
256 *
257 * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
258 * value is returned with IWL_TM_ATTR_REG_VALUE32.
259 *
260 * @hw: ieee80211_hw object that represents the device
261 * @tb: gnl message fields from the user space
262 */
263static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
264{
265 struct iwl_priv *priv = hw->priv;
266 u32 ofs, val32;
267 u8 val8;
268 struct sk_buff *skb;
269 int status = 0;
270
271 if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
272 IWL_DEBUG_INFO(priv, "Error finding register offset\n");
273 return -ENOMSG;
274 }
275 ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
276 IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
277
278 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
72bcacc2 279 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
83ed9015 280 val32 = iwl_read32(bus(priv), ofs);
4613e72d
CK
281 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
282
283 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
284 if (!skb) {
285 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
286 return -ENOMEM;
287 }
288 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
289 status = cfg80211_testmode_reply(skb);
290 if (status < 0)
291 IWL_DEBUG_INFO(priv,
292 "Error sending msg : %d\n", status);
293 break;
72bcacc2 294 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
4613e72d
CK
295 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
296 IWL_DEBUG_INFO(priv,
297 "Error finding value to write\n");
298 return -ENOMSG;
299 } else {
300 val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
301 IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
83ed9015 302 iwl_write32(bus(priv), ofs, val32);
4613e72d
CK
303 }
304 break;
72bcacc2 305 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
4613e72d
CK
306 if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
307 IWL_DEBUG_INFO(priv, "Error finding value to write\n");
308 return -ENOMSG;
309 } else {
310 val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
311 IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
83ed9015 312 iwl_write8(bus(priv), ofs, val8);
4613e72d
CK
313 }
314 break;
72bcacc2
HK
315 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32:
316 val32 = iwl_read_prph(bus(priv), ofs);
317 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
318
319 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
320 if (!skb) {
321 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
322 return -ENOMEM;
323 }
324 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
325 status = cfg80211_testmode_reply(skb);
326 if (status < 0)
327 IWL_DEBUG_INFO(priv,
328 "Error sending msg : %d\n", status);
329 break;
330 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32:
331 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
332 IWL_DEBUG_INFO(priv,
333 "Error finding value to write\n");
334 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);
338 iwl_write_prph(bus(priv), ofs, val32);
339 }
340 break;
4613e72d
CK
341 default:
342 IWL_DEBUG_INFO(priv, "Unknown testmode register command ID\n");
343 return -ENOSYS;
344 }
345
346 return status;
347
348nla_put_failure:
349 kfree_skb(skb);
350 return -EMSGSIZE;
351}
352
353
354static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
355{
356 struct iwl_notification_wait calib_wait;
357 int ret;
358
359 iwlagn_init_notification_wait(priv, &calib_wait,
360 CALIBRATION_COMPLETE_NOTIFICATION,
361 NULL, NULL);
362 ret = iwlagn_init_alive_start(priv);
363 if (ret) {
364 IWL_DEBUG_INFO(priv,
365 "Error configuring init calibration: %d\n", ret);
366 goto cfg_init_calib_error;
367 }
368
369 ret = iwlagn_wait_notification(priv, &calib_wait, 2 * HZ);
370 if (ret)
371 IWL_DEBUG_INFO(priv, "Error detecting"
372 " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
373 return ret;
374
375cfg_init_calib_error:
376 iwlagn_remove_notification(priv, &calib_wait);
377 return ret;
378}
379
380/*
381 * This function handles the user application commands for driver.
382 *
383 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
384 * handlers respectively.
385 *
386 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
387 * value of the actual command execution is replied to the user application.
388 *
389 * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
390 * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
391 * IWL_TM_CMD_DEV2APP_SYNC_RSP.
392 *
393 * @hw: ieee80211_hw object that represents the device
394 * @tb: gnl message fields from the user space
395 */
396static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
397{
398 struct iwl_priv *priv = hw->priv;
399 struct sk_buff *skb;
400 unsigned char *rsp_data_ptr = NULL;
401 int status = 0, rsp_data_len = 0;
402
403 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
404 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
405 rsp_data_ptr = (unsigned char *)priv->cfg->name;
406 rsp_data_len = strlen(priv->cfg->name);
407 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
408 rsp_data_len + 20);
409 if (!skb) {
410 IWL_DEBUG_INFO(priv,
411 "Error allocating memory\n");
412 return -ENOMEM;
413 }
414 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
415 IWL_TM_CMD_DEV2APP_SYNC_RSP);
416 NLA_PUT(skb, IWL_TM_ATTR_SYNC_RSP,
417 rsp_data_len, rsp_data_ptr);
418 status = cfg80211_testmode_reply(skb);
419 if (status < 0)
420 IWL_DEBUG_INFO(priv, "Error sending msg : %d\n",
421 status);
422 break;
423
424 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
8929c24b 425 status = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
4613e72d
CK
426 if (status)
427 IWL_DEBUG_INFO(priv,
428 "Error loading init ucode: %d\n", status);
429 break;
430
431 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
432 iwl_testmode_cfg_init_calib(priv);
e6bb4c9c 433 iwl_trans_stop_device(trans(priv));
4613e72d
CK
434 break;
435
436 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
8929c24b 437 status = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
4613e72d
CK
438 if (status) {
439 IWL_DEBUG_INFO(priv,
440 "Error loading runtime ucode: %d\n", status);
441 break;
442 }
443 status = iwl_alive_start(priv);
444 if (status)
445 IWL_DEBUG_INFO(priv,
446 "Error starting the device: %d\n", status);
447 break;
448
4babc358
WYG
449 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
450 if (priv->eeprom) {
451 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
452 priv->cfg->base_params->eeprom_size + 20);
453 if (!skb) {
454 IWL_DEBUG_INFO(priv,
455 "Error allocating memory\n");
456 return -ENOMEM;
457 }
458 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
459 IWL_TM_CMD_DEV2APP_EEPROM_RSP);
460 NLA_PUT(skb, IWL_TM_ATTR_EEPROM,
461 priv->cfg->base_params->eeprom_size,
462 priv->eeprom);
463 status = cfg80211_testmode_reply(skb);
464 if (status < 0)
465 IWL_DEBUG_INFO(priv,
466 "Error sending msg : %d\n",
467 status);
468 } else
469 return -EFAULT;
470 break;
471
6489854b
WYG
472 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
473 if (!tb[IWL_TM_ATTR_FIXRATE]) {
474 IWL_DEBUG_INFO(priv,
475 "Error finding fixrate setting\n");
476 return -ENOMSG;
477 }
4e308119 478 priv->tm_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
6489854b
WYG
479 break;
480
4613e72d
CK
481 default:
482 IWL_DEBUG_INFO(priv, "Unknown testmode driver command ID\n");
483 return -ENOSYS;
484 }
485 return status;
486
487nla_put_failure:
488 kfree_skb(skb);
489 return -EMSGSIZE;
490}
491
7a4e5281
WYG
492
493/*
494 * This function handles the user application commands for uCode trace
495 *
496 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
497 * handlers respectively.
498 *
499 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
500 * value of the actual command execution is replied to the user application.
501 *
502 * @hw: ieee80211_hw object that represents the device
503 * @tb: gnl message fields from the user space
504 */
505static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
506{
507 struct iwl_priv *priv = hw->priv;
508 struct sk_buff *skb;
509 int status = 0;
26bfc0cf 510 struct device *dev = bus(priv)->dev;
7a4e5281
WYG
511
512 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
513 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
514 if (priv->testmode_trace.trace_enabled)
515 return -EBUSY;
516
49b72100
WYG
517 if (!tb[IWL_TM_ATTR_TRACE_SIZE])
518 priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
519 else
520 priv->testmode_trace.buff_size =
521 nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
522 if (!priv->testmode_trace.buff_size)
523 return -EINVAL;
524 if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
525 priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
526 return -EINVAL;
527
528 priv->testmode_trace.total_size =
529 priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
7a4e5281
WYG
530 priv->testmode_trace.cpu_addr =
531 dma_alloc_coherent(dev,
49b72100 532 priv->testmode_trace.total_size,
7a4e5281
WYG
533 &priv->testmode_trace.dma_addr,
534 GFP_KERNEL);
535 if (!priv->testmode_trace.cpu_addr)
536 return -ENOMEM;
537 priv->testmode_trace.trace_enabled = true;
538 priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
539 priv->testmode_trace.cpu_addr, 0x100);
540 memset(priv->testmode_trace.trace_addr, 0x03B,
49b72100 541 priv->testmode_trace.buff_size);
7a4e5281
WYG
542 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
543 sizeof(priv->testmode_trace.dma_addr) + 20);
544 if (!skb) {
545 IWL_DEBUG_INFO(priv,
546 "Error allocating memory\n");
547 iwl_trace_cleanup(priv);
548 return -ENOMEM;
549 }
550 NLA_PUT(skb, IWL_TM_ATTR_TRACE_ADDR,
551 sizeof(priv->testmode_trace.dma_addr),
552 (u64 *)&priv->testmode_trace.dma_addr);
553 status = cfg80211_testmode_reply(skb);
554 if (status < 0) {
555 IWL_DEBUG_INFO(priv,
556 "Error sending msg : %d\n",
557 status);
558 }
eb64dca0
WYG
559 priv->testmode_trace.num_chunks =
560 DIV_ROUND_UP(priv->testmode_trace.buff_size,
561 TRACE_CHUNK_SIZE);
7a4e5281
WYG
562 break;
563
564 case IWL_TM_CMD_APP2DEV_END_TRACE:
565 iwl_trace_cleanup(priv);
566 break;
7a4e5281
WYG
567 default:
568 IWL_DEBUG_INFO(priv, "Unknown testmode mem command ID\n");
569 return -ENOSYS;
570 }
571 return status;
572
573nla_put_failure:
574 kfree_skb(skb);
575 if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
576 IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
577 iwl_trace_cleanup(priv);
578 return -EMSGSIZE;
579}
580
eb64dca0
WYG
581static int iwl_testmode_trace_dump(struct ieee80211_hw *hw, struct nlattr **tb,
582 struct sk_buff *skb,
583 struct netlink_callback *cb)
584{
585 struct iwl_priv *priv = hw->priv;
586 int idx, length;
587
588 if (priv->testmode_trace.trace_enabled &&
589 priv->testmode_trace.trace_addr) {
590 idx = cb->args[4];
591 if (idx >= priv->testmode_trace.num_chunks)
592 return -ENOENT;
593 length = TRACE_CHUNK_SIZE;
594 if (((idx + 1) == priv->testmode_trace.num_chunks) &&
595 (priv->testmode_trace.buff_size % TRACE_CHUNK_SIZE))
596 length = priv->testmode_trace.buff_size %
597 TRACE_CHUNK_SIZE;
598
599 NLA_PUT(skb, IWL_TM_ATTR_TRACE_DUMP, length,
600 priv->testmode_trace.trace_addr +
601 (TRACE_CHUNK_SIZE * idx));
602 idx++;
603 cb->args[4] = idx;
604 return 0;
605 } else
606 return -EFAULT;
607
608 nla_put_failure:
609 return -ENOBUFS;
610}
611
e98a1939
WYG
612/*
613 * This function handles the user application switch ucode ownership.
614 *
615 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
616 * decide who the current owner of the uCode
617 *
618 * If the current owner is OWNERSHIP_TM, then the only host command
619 * can deliver to uCode is from testmode, all the other host commands
620 * will dropped.
621 *
622 * default driver is the owner of uCode in normal operational mode
623 *
624 * @hw: ieee80211_hw object that represents the device
625 * @tb: gnl message fields from the user space
626 */
627static int iwl_testmode_ownership(struct ieee80211_hw *hw, struct nlattr **tb)
628{
629 struct iwl_priv *priv = hw->priv;
630 u8 owner;
631
632 if (!tb[IWL_TM_ATTR_UCODE_OWNER]) {
633 IWL_DEBUG_INFO(priv, "Error finding ucode owner\n");
634 return -ENOMSG;
635 }
636
637 owner = nla_get_u8(tb[IWL_TM_ATTR_UCODE_OWNER]);
638 if ((owner == IWL_OWNERSHIP_DRIVER) || (owner == IWL_OWNERSHIP_TM))
fd656935 639 priv->shrd->ucode_owner = owner;
e98a1939
WYG
640 else {
641 IWL_DEBUG_INFO(priv, "Invalid owner\n");
642 return -EINVAL;
643 }
644 return 0;
645}
646
647
4613e72d
CK
648/* The testmode gnl message handler that takes the gnl message from the
649 * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
650 * invoke the corresponding handlers.
651 *
652 * This function is invoked when there is user space application sending
653 * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
654 * by nl80211.
655 *
656 * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
657 * dispatching it to the corresponding handler.
658 *
659 * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
660 * -ENOSYS is replied to the user application if the command is unknown;
661 * Otherwise, the command is dispatched to the respective handler.
662 *
663 * @hw: ieee80211_hw object that represents the device
664 * @data: pointer to user space message
665 * @len: length in byte of @data
666 */
ade4c649 667int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
4613e72d 668{
a6779278 669 struct nlattr *tb[IWL_TM_ATTR_MAX];
4613e72d
CK
670 struct iwl_priv *priv = hw->priv;
671 int result;
672
673 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
674 iwl_testmode_gnl_msg_policy);
675 if (result != 0) {
676 IWL_DEBUG_INFO(priv,
677 "Error parsing the gnl message : %d\n", result);
678 return result;
679 }
680
681 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
682 if (!tb[IWL_TM_ATTR_COMMAND]) {
683 IWL_DEBUG_INFO(priv, "Error finding testmode command type\n");
684 return -ENOMSG;
685 }
686 /* in case multiple accesses to the device happens */
6ac2f839 687 mutex_lock(&priv->shrd->mutex);
4613e72d
CK
688
689 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
690 case IWL_TM_CMD_APP2DEV_UCODE:
691 IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
692 result = iwl_testmode_ucode(hw, tb);
693 break;
72bcacc2
HK
694 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
695 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
696 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
697 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32:
698 case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32:
4613e72d
CK
699 IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
700 result = iwl_testmode_reg(hw, tb);
701 break;
702 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
703 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
704 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
705 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
4babc358 706 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
6489854b 707 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
4613e72d
CK
708 IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
709 result = iwl_testmode_driver(hw, tb);
710 break;
7a4e5281
WYG
711
712 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
713 case IWL_TM_CMD_APP2DEV_END_TRACE:
714 case IWL_TM_CMD_APP2DEV_READ_TRACE:
715 IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
716 result = iwl_testmode_trace(hw, tb);
717 break;
718
e98a1939
WYG
719 case IWL_TM_CMD_APP2DEV_OWNERSHIP:
720 IWL_DEBUG_INFO(priv, "testmode change uCode ownership\n");
721 result = iwl_testmode_ownership(hw, tb);
722 break;
723
4613e72d
CK
724 default:
725 IWL_DEBUG_INFO(priv, "Unknown testmode command\n");
726 result = -ENOSYS;
727 break;
728 }
729
6ac2f839 730 mutex_unlock(&priv->shrd->mutex);
4613e72d
CK
731 return result;
732}
eb64dca0 733
ade4c649 734int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
eb64dca0
WYG
735 struct netlink_callback *cb,
736 void *data, int len)
737{
738 struct nlattr *tb[IWL_TM_ATTR_MAX];
739 struct iwl_priv *priv = hw->priv;
740 int result;
741 u32 cmd;
742
743 if (cb->args[3]) {
744 /* offset by 1 since commands start at 0 */
745 cmd = cb->args[3] - 1;
746 } else {
747 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
748 iwl_testmode_gnl_msg_policy);
749 if (result) {
750 IWL_DEBUG_INFO(priv,
751 "Error parsing the gnl message : %d\n", result);
752 return result;
753 }
754
755 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
756 if (!tb[IWL_TM_ATTR_COMMAND]) {
757 IWL_DEBUG_INFO(priv,
758 "Error finding testmode command type\n");
759 return -ENOMSG;
760 }
761 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
762 cb->args[3] = cmd + 1;
763 }
764
765 /* in case multiple accesses to the device happens */
6ac2f839 766 mutex_lock(&priv->shrd->mutex);
eb64dca0
WYG
767 switch (cmd) {
768 case IWL_TM_CMD_APP2DEV_READ_TRACE:
769 IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
770 result = iwl_testmode_trace_dump(hw, tb, skb, cb);
771 break;
772 default:
773 result = -EINVAL;
774 break;
775 }
776
6ac2f839 777 mutex_unlock(&priv->shrd->mutex);
eb64dca0
WYG
778 return result;
779}
This page took 0.341885 seconds and 5 git commands to generate.