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.
19 #include <net/netlink.h>
20 #include <linux/firmware.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
;
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
) {
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.
66 nl_skb
= cfg80211_testmode_alloc_event_skb(ar
->hw
->wiphy
,
67 2 * sizeof(u32
) + skb
->len
,
71 "failed to allocate skb for testmode wmi event\n");
75 ret
= nla_put_u32(nl_skb
, ATH10K_TM_ATTR_CMD
, ATH10K_TM_CMD_WMI
);
78 "failed to to put testmode wmi event cmd attribute: %d\n",
84 ret
= nla_put_u32(nl_skb
, ATH10K_TM_ATTR_WMI_CMDID
, cmd_id
);
87 "failed to to put testmode wmi even cmd_id: %d\n",
93 ret
= nla_put(nl_skb
, ATH10K_TM_ATTR_DATA
, skb
->len
, skb
->data
);
96 "failed to copy skb to testmode wmi event: %d\n",
102 cfg80211_testmode_event(nl_skb
, GFP_ATOMIC
);
105 spin_unlock_bh(&ar
->data_lock
);
110 static int ath10k_tm_cmd_get_version(struct ath10k
*ar
, struct nlattr
*tb
[])
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
)));
125 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_VERSION_MAJOR
,
126 ATH10K_TESTMODE_VERSION_MAJOR
);
132 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_VERSION_MINOR
,
133 ATH10K_TESTMODE_VERSION_MINOR
);
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
)
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
);
154 ath10k_warn(ar
, "failed to retrieve utf firmware '%s': %d\n",
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
;
173 static int ath10k_tm_fetch_firmware(struct ath10k
*ar
)
175 struct ath10k_fw_components
*utf_mode_fw
;
178 ret
= ath10k_core_fetch_firmware_api_n(ar
, ATH10K_FW_UTF_API2_FILE
,
179 &ar
->testmode
.utf_mode_fw
.fw_file
);
181 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode using fw utf api 2");
185 ret
= ath10k_tm_fetch_utf_firmware_api_1(ar
, &ar
->testmode
.utf_mode_fw
.fw_file
);
187 ath10k_err(ar
, "failed to fetch utf firmware binary: %d", ret
);
191 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode using utf api 1");
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
;
211 static int ath10k_tm_cmd_utf_start(struct ath10k
*ar
, struct nlattr
*tb
[])
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
) {
225 /* start utf only when the driver is not in use */
226 if (ar
->state
!= ATH10K_STATE_OFF
) {
231 if (WARN_ON(ar
->testmode
.utf_mode_fw
.fw_file
.firmware
!= NULL
)) {
232 /* utf image is already downloaded, it shouldn't be */
237 ret
= ath10k_tm_fetch_firmware(ar
);
239 ath10k_err(ar
, "failed to fetch UTF firmware: %d", ret
);
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
);
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
);
260 ath10k_err(ar
, "failed to start core (testmode): %d\n", ret
);
261 ar
->state
= ATH10K_STATE_OFF
;
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
;
272 ath10k_info(ar
, "UTF firmware %s started\n", ver
);
274 mutex_unlock(&ar
->conf_mutex
);
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
;
286 mutex_unlock(&ar
->conf_mutex
);
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
[])
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
) {
323 __ath10k_tm_cmd_utf_stop(ar
);
327 ath10k_info(ar
, "UTF firmware stopped\n");
330 mutex_unlock(&ar
->conf_mutex
);
334 static int ath10k_tm_cmd_wmi(struct ath10k
*ar
, struct nlattr
*tb
[])
341 mutex_lock(&ar
->conf_mutex
);
343 if (ar
->state
!= ATH10K_STATE_UTF
) {
348 if (!tb
[ATH10K_TM_ATTR_DATA
]) {
353 if (!tb
[ATH10K_TM_ATTR_WMI_CMDID
]) {
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
);
374 memcpy(skb
->data
, buf
, buf_len
);
376 ret
= ath10k_wmi_cmd_send(ar
, skb
, cmd_id
);
378 ath10k_warn(ar
, "failed to transmit wmi command (testmode): %d\n",
386 mutex_unlock(&ar
->conf_mutex
);
390 int ath10k_tm_cmd(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
393 struct ath10k
*ar
= hw
->priv
;
394 struct nlattr
*tb
[ATH10K_TM_ATTR_MAX
+ 1];
397 ret
= nla_parse(tb
, ATH10K_TM_ATTR_MAX
, data
, len
,
402 if (!tb
[ATH10K_TM_ATTR_CMD
])
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
);
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 */
428 __ath10k_tm_cmd_utf_stop(ar
);
431 mutex_unlock(&ar
->conf_mutex
);