2 * Copyright (c) 2014-2017 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <net/netlink.h>
20 #include <linux/firmware.h>
28 #include "testmode_i.h"
30 static const struct nla_policy ath10k_tm_policy
[ATH10K_TM_ATTR_MAX
+ 1] = {
31 [ATH10K_TM_ATTR_CMD
] = { .type
= NLA_U32
},
32 [ATH10K_TM_ATTR_DATA
] = { .type
= NLA_BINARY
,
33 .len
= ATH10K_TM_DATA_MAX_LEN
},
34 [ATH10K_TM_ATTR_WMI_CMDID
] = { .type
= NLA_U32
},
35 [ATH10K_TM_ATTR_VERSION_MAJOR
] = { .type
= NLA_U32
},
36 [ATH10K_TM_ATTR_VERSION_MINOR
] = { .type
= NLA_U32
},
39 /* Returns true if callee consumes the skb and the skb should be discarded.
40 * Returns false if skb is not used. Does not sleep.
42 bool ath10k_tm_event_wmi(struct ath10k
*ar
, u32 cmd_id
, struct sk_buff
*skb
)
44 struct sk_buff
*nl_skb
;
48 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
,
49 "testmode event wmi cmd_id %d skb %pK skb->len %d\n",
50 cmd_id
, skb
, skb
->len
);
52 ath10k_dbg_dump(ar
, ATH10K_DBG_TESTMODE
, NULL
, "", skb
->data
, skb
->len
);
54 spin_lock_bh(&ar
->data_lock
);
56 if (!ar
->testmode
.utf_monitor
) {
61 /* Only testmode.c should be handling events from utf firmware,
62 * otherwise all sort of problems will arise as mac80211 operations
63 * are not initialised.
67 nl_skb
= cfg80211_testmode_alloc_event_skb(ar
->hw
->wiphy
,
68 2 * sizeof(u32
) + skb
->len
,
72 "failed to allocate skb for testmode wmi event\n");
76 ret
= nla_put_u32(nl_skb
, ATH10K_TM_ATTR_CMD
, ATH10K_TM_CMD_WMI
);
79 "failed to to put testmode wmi event cmd attribute: %d\n",
85 ret
= nla_put_u32(nl_skb
, ATH10K_TM_ATTR_WMI_CMDID
, cmd_id
);
88 "failed to to put testmode wmi even cmd_id: %d\n",
94 ret
= nla_put(nl_skb
, ATH10K_TM_ATTR_DATA
, skb
->len
, skb
->data
);
97 "failed to copy skb to testmode wmi event: %d\n",
103 cfg80211_testmode_event(nl_skb
, GFP_ATOMIC
);
106 spin_unlock_bh(&ar
->data_lock
);
111 static int ath10k_tm_cmd_get_version(struct ath10k
*ar
, struct nlattr
*tb
[])
116 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
,
117 "testmode cmd get version_major %d version_minor %d\n",
118 ATH10K_TESTMODE_VERSION_MAJOR
,
119 ATH10K_TESTMODE_VERSION_MINOR
);
121 skb
= cfg80211_testmode_alloc_reply_skb(ar
->hw
->wiphy
,
122 nla_total_size(sizeof(u32
)));
126 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_VERSION_MAJOR
,
127 ATH10K_TESTMODE_VERSION_MAJOR
);
133 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_VERSION_MINOR
,
134 ATH10K_TESTMODE_VERSION_MINOR
);
140 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_WMI_OP_VERSION
,
141 ar
->normal_mode_fw
.fw_file
.wmi_op_version
);
147 return cfg80211_testmode_reply(skb
);
150 static int ath10k_tm_fetch_utf_firmware_api_1(struct ath10k
*ar
,
151 struct ath10k_fw_file
*fw_file
)
156 snprintf(filename
, sizeof(filename
), "%s/%s",
157 ar
->hw_params
.fw
.dir
, ATH10K_FW_UTF_FILE
);
159 /* load utf firmware image */
160 ret
= firmware_request_nowarn(&fw_file
->firmware
, filename
, ar
->dev
);
161 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode fw request '%s': %d\n",
165 ath10k_warn(ar
, "failed to retrieve utf firmware '%s': %d\n",
170 /* We didn't find FW UTF API 1 ("utf.bin") does not advertise
171 * firmware features. Do an ugly hack where we force the firmware
172 * features to match with 10.1 branch so that wmi.c will use the
173 * correct WMI interface.
176 fw_file
->wmi_op_version
= ATH10K_FW_WMI_OP_VERSION_10_1
;
177 fw_file
->htt_op_version
= ATH10K_FW_HTT_OP_VERSION_10_1
;
178 fw_file
->firmware_data
= fw_file
->firmware
->data
;
179 fw_file
->firmware_len
= fw_file
->firmware
->size
;
184 static int ath10k_tm_fetch_firmware(struct ath10k
*ar
)
186 struct ath10k_fw_components
*utf_mode_fw
;
189 ret
= ath10k_core_fetch_firmware_api_n(ar
, ATH10K_FW_UTF_API2_FILE
,
190 &ar
->testmode
.utf_mode_fw
.fw_file
);
192 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode using fw utf api 2");
196 ret
= ath10k_tm_fetch_utf_firmware_api_1(ar
, &ar
->testmode
.utf_mode_fw
.fw_file
);
198 ath10k_err(ar
, "failed to fetch utf firmware binary: %d", ret
);
202 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode using utf api 1");
205 utf_mode_fw
= &ar
->testmode
.utf_mode_fw
;
207 /* Use the same board data file as the normal firmware uses (but
208 * it's still "owned" by normal_mode_fw so we shouldn't free it.
210 utf_mode_fw
->board_data
= ar
->normal_mode_fw
.board_data
;
211 utf_mode_fw
->board_len
= ar
->normal_mode_fw
.board_len
;
213 if (!utf_mode_fw
->fw_file
.otp_data
) {
214 ath10k_info(ar
, "utf.bin didn't contain otp binary, taking it from the normal mode firmware");
215 utf_mode_fw
->fw_file
.otp_data
= ar
->normal_mode_fw
.fw_file
.otp_data
;
216 utf_mode_fw
->fw_file
.otp_len
= ar
->normal_mode_fw
.fw_file
.otp_len
;
222 static int ath10k_tm_cmd_utf_start(struct ath10k
*ar
, struct nlattr
*tb
[])
227 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode cmd utf start\n");
229 mutex_lock(&ar
->conf_mutex
);
231 if (ar
->state
== ATH10K_STATE_UTF
) {
236 /* start utf only when the driver is not in use */
237 if (ar
->state
!= ATH10K_STATE_OFF
) {
242 if (WARN_ON(ar
->testmode
.utf_mode_fw
.fw_file
.firmware
!= NULL
)) {
243 /* utf image is already downloaded, it shouldn't be */
248 ret
= ath10k_tm_fetch_firmware(ar
);
250 ath10k_err(ar
, "failed to fetch UTF firmware: %d", ret
);
254 if (ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_data
&&
255 ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_len
) {
256 ret
= ath10k_swap_code_seg_init(ar
,
257 &ar
->testmode
.utf_mode_fw
.fw_file
);
260 "failed to init utf code swap segment: %d\n",
262 goto err_release_utf_mode_fw
;
266 spin_lock_bh(&ar
->data_lock
);
267 ar
->testmode
.utf_monitor
= true;
268 spin_unlock_bh(&ar
->data_lock
);
270 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode wmi version %d\n",
271 ar
->testmode
.utf_mode_fw
.fw_file
.wmi_op_version
);
273 ret
= ath10k_hif_power_up(ar
);
275 ath10k_err(ar
, "failed to power up hif (testmode): %d\n", ret
);
276 ar
->state
= ATH10K_STATE_OFF
;
277 goto err_release_utf_mode_fw
;
280 ret
= ath10k_core_start(ar
, ATH10K_FIRMWARE_MODE_UTF
,
281 &ar
->testmode
.utf_mode_fw
);
283 ath10k_err(ar
, "failed to start core (testmode): %d\n", ret
);
284 ar
->state
= ATH10K_STATE_OFF
;
288 ar
->state
= ATH10K_STATE_UTF
;
290 if (strlen(ar
->testmode
.utf_mode_fw
.fw_file
.fw_version
) > 0)
291 ver
= ar
->testmode
.utf_mode_fw
.fw_file
.fw_version
;
295 ath10k_info(ar
, "UTF firmware %s started\n", ver
);
297 mutex_unlock(&ar
->conf_mutex
);
302 ath10k_hif_power_down(ar
);
304 err_release_utf_mode_fw
:
305 if (ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_data
&&
306 ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_len
)
307 ath10k_swap_code_seg_release(ar
,
308 &ar
->testmode
.utf_mode_fw
.fw_file
);
310 release_firmware(ar
->testmode
.utf_mode_fw
.fw_file
.firmware
);
311 ar
->testmode
.utf_mode_fw
.fw_file
.firmware
= NULL
;
314 mutex_unlock(&ar
->conf_mutex
);
319 static void __ath10k_tm_cmd_utf_stop(struct ath10k
*ar
)
321 lockdep_assert_held(&ar
->conf_mutex
);
323 ath10k_core_stop(ar
);
324 ath10k_hif_power_down(ar
);
326 spin_lock_bh(&ar
->data_lock
);
328 ar
->testmode
.utf_monitor
= false;
330 spin_unlock_bh(&ar
->data_lock
);
332 if (ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_data
&&
333 ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_len
)
334 ath10k_swap_code_seg_release(ar
,
335 &ar
->testmode
.utf_mode_fw
.fw_file
);
337 release_firmware(ar
->testmode
.utf_mode_fw
.fw_file
.firmware
);
338 ar
->testmode
.utf_mode_fw
.fw_file
.firmware
= NULL
;
340 ar
->state
= ATH10K_STATE_OFF
;
343 static int ath10k_tm_cmd_utf_stop(struct ath10k
*ar
, struct nlattr
*tb
[])
347 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode cmd utf stop\n");
349 mutex_lock(&ar
->conf_mutex
);
351 if (ar
->state
!= ATH10K_STATE_UTF
) {
356 __ath10k_tm_cmd_utf_stop(ar
);
360 ath10k_info(ar
, "UTF firmware stopped\n");
363 mutex_unlock(&ar
->conf_mutex
);
367 static int ath10k_tm_cmd_wmi(struct ath10k
*ar
, struct nlattr
*tb
[])
374 mutex_lock(&ar
->conf_mutex
);
376 if (ar
->state
!= ATH10K_STATE_UTF
) {
381 if (!tb
[ATH10K_TM_ATTR_DATA
]) {
386 if (!tb
[ATH10K_TM_ATTR_WMI_CMDID
]) {
391 buf
= nla_data(tb
[ATH10K_TM_ATTR_DATA
]);
392 buf_len
= nla_len(tb
[ATH10K_TM_ATTR_DATA
]);
393 cmd_id
= nla_get_u32(tb
[ATH10K_TM_ATTR_WMI_CMDID
]);
395 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
,
396 "testmode cmd wmi cmd_id %d buf %pK buf_len %d\n",
397 cmd_id
, buf
, buf_len
);
399 ath10k_dbg_dump(ar
, ATH10K_DBG_TESTMODE
, NULL
, "", buf
, buf_len
);
401 skb
= ath10k_wmi_alloc_skb(ar
, buf_len
);
407 memcpy(skb
->data
, buf
, buf_len
);
409 ret
= ath10k_wmi_cmd_send(ar
, skb
, cmd_id
);
411 ath10k_warn(ar
, "failed to transmit wmi command (testmode): %d\n",
419 mutex_unlock(&ar
->conf_mutex
);
423 int ath10k_tm_cmd(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
426 struct ath10k
*ar
= hw
->priv
;
427 struct nlattr
*tb
[ATH10K_TM_ATTR_MAX
+ 1];
430 ret
= nla_parse(tb
, ATH10K_TM_ATTR_MAX
, data
, len
, ath10k_tm_policy
,
435 if (!tb
[ATH10K_TM_ATTR_CMD
])
438 switch (nla_get_u32(tb
[ATH10K_TM_ATTR_CMD
])) {
439 case ATH10K_TM_CMD_GET_VERSION
:
440 return ath10k_tm_cmd_get_version(ar
, tb
);
441 case ATH10K_TM_CMD_UTF_START
:
442 return ath10k_tm_cmd_utf_start(ar
, tb
);
443 case ATH10K_TM_CMD_UTF_STOP
:
444 return ath10k_tm_cmd_utf_stop(ar
, tb
);
445 case ATH10K_TM_CMD_WMI
:
446 return ath10k_tm_cmd_wmi(ar
, tb
);
452 void ath10k_testmode_destroy(struct ath10k
*ar
)
454 mutex_lock(&ar
->conf_mutex
);
456 if (ar
->state
!= ATH10K_STATE_UTF
) {
457 /* utf firmware is not running, nothing to do */
461 __ath10k_tm_cmd_utf_stop(ar
);
464 mutex_unlock(&ar
->conf_mutex
);