perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath10k / testmode.c
blobc24ee616833c7b569d1bbe691161413acec6f8a7
1 /*
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.
17 #include "testmode.h"
19 #include <net/netlink.h>
20 #include <linux/firmware.h>
22 #include "debug.h"
23 #include "wmi.h"
24 #include "hif.h"
25 #include "hw.h"
26 #include "core.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;
45 bool consumed;
46 int ret;
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) {
57 consumed = false;
58 goto out;
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.
65 consumed = true;
67 nl_skb = cfg80211_testmode_alloc_event_skb(ar->hw->wiphy,
68 2 * sizeof(u32) + skb->len,
69 GFP_ATOMIC);
70 if (!nl_skb) {
71 ath10k_warn(ar,
72 "failed to allocate skb for testmode wmi event\n");
73 goto out;
76 ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_CMD, ATH10K_TM_CMD_WMI);
77 if (ret) {
78 ath10k_warn(ar,
79 "failed to to put testmode wmi event cmd attribute: %d\n",
80 ret);
81 kfree_skb(nl_skb);
82 goto out;
85 ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_WMI_CMDID, cmd_id);
86 if (ret) {
87 ath10k_warn(ar,
88 "failed to to put testmode wmi even cmd_id: %d\n",
89 ret);
90 kfree_skb(nl_skb);
91 goto out;
94 ret = nla_put(nl_skb, ATH10K_TM_ATTR_DATA, skb->len, skb->data);
95 if (ret) {
96 ath10k_warn(ar,
97 "failed to copy skb to testmode wmi event: %d\n",
98 ret);
99 kfree_skb(nl_skb);
100 goto out;
103 cfg80211_testmode_event(nl_skb, GFP_ATOMIC);
105 out:
106 spin_unlock_bh(&ar->data_lock);
108 return consumed;
111 static int ath10k_tm_cmd_get_version(struct ath10k *ar, struct nlattr *tb[])
113 struct sk_buff *skb;
114 int ret;
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)));
123 if (!skb)
124 return -ENOMEM;
126 ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MAJOR,
127 ATH10K_TESTMODE_VERSION_MAJOR);
128 if (ret) {
129 kfree_skb(skb);
130 return ret;
133 ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MINOR,
134 ATH10K_TESTMODE_VERSION_MINOR);
135 if (ret) {
136 kfree_skb(skb);
137 return ret;
140 ret = nla_put_u32(skb, ATH10K_TM_ATTR_WMI_OP_VERSION,
141 ar->normal_mode_fw.fw_file.wmi_op_version);
142 if (ret) {
143 kfree_skb(skb);
144 return ret;
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)
153 char filename[100];
154 int ret;
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",
162 filename, ret);
164 if (ret) {
165 ath10k_warn(ar, "failed to retrieve utf firmware '%s': %d\n",
166 filename, ret);
167 return ret;
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;
181 return 0;
184 static int ath10k_tm_fetch_firmware(struct ath10k *ar)
186 struct ath10k_fw_components *utf_mode_fw;
187 int ret;
189 ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_UTF_API2_FILE,
190 &ar->testmode.utf_mode_fw.fw_file);
191 if (ret == 0) {
192 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode using fw utf api 2");
193 goto out;
196 ret = ath10k_tm_fetch_utf_firmware_api_1(ar, &ar->testmode.utf_mode_fw.fw_file);
197 if (ret) {
198 ath10k_err(ar, "failed to fetch utf firmware binary: %d", ret);
199 return ret;
202 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode using utf api 1");
204 out:
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;
219 return 0;
222 static int ath10k_tm_cmd_utf_start(struct ath10k *ar, struct nlattr *tb[])
224 const char *ver;
225 int ret;
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) {
232 ret = -EALREADY;
233 goto err;
236 /* start utf only when the driver is not in use */
237 if (ar->state != ATH10K_STATE_OFF) {
238 ret = -EBUSY;
239 goto err;
242 if (WARN_ON(ar->testmode.utf_mode_fw.fw_file.firmware != NULL)) {
243 /* utf image is already downloaded, it shouldn't be */
244 ret = -EEXIST;
245 goto err;
248 ret = ath10k_tm_fetch_firmware(ar);
249 if (ret) {
250 ath10k_err(ar, "failed to fetch UTF firmware: %d", ret);
251 goto err;
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);
258 if (ret) {
259 ath10k_warn(ar,
260 "failed to init utf code swap segment: %d\n",
261 ret);
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);
274 if (ret) {
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);
282 if (ret) {
283 ath10k_err(ar, "failed to start core (testmode): %d\n", ret);
284 ar->state = ATH10K_STATE_OFF;
285 goto err_power_down;
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;
292 else
293 ver = "API 1";
295 ath10k_info(ar, "UTF firmware %s started\n", ver);
297 mutex_unlock(&ar->conf_mutex);
299 return 0;
301 err_power_down:
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;
313 err:
314 mutex_unlock(&ar->conf_mutex);
316 return ret;
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[])
345 int ret;
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) {
352 ret = -ENETDOWN;
353 goto out;
356 __ath10k_tm_cmd_utf_stop(ar);
358 ret = 0;
360 ath10k_info(ar, "UTF firmware stopped\n");
362 out:
363 mutex_unlock(&ar->conf_mutex);
364 return ret;
367 static int ath10k_tm_cmd_wmi(struct ath10k *ar, struct nlattr *tb[])
369 struct sk_buff *skb;
370 int ret, buf_len;
371 u32 cmd_id;
372 void *buf;
374 mutex_lock(&ar->conf_mutex);
376 if (ar->state != ATH10K_STATE_UTF) {
377 ret = -ENETDOWN;
378 goto out;
381 if (!tb[ATH10K_TM_ATTR_DATA]) {
382 ret = -EINVAL;
383 goto out;
386 if (!tb[ATH10K_TM_ATTR_WMI_CMDID]) {
387 ret = -EINVAL;
388 goto out;
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);
402 if (!skb) {
403 ret = -ENOMEM;
404 goto out;
407 memcpy(skb->data, buf, buf_len);
409 ret = ath10k_wmi_cmd_send(ar, skb, cmd_id);
410 if (ret) {
411 ath10k_warn(ar, "failed to transmit wmi command (testmode): %d\n",
412 ret);
413 goto out;
416 ret = 0;
418 out:
419 mutex_unlock(&ar->conf_mutex);
420 return ret;
423 int ath10k_tm_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
424 void *data, int len)
426 struct ath10k *ar = hw->priv;
427 struct nlattr *tb[ATH10K_TM_ATTR_MAX + 1];
428 int ret;
430 ret = nla_parse(tb, ATH10K_TM_ATTR_MAX, data, len, ath10k_tm_policy,
431 NULL);
432 if (ret)
433 return ret;
435 if (!tb[ATH10K_TM_ATTR_CMD])
436 return -EINVAL;
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);
447 default:
448 return -EOPNOTSUPP;
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 */
458 goto out;
461 __ath10k_tm_cmd_utf_stop(ar);
463 out:
464 mutex_unlock(&ar->conf_mutex);