Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / net / wireless / ath / ath10k / testmode.c
blob120f4234d3b0b5f023d00a940e11e03ac1498e39
1 /*
2 * Copyright (c) 2014 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"
27 #include "testmode_i.h"
29 static const struct nla_policy ath10k_tm_policy[ATH10K_TM_ATTR_MAX + 1] = {
30 [ATH10K_TM_ATTR_CMD] = { .type = NLA_U32 },
31 [ATH10K_TM_ATTR_DATA] = { .type = NLA_BINARY,
32 .len = ATH10K_TM_DATA_MAX_LEN },
33 [ATH10K_TM_ATTR_WMI_CMDID] = { .type = NLA_U32 },
34 [ATH10K_TM_ATTR_VERSION_MAJOR] = { .type = NLA_U32 },
35 [ATH10K_TM_ATTR_VERSION_MINOR] = { .type = NLA_U32 },
38 /* Returns true if callee consumes the skb and the skb should be discarded.
39 * Returns false if skb is not used. Does not sleep.
41 bool ath10k_tm_event_wmi(struct ath10k *ar, u32 cmd_id, struct sk_buff *skb)
43 struct sk_buff *nl_skb;
44 bool consumed;
45 int ret;
47 ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
48 "testmode event wmi cmd_id %d skb %p skb->len %d\n",
49 cmd_id, skb, skb->len);
51 ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", skb->data, skb->len);
53 spin_lock_bh(&ar->data_lock);
55 if (!ar->testmode.utf_monitor) {
56 consumed = false;
57 goto out;
60 /* Only testmode.c should be handling events from utf firmware,
61 * otherwise all sort of problems will arise as mac80211 operations
62 * are not initialised.
64 consumed = true;
66 nl_skb = cfg80211_testmode_alloc_event_skb(ar->hw->wiphy,
67 2 * sizeof(u32) + skb->len,
68 GFP_ATOMIC);
69 if (!nl_skb) {
70 ath10k_warn(ar,
71 "failed to allocate skb for testmode wmi event\n");
72 goto out;
75 ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_CMD, ATH10K_TM_CMD_WMI);
76 if (ret) {
77 ath10k_warn(ar,
78 "failed to to put testmode wmi event cmd attribute: %d\n",
79 ret);
80 kfree_skb(nl_skb);
81 goto out;
84 ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_WMI_CMDID, cmd_id);
85 if (ret) {
86 ath10k_warn(ar,
87 "failed to to put testmode wmi even cmd_id: %d\n",
88 ret);
89 kfree_skb(nl_skb);
90 goto out;
93 ret = nla_put(nl_skb, ATH10K_TM_ATTR_DATA, skb->len, skb->data);
94 if (ret) {
95 ath10k_warn(ar,
96 "failed to copy skb to testmode wmi event: %d\n",
97 ret);
98 kfree_skb(nl_skb);
99 goto out;
102 cfg80211_testmode_event(nl_skb, GFP_ATOMIC);
104 out:
105 spin_unlock_bh(&ar->data_lock);
107 return consumed;
110 static int ath10k_tm_cmd_get_version(struct ath10k *ar, struct nlattr *tb[])
112 struct sk_buff *skb;
113 int ret;
115 ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
116 "testmode cmd get version_major %d version_minor %d\n",
117 ATH10K_TESTMODE_VERSION_MAJOR,
118 ATH10K_TESTMODE_VERSION_MINOR);
120 skb = cfg80211_testmode_alloc_reply_skb(ar->hw->wiphy,
121 nla_total_size(sizeof(u32)));
122 if (!skb)
123 return -ENOMEM;
125 ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MAJOR,
126 ATH10K_TESTMODE_VERSION_MAJOR);
127 if (ret) {
128 kfree_skb(skb);
129 return ret;
132 ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MINOR,
133 ATH10K_TESTMODE_VERSION_MINOR);
134 if (ret) {
135 kfree_skb(skb);
136 return ret;
139 return cfg80211_testmode_reply(skb);
142 static int ath10k_tm_fetch_utf_firmware_api_1(struct ath10k *ar,
143 struct ath10k_fw_file *fw_file)
145 char filename[100];
146 int ret;
148 snprintf(filename, sizeof(filename), "%s/%s",
149 ar->hw_params.fw.dir, ATH10K_FW_UTF_FILE);
151 /* load utf firmware image */
152 ret = request_firmware(&fw_file->firmware, filename, ar->dev);
153 if (ret) {
154 ath10k_warn(ar, "failed to retrieve utf firmware '%s': %d\n",
155 filename, ret);
156 return ret;
159 /* We didn't find FW UTF API 1 ("utf.bin") does not advertise
160 * firmware features. Do an ugly hack where we force the firmware
161 * features to match with 10.1 branch so that wmi.c will use the
162 * correct WMI interface.
165 fw_file->wmi_op_version = ATH10K_FW_WMI_OP_VERSION_10_1;
166 fw_file->htt_op_version = ATH10K_FW_HTT_OP_VERSION_10_1;
167 fw_file->firmware_data = fw_file->firmware->data;
168 fw_file->firmware_len = fw_file->firmware->size;
170 return 0;
173 static int ath10k_tm_fetch_firmware(struct ath10k *ar)
175 struct ath10k_fw_components *utf_mode_fw;
176 int ret;
178 ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_UTF_API2_FILE,
179 &ar->testmode.utf_mode_fw.fw_file);
180 if (ret == 0) {
181 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode using fw utf api 2");
182 goto out;
185 ret = ath10k_tm_fetch_utf_firmware_api_1(ar, &ar->testmode.utf_mode_fw.fw_file);
186 if (ret) {
187 ath10k_err(ar, "failed to fetch utf firmware binary: %d", ret);
188 return ret;
191 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode using utf api 1");
193 out:
194 utf_mode_fw = &ar->testmode.utf_mode_fw;
196 /* Use the same board data file as the normal firmware uses (but
197 * it's still "owned" by normal_mode_fw so we shouldn't free it.
199 utf_mode_fw->board_data = ar->normal_mode_fw.board_data;
200 utf_mode_fw->board_len = ar->normal_mode_fw.board_len;
202 if (!utf_mode_fw->fw_file.otp_data) {
203 ath10k_info(ar, "utf.bin didn't contain otp binary, taking it from the normal mode firmware");
204 utf_mode_fw->fw_file.otp_data = ar->normal_mode_fw.fw_file.otp_data;
205 utf_mode_fw->fw_file.otp_len = ar->normal_mode_fw.fw_file.otp_len;
208 return 0;
211 static int ath10k_tm_cmd_utf_start(struct ath10k *ar, struct nlattr *tb[])
213 const char *ver;
214 int ret;
216 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf start\n");
218 mutex_lock(&ar->conf_mutex);
220 if (ar->state == ATH10K_STATE_UTF) {
221 ret = -EALREADY;
222 goto err;
225 /* start utf only when the driver is not in use */
226 if (ar->state != ATH10K_STATE_OFF) {
227 ret = -EBUSY;
228 goto err;
231 if (WARN_ON(ar->testmode.utf_mode_fw.fw_file.firmware != NULL)) {
232 /* utf image is already downloaded, it shouldn't be */
233 ret = -EEXIST;
234 goto err;
237 ret = ath10k_tm_fetch_firmware(ar);
238 if (ret) {
239 ath10k_err(ar, "failed to fetch UTF firmware: %d", ret);
240 goto err;
243 spin_lock_bh(&ar->data_lock);
244 ar->testmode.utf_monitor = true;
245 spin_unlock_bh(&ar->data_lock);
247 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode wmi version %d\n",
248 ar->testmode.utf_mode_fw.fw_file.wmi_op_version);
250 ret = ath10k_hif_power_up(ar);
251 if (ret) {
252 ath10k_err(ar, "failed to power up hif (testmode): %d\n", ret);
253 ar->state = ATH10K_STATE_OFF;
254 goto err_release_utf_mode_fw;
257 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_UTF,
258 &ar->testmode.utf_mode_fw);
259 if (ret) {
260 ath10k_err(ar, "failed to start core (testmode): %d\n", ret);
261 ar->state = ATH10K_STATE_OFF;
262 goto err_power_down;
265 ar->state = ATH10K_STATE_UTF;
267 if (strlen(ar->testmode.utf_mode_fw.fw_file.fw_version) > 0)
268 ver = ar->testmode.utf_mode_fw.fw_file.fw_version;
269 else
270 ver = "API 1";
272 ath10k_info(ar, "UTF firmware %s started\n", ver);
274 mutex_unlock(&ar->conf_mutex);
276 return 0;
278 err_power_down:
279 ath10k_hif_power_down(ar);
281 err_release_utf_mode_fw:
282 release_firmware(ar->testmode.utf_mode_fw.fw_file.firmware);
283 ar->testmode.utf_mode_fw.fw_file.firmware = NULL;
285 err:
286 mutex_unlock(&ar->conf_mutex);
288 return ret;
291 static void __ath10k_tm_cmd_utf_stop(struct ath10k *ar)
293 lockdep_assert_held(&ar->conf_mutex);
295 ath10k_core_stop(ar);
296 ath10k_hif_power_down(ar);
298 spin_lock_bh(&ar->data_lock);
300 ar->testmode.utf_monitor = false;
302 spin_unlock_bh(&ar->data_lock);
304 release_firmware(ar->testmode.utf_mode_fw.fw_file.firmware);
305 ar->testmode.utf_mode_fw.fw_file.firmware = NULL;
307 ar->state = ATH10K_STATE_OFF;
310 static int ath10k_tm_cmd_utf_stop(struct ath10k *ar, struct nlattr *tb[])
312 int ret;
314 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf stop\n");
316 mutex_lock(&ar->conf_mutex);
318 if (ar->state != ATH10K_STATE_UTF) {
319 ret = -ENETDOWN;
320 goto out;
323 __ath10k_tm_cmd_utf_stop(ar);
325 ret = 0;
327 ath10k_info(ar, "UTF firmware stopped\n");
329 out:
330 mutex_unlock(&ar->conf_mutex);
331 return ret;
334 static int ath10k_tm_cmd_wmi(struct ath10k *ar, struct nlattr *tb[])
336 struct sk_buff *skb;
337 int ret, buf_len;
338 u32 cmd_id;
339 void *buf;
341 mutex_lock(&ar->conf_mutex);
343 if (ar->state != ATH10K_STATE_UTF) {
344 ret = -ENETDOWN;
345 goto out;
348 if (!tb[ATH10K_TM_ATTR_DATA]) {
349 ret = -EINVAL;
350 goto out;
353 if (!tb[ATH10K_TM_ATTR_WMI_CMDID]) {
354 ret = -EINVAL;
355 goto out;
358 buf = nla_data(tb[ATH10K_TM_ATTR_DATA]);
359 buf_len = nla_len(tb[ATH10K_TM_ATTR_DATA]);
360 cmd_id = nla_get_u32(tb[ATH10K_TM_ATTR_WMI_CMDID]);
362 ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
363 "testmode cmd wmi cmd_id %d buf %p buf_len %d\n",
364 cmd_id, buf, buf_len);
366 ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", buf, buf_len);
368 skb = ath10k_wmi_alloc_skb(ar, buf_len);
369 if (!skb) {
370 ret = -ENOMEM;
371 goto out;
374 memcpy(skb->data, buf, buf_len);
376 ret = ath10k_wmi_cmd_send(ar, skb, cmd_id);
377 if (ret) {
378 ath10k_warn(ar, "failed to transmit wmi command (testmode): %d\n",
379 ret);
380 goto out;
383 ret = 0;
385 out:
386 mutex_unlock(&ar->conf_mutex);
387 return ret;
390 int ath10k_tm_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
391 void *data, int len)
393 struct ath10k *ar = hw->priv;
394 struct nlattr *tb[ATH10K_TM_ATTR_MAX + 1];
395 int ret;
397 ret = nla_parse(tb, ATH10K_TM_ATTR_MAX, data, len,
398 ath10k_tm_policy);
399 if (ret)
400 return ret;
402 if (!tb[ATH10K_TM_ATTR_CMD])
403 return -EINVAL;
405 switch (nla_get_u32(tb[ATH10K_TM_ATTR_CMD])) {
406 case ATH10K_TM_CMD_GET_VERSION:
407 return ath10k_tm_cmd_get_version(ar, tb);
408 case ATH10K_TM_CMD_UTF_START:
409 return ath10k_tm_cmd_utf_start(ar, tb);
410 case ATH10K_TM_CMD_UTF_STOP:
411 return ath10k_tm_cmd_utf_stop(ar, tb);
412 case ATH10K_TM_CMD_WMI:
413 return ath10k_tm_cmd_wmi(ar, tb);
414 default:
415 return -EOPNOTSUPP;
419 void ath10k_testmode_destroy(struct ath10k *ar)
421 mutex_lock(&ar->conf_mutex);
423 if (ar->state != ATH10K_STATE_UTF) {
424 /* utf firmware is not running, nothing to do */
425 goto out;
428 __ath10k_tm_cmd_utf_stop(ar);
430 out:
431 mutex_unlock(&ar->conf_mutex);