1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2014-2017 Qualcomm Atheros, Inc.
8 #include <net/netlink.h>
9 #include <linux/firmware.h>
17 #include "testmode_i.h"
19 static const struct nla_policy ath10k_tm_policy
[ATH10K_TM_ATTR_MAX
+ 1] = {
20 [ATH10K_TM_ATTR_CMD
] = { .type
= NLA_U32
},
21 [ATH10K_TM_ATTR_DATA
] = { .type
= NLA_BINARY
,
22 .len
= ATH10K_TM_DATA_MAX_LEN
},
23 [ATH10K_TM_ATTR_WMI_CMDID
] = { .type
= NLA_U32
},
24 [ATH10K_TM_ATTR_VERSION_MAJOR
] = { .type
= NLA_U32
},
25 [ATH10K_TM_ATTR_VERSION_MINOR
] = { .type
= NLA_U32
},
28 /* Returns true if callee consumes the skb and the skb should be discarded.
29 * Returns false if skb is not used. Does not sleep.
31 bool ath10k_tm_event_wmi(struct ath10k
*ar
, u32 cmd_id
, struct sk_buff
*skb
)
33 struct sk_buff
*nl_skb
;
37 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
,
38 "testmode event wmi cmd_id %d skb %pK skb->len %d\n",
39 cmd_id
, skb
, skb
->len
);
41 ath10k_dbg_dump(ar
, ATH10K_DBG_TESTMODE
, NULL
, "", skb
->data
, skb
->len
);
43 spin_lock_bh(&ar
->data_lock
);
45 if (!ar
->testmode
.utf_monitor
) {
50 /* Only testmode.c should be handling events from utf firmware,
51 * otherwise all sort of problems will arise as mac80211 operations
52 * are not initialised.
56 nl_skb
= cfg80211_testmode_alloc_event_skb(ar
->hw
->wiphy
,
57 2 * sizeof(u32
) + skb
->len
,
61 "failed to allocate skb for testmode wmi event\n");
65 ret
= nla_put_u32(nl_skb
, ATH10K_TM_ATTR_CMD
, ATH10K_TM_CMD_WMI
);
68 "failed to put testmode wmi event cmd attribute: %d\n",
74 ret
= nla_put_u32(nl_skb
, ATH10K_TM_ATTR_WMI_CMDID
, cmd_id
);
77 "failed to put testmode wmi event cmd_id: %d\n",
83 ret
= nla_put(nl_skb
, ATH10K_TM_ATTR_DATA
, skb
->len
, skb
->data
);
86 "failed to copy skb to testmode wmi event: %d\n",
92 cfg80211_testmode_event(nl_skb
, GFP_ATOMIC
);
95 spin_unlock_bh(&ar
->data_lock
);
100 static int ath10k_tm_cmd_get_version(struct ath10k
*ar
, struct nlattr
*tb
[])
105 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
,
106 "testmode cmd get version_major %d version_minor %d\n",
107 ATH10K_TESTMODE_VERSION_MAJOR
,
108 ATH10K_TESTMODE_VERSION_MINOR
);
110 skb
= cfg80211_testmode_alloc_reply_skb(ar
->hw
->wiphy
,
111 nla_total_size(sizeof(u32
)));
115 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_VERSION_MAJOR
,
116 ATH10K_TESTMODE_VERSION_MAJOR
);
122 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_VERSION_MINOR
,
123 ATH10K_TESTMODE_VERSION_MINOR
);
129 ret
= nla_put_u32(skb
, ATH10K_TM_ATTR_WMI_OP_VERSION
,
130 ar
->normal_mode_fw
.fw_file
.wmi_op_version
);
136 return cfg80211_testmode_reply(skb
);
139 static int ath10k_tm_fetch_utf_firmware_api_1(struct ath10k
*ar
,
140 struct ath10k_fw_file
*fw_file
)
145 snprintf(filename
, sizeof(filename
), "%s/%s",
146 ar
->hw_params
.fw
.dir
, ATH10K_FW_UTF_FILE
);
148 /* load utf firmware image */
149 ret
= firmware_request_nowarn(&fw_file
->firmware
, filename
, ar
->dev
);
150 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode fw request '%s': %d\n",
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
;
180 switch (ar
->hif
.bus
) {
181 case ATH10K_BUS_SDIO
:
183 scnprintf(fw_name
, sizeof(fw_name
), "%s-%s-%d.bin",
184 ATH10K_FW_UTF_FILE_BASE
, ath10k_bus_str(ar
->hif
.bus
),
188 scnprintf(fw_name
, sizeof(fw_name
), "%s-%d.bin",
189 ATH10K_FW_UTF_FILE_BASE
, fw_api2
);
193 ret
= ath10k_core_fetch_firmware_api_n(ar
, fw_name
,
194 &ar
->testmode
.utf_mode_fw
.fw_file
);
196 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode using fw utf api 2");
200 ret
= ath10k_tm_fetch_utf_firmware_api_1(ar
, &ar
->testmode
.utf_mode_fw
.fw_file
);
202 ath10k_err(ar
, "failed to fetch utf firmware binary: %d", ret
);
206 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode using utf api 1");
209 utf_mode_fw
= &ar
->testmode
.utf_mode_fw
;
211 /* Use the same board data file as the normal firmware uses (but
212 * it's still "owned" by normal_mode_fw so we shouldn't free it.
214 utf_mode_fw
->board_data
= ar
->normal_mode_fw
.board_data
;
215 utf_mode_fw
->board_len
= ar
->normal_mode_fw
.board_len
;
217 if (!utf_mode_fw
->fw_file
.otp_data
) {
218 ath10k_info(ar
, "utf.bin didn't contain otp binary, taking it from the normal mode firmware");
219 utf_mode_fw
->fw_file
.otp_data
= ar
->normal_mode_fw
.fw_file
.otp_data
;
220 utf_mode_fw
->fw_file
.otp_len
= ar
->normal_mode_fw
.fw_file
.otp_len
;
226 static int ath10k_tm_cmd_utf_start(struct ath10k
*ar
, struct nlattr
*tb
[])
231 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode cmd utf start\n");
233 mutex_lock(&ar
->conf_mutex
);
235 if (ar
->state
== ATH10K_STATE_UTF
) {
240 /* start utf only when the driver is not in use */
241 if (ar
->state
!= ATH10K_STATE_OFF
) {
246 if (WARN_ON(ar
->testmode
.utf_mode_fw
.fw_file
.firmware
!= NULL
)) {
247 /* utf image is already downloaded, it shouldn't be */
252 ret
= ath10k_tm_fetch_firmware(ar
);
254 ath10k_err(ar
, "failed to fetch UTF firmware: %d", ret
);
258 if (ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_data
&&
259 ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_len
) {
260 ret
= ath10k_swap_code_seg_init(ar
,
261 &ar
->testmode
.utf_mode_fw
.fw_file
);
264 "failed to init utf code swap segment: %d\n",
266 goto err_release_utf_mode_fw
;
270 spin_lock_bh(&ar
->data_lock
);
271 ar
->testmode
.utf_monitor
= true;
272 spin_unlock_bh(&ar
->data_lock
);
274 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode wmi version %d\n",
275 ar
->testmode
.utf_mode_fw
.fw_file
.wmi_op_version
);
277 ret
= ath10k_hif_power_up(ar
, ATH10K_FIRMWARE_MODE_UTF
);
279 ath10k_err(ar
, "failed to power up hif (testmode): %d\n", ret
);
280 ar
->state
= ATH10K_STATE_OFF
;
281 goto err_release_utf_mode_fw
;
284 ret
= ath10k_core_start(ar
, ATH10K_FIRMWARE_MODE_UTF
,
285 &ar
->testmode
.utf_mode_fw
);
287 ath10k_err(ar
, "failed to start core (testmode): %d\n", ret
);
288 ar
->state
= ATH10K_STATE_OFF
;
292 ar
->state
= ATH10K_STATE_UTF
;
294 if (strlen(ar
->testmode
.utf_mode_fw
.fw_file
.fw_version
) > 0)
295 ver
= ar
->testmode
.utf_mode_fw
.fw_file
.fw_version
;
299 ath10k_info(ar
, "UTF firmware %s started\n", ver
);
301 mutex_unlock(&ar
->conf_mutex
);
306 ath10k_hif_power_down(ar
);
308 err_release_utf_mode_fw
:
309 if (ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_data
&&
310 ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_len
)
311 ath10k_swap_code_seg_release(ar
,
312 &ar
->testmode
.utf_mode_fw
.fw_file
);
314 release_firmware(ar
->testmode
.utf_mode_fw
.fw_file
.firmware
);
315 ar
->testmode
.utf_mode_fw
.fw_file
.firmware
= NULL
;
318 mutex_unlock(&ar
->conf_mutex
);
323 static void __ath10k_tm_cmd_utf_stop(struct ath10k
*ar
)
325 lockdep_assert_held(&ar
->conf_mutex
);
327 ath10k_core_stop(ar
);
328 ath10k_hif_power_down(ar
);
330 spin_lock_bh(&ar
->data_lock
);
332 ar
->testmode
.utf_monitor
= false;
334 spin_unlock_bh(&ar
->data_lock
);
336 if (ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_data
&&
337 ar
->testmode
.utf_mode_fw
.fw_file
.codeswap_len
)
338 ath10k_swap_code_seg_release(ar
,
339 &ar
->testmode
.utf_mode_fw
.fw_file
);
341 release_firmware(ar
->testmode
.utf_mode_fw
.fw_file
.firmware
);
342 ar
->testmode
.utf_mode_fw
.fw_file
.firmware
= NULL
;
344 ar
->state
= ATH10K_STATE_OFF
;
347 static int ath10k_tm_cmd_utf_stop(struct ath10k
*ar
, struct nlattr
*tb
[])
351 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
, "testmode cmd utf stop\n");
353 mutex_lock(&ar
->conf_mutex
);
355 if (ar
->state
!= ATH10K_STATE_UTF
) {
360 __ath10k_tm_cmd_utf_stop(ar
);
364 ath10k_info(ar
, "UTF firmware stopped\n");
367 mutex_unlock(&ar
->conf_mutex
);
371 static int ath10k_tm_cmd_wmi(struct ath10k
*ar
, struct nlattr
*tb
[])
378 mutex_lock(&ar
->conf_mutex
);
380 if (ar
->state
!= ATH10K_STATE_UTF
) {
385 if (!tb
[ATH10K_TM_ATTR_DATA
]) {
390 if (!tb
[ATH10K_TM_ATTR_WMI_CMDID
]) {
395 buf
= nla_data(tb
[ATH10K_TM_ATTR_DATA
]);
396 buf_len
= nla_len(tb
[ATH10K_TM_ATTR_DATA
]);
397 cmd_id
= nla_get_u32(tb
[ATH10K_TM_ATTR_WMI_CMDID
]);
399 ath10k_dbg(ar
, ATH10K_DBG_TESTMODE
,
400 "testmode cmd wmi cmd_id %d buf %pK buf_len %d\n",
401 cmd_id
, buf
, buf_len
);
403 ath10k_dbg_dump(ar
, ATH10K_DBG_TESTMODE
, NULL
, "", buf
, buf_len
);
405 skb
= ath10k_wmi_alloc_skb(ar
, buf_len
);
411 memcpy(skb
->data
, buf
, buf_len
);
413 ret
= ath10k_wmi_cmd_send(ar
, skb
, cmd_id
);
415 ath10k_warn(ar
, "failed to transmit wmi command (testmode): %d\n",
423 mutex_unlock(&ar
->conf_mutex
);
427 int ath10k_tm_cmd(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
430 struct ath10k
*ar
= hw
->priv
;
431 struct nlattr
*tb
[ATH10K_TM_ATTR_MAX
+ 1];
434 ret
= nla_parse_deprecated(tb
, ATH10K_TM_ATTR_MAX
, data
, len
,
435 ath10k_tm_policy
, NULL
);
439 if (!tb
[ATH10K_TM_ATTR_CMD
])
442 switch (nla_get_u32(tb
[ATH10K_TM_ATTR_CMD
])) {
443 case ATH10K_TM_CMD_GET_VERSION
:
444 return ath10k_tm_cmd_get_version(ar
, tb
);
445 case ATH10K_TM_CMD_UTF_START
:
446 return ath10k_tm_cmd_utf_start(ar
, tb
);
447 case ATH10K_TM_CMD_UTF_STOP
:
448 return ath10k_tm_cmd_utf_stop(ar
, tb
);
449 case ATH10K_TM_CMD_WMI
:
450 return ath10k_tm_cmd_wmi(ar
, tb
);
456 void ath10k_testmode_destroy(struct ath10k
*ar
)
458 mutex_lock(&ar
->conf_mutex
);
460 if (ar
->state
!= ATH10K_STATE_UTF
) {
461 /* utf firmware is not running, nothing to do */
465 __ath10k_tm_cmd_utf_stop(ar
);
468 mutex_unlock(&ar
->conf_mutex
);