1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2020 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include "api/commands.h"
8 #include "debugfs.h"
9 #include "dbg.h"
10 #include <linux/seq_file.h>
11 
12 #define FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)		\
13 struct dbgfs_##name##_data {						\
14 	argtype *arg;							\
15 	bool read_done;							\
16 	ssize_t rlen;							\
17 	char rbuf[buflen];						\
18 };									\
19 static int _iwl_dbgfs_##name##_open(struct inode *inode,		\
20 				    struct file *file)			\
21 {									\
22 	struct dbgfs_##name##_data *data;				\
23 									\
24 	data = kzalloc(sizeof(*data), GFP_KERNEL);			\
25 	if (!data)							\
26 		return -ENOMEM;						\
27 									\
28 	data->read_done = false;					\
29 	data->arg = inode->i_private;					\
30 	file->private_data = data;					\
31 									\
32 	return 0;							\
33 }
34 
35 #define FWRT_DEBUGFS_READ_WRAPPER(name)					\
36 static ssize_t _iwl_dbgfs_##name##_read(struct file *file,		\
37 					char __user *user_buf,		\
38 					size_t count, loff_t *ppos)	\
39 {									\
40 	struct dbgfs_##name##_data *data = file->private_data;		\
41 									\
42 	if (!data->read_done) {						\
43 		data->read_done = true;					\
44 		data->rlen = iwl_dbgfs_##name##_read(data->arg,		\
45 						     sizeof(data->rbuf),\
46 						     data->rbuf);	\
47 	}								\
48 									\
49 	if (data->rlen < 0)						\
50 		return data->rlen;					\
51 	return simple_read_from_buffer(user_buf, count, ppos,		\
52 				       data->rbuf, data->rlen);		\
53 }
54 
_iwl_dbgfs_release(struct inode * inode,struct file * file)55 static int _iwl_dbgfs_release(struct inode *inode, struct file *file)
56 {
57 	kfree(file->private_data);
58 
59 	return 0;
60 }
61 
62 #define _FWRT_DEBUGFS_READ_FILE_OPS(name, buflen, argtype)		\
63 FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)			\
64 FWRT_DEBUGFS_READ_WRAPPER(name)						\
65 static const struct file_operations iwl_dbgfs_##name##_ops = {		\
66 	.read = _iwl_dbgfs_##name##_read,				\
67 	.open = _iwl_dbgfs_##name##_open,				\
68 	.llseek = generic_file_llseek,					\
69 	.release = _iwl_dbgfs_release,					\
70 }
71 
72 #define FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen, argtype)		\
73 static ssize_t _iwl_dbgfs_##name##_write(struct file *file,		\
74 					 const char __user *user_buf,	\
75 					 size_t count, loff_t *ppos)	\
76 {									\
77 	argtype *arg =							\
78 		((struct dbgfs_##name##_data *)file->private_data)->arg;\
79 	char buf[buflen] = {};						\
80 	size_t buf_size = min(count, sizeof(buf) -  1);			\
81 									\
82 	if (copy_from_user(buf, user_buf, buf_size))			\
83 		return -EFAULT;						\
84 									\
85 	return iwl_dbgfs_##name##_write(arg, buf, buf_size);		\
86 }
87 
88 #define _FWRT_DEBUGFS_READ_WRITE_FILE_OPS(name, buflen, argtype)	\
89 FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)			\
90 FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen, argtype)			\
91 FWRT_DEBUGFS_READ_WRAPPER(name)						\
92 static const struct file_operations iwl_dbgfs_##name##_ops = {		\
93 	.write = _iwl_dbgfs_##name##_write,				\
94 	.read = _iwl_dbgfs_##name##_read,				\
95 	.open = _iwl_dbgfs_##name##_open,				\
96 	.llseek = generic_file_llseek,					\
97 	.release = _iwl_dbgfs_release,					\
98 }
99 
100 #define _FWRT_DEBUGFS_WRITE_FILE_OPS(name, buflen, argtype)		\
101 FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)			\
102 FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen, argtype)			\
103 static const struct file_operations iwl_dbgfs_##name##_ops = {		\
104 	.write = _iwl_dbgfs_##name##_write,				\
105 	.open = _iwl_dbgfs_##name##_open,				\
106 	.llseek = generic_file_llseek,					\
107 	.release = _iwl_dbgfs_release,					\
108 }
109 
110 #define FWRT_DEBUGFS_READ_FILE_OPS(name, bufsz)				\
111 	_FWRT_DEBUGFS_READ_FILE_OPS(name, bufsz, struct iwl_fw_runtime)
112 
113 #define FWRT_DEBUGFS_WRITE_FILE_OPS(name, bufsz)			\
114 	_FWRT_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_fw_runtime)
115 
116 #define FWRT_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz)			\
117 	_FWRT_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_fw_runtime)
118 
119 #define FWRT_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {	\
120 	debugfs_create_file(alias, mode, parent, fwrt,			\
121 			    &iwl_dbgfs_##name##_ops);			\
122 	} while (0)
123 #define FWRT_DEBUGFS_ADD_FILE(name, parent, mode) \
124 	FWRT_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
125 
iwl_dbgfs_enabled_severities_write(struct iwl_fw_runtime * fwrt,char * buf,size_t count)126 static int iwl_dbgfs_enabled_severities_write(struct iwl_fw_runtime *fwrt,
127 					      char *buf, size_t count)
128 {
129 	struct iwl_dbg_host_event_cfg_cmd event_cfg;
130 	struct iwl_host_cmd hcmd = {
131 		.id = WIDE_ID(DEBUG_GROUP, HOST_EVENT_CFG),
132 		.flags = CMD_ASYNC,
133 		.data[0] = &event_cfg,
134 		.len[0] = sizeof(event_cfg),
135 	};
136 	u32 enabled_severities;
137 	int ret = kstrtou32(buf, 10, &enabled_severities);
138 
139 	if (ret < 0)
140 		return ret;
141 
142 	event_cfg.enabled_severities = cpu_to_le32(enabled_severities);
143 
144 	if (fwrt->ops && fwrt->ops->send_hcmd)
145 		ret = fwrt->ops->send_hcmd(fwrt->ops_ctx, &hcmd);
146 	else
147 		ret = -EPERM;
148 
149 	IWL_INFO(fwrt,
150 		 "sent host event cfg with enabled_severities: %u, ret: %d\n",
151 		 enabled_severities, ret);
152 
153 	return ret ?: count;
154 }
155 
156 FWRT_DEBUGFS_WRITE_FILE_OPS(enabled_severities, 16);
157 
iwl_fw_timestamp_marker_wk(struct work_struct * work)158 static void iwl_fw_timestamp_marker_wk(struct work_struct *work)
159 {
160 	int ret;
161 	struct iwl_fw_runtime *fwrt =
162 		container_of(work, struct iwl_fw_runtime, timestamp.wk.work);
163 	unsigned long delay = fwrt->timestamp.delay;
164 
165 	ret = iwl_fw_send_timestamp_marker_cmd(fwrt);
166 	if (!ret && delay)
167 		schedule_delayed_work(&fwrt->timestamp.wk,
168 				      round_jiffies_relative(delay));
169 	else
170 		IWL_INFO(fwrt,
171 			 "stopping timestamp_marker, ret: %d, delay: %u\n",
172 			 ret, jiffies_to_msecs(delay) / 1000);
173 }
174 
iwl_fw_trigger_timestamp(struct iwl_fw_runtime * fwrt,u32 delay)175 void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay)
176 {
177 	IWL_INFO(fwrt,
178 		 "starting timestamp_marker trigger with delay: %us\n",
179 		 delay);
180 
181 	iwl_fw_cancel_timestamp(fwrt);
182 
183 	fwrt->timestamp.delay = msecs_to_jiffies(delay * 1000);
184 
185 	schedule_delayed_work(&fwrt->timestamp.wk,
186 			      round_jiffies_relative(fwrt->timestamp.delay));
187 }
188 
iwl_dbgfs_timestamp_marker_write(struct iwl_fw_runtime * fwrt,char * buf,size_t count)189 static ssize_t iwl_dbgfs_timestamp_marker_write(struct iwl_fw_runtime *fwrt,
190 						char *buf, size_t count)
191 {
192 	int ret;
193 	u32 delay;
194 
195 	ret = kstrtou32(buf, 10, &delay);
196 	if (ret < 0)
197 		return ret;
198 
199 	iwl_fw_trigger_timestamp(fwrt, delay);
200 
201 	return count;
202 }
203 
iwl_dbgfs_timestamp_marker_read(struct iwl_fw_runtime * fwrt,size_t size,char * buf)204 static ssize_t iwl_dbgfs_timestamp_marker_read(struct iwl_fw_runtime *fwrt,
205 					       size_t size, char *buf)
206 {
207 	u32 delay_secs = jiffies_to_msecs(fwrt->timestamp.delay) / 1000;
208 
209 	return scnprintf(buf, size, "%d\n", delay_secs);
210 }
211 
212 FWRT_DEBUGFS_READ_WRITE_FILE_OPS(timestamp_marker, 16);
213 
214 struct hcmd_write_data {
215 	__be32 cmd_id;
216 	__be32 flags;
217 	__be16 length;
218 	u8 data[];
219 } __packed;
220 
iwl_dbgfs_send_hcmd_write(struct iwl_fw_runtime * fwrt,char * buf,size_t count)221 static ssize_t iwl_dbgfs_send_hcmd_write(struct iwl_fw_runtime *fwrt, char *buf,
222 					 size_t count)
223 {
224 	size_t header_size = (sizeof(u32) * 2 + sizeof(u16)) * 2;
225 	size_t data_size = (count - 1) / 2;
226 	int ret;
227 	struct hcmd_write_data *data;
228 	struct iwl_host_cmd hcmd = {
229 		.len = { 0, },
230 		.data = { NULL, },
231 	};
232 
233 	if (!iwl_trans_fw_running(fwrt->trans))
234 		return -EIO;
235 
236 	if (count < header_size + 1 || count > 1024 * 4)
237 		return -EINVAL;
238 
239 	data = kmalloc(data_size, GFP_KERNEL);
240 	if (!data)
241 		return -ENOMEM;
242 
243 	ret = hex2bin((u8 *)data, buf, data_size);
244 	if (ret)
245 		goto out;
246 
247 	hcmd.id = be32_to_cpu(data->cmd_id);
248 	hcmd.flags = be32_to_cpu(data->flags);
249 	hcmd.len[0] = be16_to_cpu(data->length);
250 	hcmd.data[0] = data->data;
251 
252 	if (count != header_size + hcmd.len[0] * 2 + 1) {
253 		IWL_ERR(fwrt,
254 			"host command data size does not match header length\n");
255 		ret = -EINVAL;
256 		goto out;
257 	}
258 
259 	if (fwrt->ops && fwrt->ops->send_hcmd)
260 		ret = fwrt->ops->send_hcmd(fwrt->ops_ctx, &hcmd);
261 	else
262 		ret = -EPERM;
263 
264 	if (ret < 0)
265 		goto out;
266 
267 	if (hcmd.flags & CMD_WANT_SKB)
268 		iwl_free_resp(&hcmd);
269 out:
270 	kfree(data);
271 	return ret ?: count;
272 }
273 
274 FWRT_DEBUGFS_WRITE_FILE_OPS(send_hcmd, 512);
275 
iwl_dbgfs_fw_dbg_domain_read(struct iwl_fw_runtime * fwrt,size_t size,char * buf)276 static ssize_t iwl_dbgfs_fw_dbg_domain_read(struct iwl_fw_runtime *fwrt,
277 					    size_t size, char *buf)
278 {
279 	return scnprintf(buf, size, "0x%08x\n",
280 			 fwrt->trans->dbg.domains_bitmap);
281 }
282 
283 FWRT_DEBUGFS_READ_FILE_OPS(fw_dbg_domain, 20);
284 
285 struct iwl_dbgfs_fw_info_priv {
286 	struct iwl_fw_runtime *fwrt;
287 };
288 
289 struct iwl_dbgfs_fw_info_state {
290 	loff_t pos;
291 };
292 
iwl_dbgfs_fw_info_seq_next(struct seq_file * seq,void * v,loff_t * pos)293 static void *iwl_dbgfs_fw_info_seq_next(struct seq_file *seq,
294 					void *v, loff_t *pos)
295 {
296 	struct iwl_dbgfs_fw_info_state *state = v;
297 	struct iwl_dbgfs_fw_info_priv *priv = seq->private;
298 	const struct iwl_fw *fw = priv->fwrt->fw;
299 
300 	*pos = ++state->pos;
301 	if (*pos >= fw->ucode_capa.n_cmd_versions) {
302 		kfree(state);
303 		return NULL;
304 	}
305 
306 	return state;
307 }
308 
iwl_dbgfs_fw_info_seq_stop(struct seq_file * seq,void * v)309 static void iwl_dbgfs_fw_info_seq_stop(struct seq_file *seq,
310 				       void *v)
311 {
312 	kfree(v);
313 }
314 
iwl_dbgfs_fw_info_seq_start(struct seq_file * seq,loff_t * pos)315 static void *iwl_dbgfs_fw_info_seq_start(struct seq_file *seq, loff_t *pos)
316 {
317 	struct iwl_dbgfs_fw_info_priv *priv = seq->private;
318 	const struct iwl_fw *fw = priv->fwrt->fw;
319 	struct iwl_dbgfs_fw_info_state *state;
320 
321 	if (*pos >= fw->ucode_capa.n_cmd_versions)
322 		return NULL;
323 
324 	state = kzalloc(sizeof(*state), GFP_KERNEL);
325 	if (!state)
326 		return NULL;
327 	state->pos = *pos;
328 	return state;
329 };
330 
iwl_dbgfs_fw_info_seq_show(struct seq_file * seq,void * v)331 static int iwl_dbgfs_fw_info_seq_show(struct seq_file *seq, void *v)
332 {
333 	struct iwl_dbgfs_fw_info_state *state = v;
334 	struct iwl_dbgfs_fw_info_priv *priv = seq->private;
335 	const struct iwl_fw *fw = priv->fwrt->fw;
336 	const struct iwl_fw_cmd_version *ver;
337 	u32 cmd_id;
338 	int has_capa;
339 
340 	if (!state->pos) {
341 		seq_puts(seq, "fw_capa:\n");
342 		has_capa = fw_has_capa(&fw->ucode_capa,
343 				       IWL_UCODE_TLV_CAPA_PPAG_CHINA_BIOS_SUPPORT) ? 1 : 0;
344 		seq_printf(seq,
345 			   "    %d: %d\n",
346 			   IWL_UCODE_TLV_CAPA_PPAG_CHINA_BIOS_SUPPORT,
347 			   has_capa);
348 		seq_puts(seq, "fw_api_ver:\n");
349 	}
350 
351 	ver = &fw->ucode_capa.cmd_versions[state->pos];
352 
353 	cmd_id = WIDE_ID(ver->group, ver->cmd);
354 
355 	seq_printf(seq, "  0x%04x:\n", cmd_id);
356 	seq_printf(seq, "    name: %s\n",
357 		   iwl_get_cmd_string(priv->fwrt->trans, cmd_id));
358 	seq_printf(seq, "    cmd_ver: %d\n", ver->cmd_ver);
359 	seq_printf(seq, "    notif_ver: %d\n", ver->notif_ver);
360 	return 0;
361 }
362 
363 static const struct seq_operations iwl_dbgfs_info_seq_ops = {
364 	.start = iwl_dbgfs_fw_info_seq_start,
365 	.next = iwl_dbgfs_fw_info_seq_next,
366 	.stop = iwl_dbgfs_fw_info_seq_stop,
367 	.show = iwl_dbgfs_fw_info_seq_show,
368 };
369 
iwl_dbgfs_fw_info_open(struct inode * inode,struct file * filp)370 static int iwl_dbgfs_fw_info_open(struct inode *inode, struct file *filp)
371 {
372 	struct iwl_dbgfs_fw_info_priv *priv;
373 
374 	priv = __seq_open_private(filp, &iwl_dbgfs_info_seq_ops,
375 				  sizeof(*priv));
376 
377 	if (!priv)
378 		return -ENOMEM;
379 
380 	priv->fwrt = inode->i_private;
381 	return 0;
382 }
383 
384 static const struct file_operations iwl_dbgfs_fw_info_ops = {
385 	.owner = THIS_MODULE,
386 	.open = iwl_dbgfs_fw_info_open,
387 	.read = seq_read,
388 	.llseek = seq_lseek,
389 	.release = seq_release_private,
390 };
391 
iwl_fwrt_dbgfs_register(struct iwl_fw_runtime * fwrt,struct dentry * dbgfs_dir)392 void iwl_fwrt_dbgfs_register(struct iwl_fw_runtime *fwrt,
393 			    struct dentry *dbgfs_dir)
394 {
395 	INIT_DELAYED_WORK(&fwrt->timestamp.wk, iwl_fw_timestamp_marker_wk);
396 	FWRT_DEBUGFS_ADD_FILE(timestamp_marker, dbgfs_dir, 0200);
397 	FWRT_DEBUGFS_ADD_FILE(fw_info, dbgfs_dir, 0200);
398 	FWRT_DEBUGFS_ADD_FILE(send_hcmd, dbgfs_dir, 0200);
399 	FWRT_DEBUGFS_ADD_FILE(enabled_severities, dbgfs_dir, 0200);
400 	FWRT_DEBUGFS_ADD_FILE(fw_dbg_domain, dbgfs_dir, 0400);
401 }
402