xref: /linux/drivers/net/wireless/ath/ath10k/testmode.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Copyright (c) 2014 Qualcomm Atheros, Inc.
3  *
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.
7  *
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.
15  */
16 
17 #include "testmode.h"
18 
19 #include <net/netlink.h>
20 #include <linux/firmware.h>
21 
22 #include "debug.h"
23 #include "wmi.h"
24 #include "hif.h"
25 #include "hw.h"
26 
27 #include "testmode_i.h"
28 
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 },
36 };
37 
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.
40  */
41 bool ath10k_tm_event_wmi(struct ath10k *ar, u32 cmd_id, struct sk_buff *skb)
42 {
43 	struct sk_buff *nl_skb;
44 	bool consumed;
45 	int ret;
46 
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);
50 
51 	ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", skb->data, skb->len);
52 
53 	spin_lock_bh(&ar->data_lock);
54 
55 	if (!ar->testmode.utf_monitor) {
56 		consumed = false;
57 		goto out;
58 	}
59 
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.
63 	 */
64 	consumed = true;
65 
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;
73 	}
74 
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;
82 	}
83 
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;
91 	}
92 
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;
100 	}
101 
102 	cfg80211_testmode_event(nl_skb, GFP_ATOMIC);
103 
104 out:
105 	spin_unlock_bh(&ar->data_lock);
106 
107 	return consumed;
108 }
109 
110 static int ath10k_tm_cmd_get_version(struct ath10k *ar, struct nlattr *tb[])
111 {
112 	struct sk_buff *skb;
113 	int ret;
114 
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);
119 
120 	skb = cfg80211_testmode_alloc_reply_skb(ar->hw->wiphy,
121 						nla_total_size(sizeof(u32)));
122 	if (!skb)
123 		return -ENOMEM;
124 
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;
130 	}
131 
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;
137 	}
138 
139 	return cfg80211_testmode_reply(skb);
140 }
141 
142 static int ath10k_tm_cmd_utf_start(struct ath10k *ar, struct nlattr *tb[])
143 {
144 	char filename[100];
145 	int ret;
146 
147 	ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf start\n");
148 
149 	mutex_lock(&ar->conf_mutex);
150 
151 	if (ar->state == ATH10K_STATE_UTF) {
152 		ret = -EALREADY;
153 		goto err;
154 	}
155 
156 	/* start utf only when the driver is not in use  */
157 	if (ar->state != ATH10K_STATE_OFF) {
158 		ret = -EBUSY;
159 		goto err;
160 	}
161 
162 	if (WARN_ON(ar->testmode.utf != NULL)) {
163 		/* utf image is already downloaded, it shouldn't be */
164 		ret = -EEXIST;
165 		goto err;
166 	}
167 
168 	snprintf(filename, sizeof(filename), "%s/%s",
169 		 ar->hw_params.fw.dir, ATH10K_FW_UTF_FILE);
170 
171 	/* load utf firmware image */
172 	ret = request_firmware(&ar->testmode.utf, filename, ar->dev);
173 	if (ret) {
174 		ath10k_warn(ar, "failed to retrieve utf firmware '%s': %d\n",
175 			    filename, ret);
176 		goto err;
177 	}
178 
179 	spin_lock_bh(&ar->data_lock);
180 
181 	ar->testmode.utf_monitor = true;
182 
183 	spin_unlock_bh(&ar->data_lock);
184 
185 	BUILD_BUG_ON(sizeof(ar->fw_features) !=
186 		     sizeof(ar->testmode.orig_fw_features));
187 
188 	memcpy(ar->testmode.orig_fw_features, ar->fw_features,
189 	       sizeof(ar->fw_features));
190 	ar->testmode.orig_wmi_op_version = ar->wmi.op_version;
191 
192 	/* utf.bin firmware image does not advertise firmware features. Do
193 	 * an ugly hack where we force the firmware features so that wmi.c
194 	 * will use the correct WMI interface.
195 	 */
196 	memset(ar->fw_features, 0, sizeof(ar->fw_features));
197 	ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_10_1;
198 
199 	ret = ath10k_hif_power_up(ar);
200 	if (ret) {
201 		ath10k_err(ar, "failed to power up hif (testmode): %d\n", ret);
202 		ar->state = ATH10K_STATE_OFF;
203 		goto err_fw_features;
204 	}
205 
206 	ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_UTF);
207 	if (ret) {
208 		ath10k_err(ar, "failed to start core (testmode): %d\n", ret);
209 		ar->state = ATH10K_STATE_OFF;
210 		goto err_power_down;
211 	}
212 
213 	ar->state = ATH10K_STATE_UTF;
214 
215 	ath10k_info(ar, "UTF firmware started\n");
216 
217 	mutex_unlock(&ar->conf_mutex);
218 
219 	return 0;
220 
221 err_power_down:
222 	ath10k_hif_power_down(ar);
223 
224 err_fw_features:
225 	/* return the original firmware features */
226 	memcpy(ar->fw_features, ar->testmode.orig_fw_features,
227 	       sizeof(ar->fw_features));
228 	ar->wmi.op_version = ar->testmode.orig_wmi_op_version;
229 
230 	release_firmware(ar->testmode.utf);
231 	ar->testmode.utf = NULL;
232 
233 err:
234 	mutex_unlock(&ar->conf_mutex);
235 
236 	return ret;
237 }
238 
239 static void __ath10k_tm_cmd_utf_stop(struct ath10k *ar)
240 {
241 	lockdep_assert_held(&ar->conf_mutex);
242 
243 	ath10k_core_stop(ar);
244 	ath10k_hif_power_down(ar);
245 
246 	spin_lock_bh(&ar->data_lock);
247 
248 	ar->testmode.utf_monitor = false;
249 
250 	spin_unlock_bh(&ar->data_lock);
251 
252 	/* return the original firmware features */
253 	memcpy(ar->fw_features, ar->testmode.orig_fw_features,
254 	       sizeof(ar->fw_features));
255 	ar->wmi.op_version = ar->testmode.orig_wmi_op_version;
256 
257 	release_firmware(ar->testmode.utf);
258 	ar->testmode.utf = NULL;
259 
260 	ar->state = ATH10K_STATE_OFF;
261 }
262 
263 static int ath10k_tm_cmd_utf_stop(struct ath10k *ar, struct nlattr *tb[])
264 {
265 	int ret;
266 
267 	ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf stop\n");
268 
269 	mutex_lock(&ar->conf_mutex);
270 
271 	if (ar->state != ATH10K_STATE_UTF) {
272 		ret = -ENETDOWN;
273 		goto out;
274 	}
275 
276 	__ath10k_tm_cmd_utf_stop(ar);
277 
278 	ret = 0;
279 
280 	ath10k_info(ar, "UTF firmware stopped\n");
281 
282 out:
283 	mutex_unlock(&ar->conf_mutex);
284 	return ret;
285 }
286 
287 static int ath10k_tm_cmd_wmi(struct ath10k *ar, struct nlattr *tb[])
288 {
289 	struct sk_buff *skb;
290 	int ret, buf_len;
291 	u32 cmd_id;
292 	void *buf;
293 
294 	mutex_lock(&ar->conf_mutex);
295 
296 	if (ar->state != ATH10K_STATE_UTF) {
297 		ret = -ENETDOWN;
298 		goto out;
299 	}
300 
301 	if (!tb[ATH10K_TM_ATTR_DATA]) {
302 		ret = -EINVAL;
303 		goto out;
304 	}
305 
306 	if (!tb[ATH10K_TM_ATTR_WMI_CMDID]) {
307 		ret = -EINVAL;
308 		goto out;
309 	}
310 
311 	buf = nla_data(tb[ATH10K_TM_ATTR_DATA]);
312 	buf_len = nla_len(tb[ATH10K_TM_ATTR_DATA]);
313 	cmd_id = nla_get_u32(tb[ATH10K_TM_ATTR_WMI_CMDID]);
314 
315 	ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
316 		   "testmode cmd wmi cmd_id %d buf %p buf_len %d\n",
317 		   cmd_id, buf, buf_len);
318 
319 	ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", buf, buf_len);
320 
321 	skb = ath10k_wmi_alloc_skb(ar, buf_len);
322 	if (!skb) {
323 		ret = -ENOMEM;
324 		goto out;
325 	}
326 
327 	memcpy(skb->data, buf, buf_len);
328 
329 	ret = ath10k_wmi_cmd_send(ar, skb, cmd_id);
330 	if (ret) {
331 		ath10k_warn(ar, "failed to transmit wmi command (testmode): %d\n",
332 			    ret);
333 		goto out;
334 	}
335 
336 	ret = 0;
337 
338 out:
339 	mutex_unlock(&ar->conf_mutex);
340 	return ret;
341 }
342 
343 int ath10k_tm_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
344 		  void *data, int len)
345 {
346 	struct ath10k *ar = hw->priv;
347 	struct nlattr *tb[ATH10K_TM_ATTR_MAX + 1];
348 	int ret;
349 
350 	ret = nla_parse(tb, ATH10K_TM_ATTR_MAX, data, len,
351 			ath10k_tm_policy);
352 	if (ret)
353 		return ret;
354 
355 	if (!tb[ATH10K_TM_ATTR_CMD])
356 		return -EINVAL;
357 
358 	switch (nla_get_u32(tb[ATH10K_TM_ATTR_CMD])) {
359 	case ATH10K_TM_CMD_GET_VERSION:
360 		return ath10k_tm_cmd_get_version(ar, tb);
361 	case ATH10K_TM_CMD_UTF_START:
362 		return ath10k_tm_cmd_utf_start(ar, tb);
363 	case ATH10K_TM_CMD_UTF_STOP:
364 		return ath10k_tm_cmd_utf_stop(ar, tb);
365 	case ATH10K_TM_CMD_WMI:
366 		return ath10k_tm_cmd_wmi(ar, tb);
367 	default:
368 		return -EOPNOTSUPP;
369 	}
370 }
371 
372 void ath10k_testmode_destroy(struct ath10k *ar)
373 {
374 	mutex_lock(&ar->conf_mutex);
375 
376 	if (ar->state != ATH10K_STATE_UTF) {
377 		/* utf firmware is not running, nothing to do */
378 		goto out;
379 	}
380 
381 	__ath10k_tm_cmd_utf_stop(ar);
382 
383 out:
384 	mutex_unlock(&ar->conf_mutex);
385 }
386