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