18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
38e99ea8dSJohannes Berg  * Copyright (C) 2012-2014, 2018-2020 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
793b167c1SMordechay Goodstein #include "api/commands.h"
893b167c1SMordechay Goodstein #include "debugfs.h"
9422b5dd4SHaim Dreyfuss #include "dbg.h"
1036dfe9acSMordechay Goodstein #include <linux/seq_file.h>
1193b167c1SMordechay Goodstein 
12dd269057SNaftali Goldstein #define FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)		\
13dd269057SNaftali Goldstein struct dbgfs_##name##_data {						\
14dd269057SNaftali Goldstein 	argtype *arg;							\
15dd269057SNaftali Goldstein 	bool read_done;							\
16dd269057SNaftali Goldstein 	ssize_t rlen;							\
17dd269057SNaftali Goldstein 	char rbuf[buflen];						\
18dd269057SNaftali Goldstein };									\
19dd269057SNaftali Goldstein static int _iwl_dbgfs_##name##_open(struct inode *inode,		\
20dd269057SNaftali Goldstein 				    struct file *file)			\
21dd269057SNaftali Goldstein {									\
22dd269057SNaftali Goldstein 	struct dbgfs_##name##_data *data;				\
23dd269057SNaftali Goldstein 									\
24dd269057SNaftali Goldstein 	data = kzalloc(sizeof(*data), GFP_KERNEL);			\
25dd269057SNaftali Goldstein 	if (!data)							\
26dd269057SNaftali Goldstein 		return -ENOMEM;						\
27dd269057SNaftali Goldstein 									\
28dd269057SNaftali Goldstein 	data->read_done = false;					\
29dd269057SNaftali Goldstein 	data->arg = inode->i_private;					\
30dd269057SNaftali Goldstein 	file->private_data = data;					\
31dd269057SNaftali Goldstein 									\
32dd269057SNaftali Goldstein 	return 0;							\
3393b167c1SMordechay Goodstein }
3493b167c1SMordechay Goodstein 
35dd269057SNaftali Goldstein #define FWRT_DEBUGFS_READ_WRAPPER(name)					\
36dd269057SNaftali Goldstein static ssize_t _iwl_dbgfs_##name##_read(struct file *file,		\
37dd269057SNaftali Goldstein 					char __user *user_buf,		\
38dd269057SNaftali Goldstein 					size_t count, loff_t *ppos)	\
39dd269057SNaftali Goldstein {									\
40dd269057SNaftali Goldstein 	struct dbgfs_##name##_data *data = file->private_data;		\
41dd269057SNaftali Goldstein 									\
42dd269057SNaftali Goldstein 	if (!data->read_done) {						\
43dd269057SNaftali Goldstein 		data->read_done = true;					\
44dd269057SNaftali Goldstein 		data->rlen = iwl_dbgfs_##name##_read(data->arg,		\
45dd269057SNaftali Goldstein 						     sizeof(data->rbuf),\
46dd269057SNaftali Goldstein 						     data->rbuf);	\
47dd269057SNaftali Goldstein 	}								\
48dd269057SNaftali Goldstein 									\
49dd269057SNaftali Goldstein 	if (data->rlen < 0)						\
50dd269057SNaftali Goldstein 		return data->rlen;					\
51dd269057SNaftali Goldstein 	return simple_read_from_buffer(user_buf, count, ppos,		\
52dd269057SNaftali Goldstein 				       data->rbuf, data->rlen);		\
53dd269057SNaftali Goldstein }
54dd269057SNaftali Goldstein 
_iwl_dbgfs_release(struct inode * inode,struct file * file)55dd269057SNaftali Goldstein static int _iwl_dbgfs_release(struct inode *inode, struct file *file)
56dd269057SNaftali Goldstein {
57dd269057SNaftali Goldstein 	kfree(file->private_data);
58dd269057SNaftali Goldstein 
59dd269057SNaftali Goldstein 	return 0;
60dd269057SNaftali Goldstein }
61dd269057SNaftali Goldstein 
62dd269057SNaftali Goldstein #define _FWRT_DEBUGFS_READ_FILE_OPS(name, buflen, argtype)		\
63dd269057SNaftali Goldstein FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)			\
64dd269057SNaftali Goldstein FWRT_DEBUGFS_READ_WRAPPER(name)						\
65dd269057SNaftali Goldstein static const struct file_operations iwl_dbgfs_##name##_ops = {		\
66dd269057SNaftali Goldstein 	.read = _iwl_dbgfs_##name##_read,				\
67dd269057SNaftali Goldstein 	.open = _iwl_dbgfs_##name##_open,				\
68dd269057SNaftali Goldstein 	.llseek = generic_file_llseek,					\
69dd269057SNaftali Goldstein 	.release = _iwl_dbgfs_release,					\
70dd269057SNaftali Goldstein }
71dd269057SNaftali Goldstein 
72dd269057SNaftali Goldstein #define FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen, argtype)		\
7393b167c1SMordechay Goodstein static ssize_t _iwl_dbgfs_##name##_write(struct file *file,		\
7493b167c1SMordechay Goodstein 					 const char __user *user_buf,	\
7593b167c1SMordechay Goodstein 					 size_t count, loff_t *ppos)	\
7693b167c1SMordechay Goodstein {									\
77dd269057SNaftali Goldstein 	argtype *arg =							\
78dd269057SNaftali Goldstein 		((struct dbgfs_##name##_data *)file->private_data)->arg;\
7993b167c1SMordechay Goodstein 	char buf[buflen] = {};						\
8093b167c1SMordechay Goodstein 	size_t buf_size = min(count, sizeof(buf) -  1);			\
8193b167c1SMordechay Goodstein 									\
8293b167c1SMordechay Goodstein 	if (copy_from_user(buf, user_buf, buf_size))			\
8393b167c1SMordechay Goodstein 		return -EFAULT;						\
8493b167c1SMordechay Goodstein 									\
85dd269057SNaftali Goldstein 	return iwl_dbgfs_##name##_write(arg, buf, buf_size);		\
8693b167c1SMordechay Goodstein }
8793b167c1SMordechay Goodstein 
88dd269057SNaftali Goldstein #define _FWRT_DEBUGFS_READ_WRITE_FILE_OPS(name, buflen, argtype)	\
89dd269057SNaftali Goldstein FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)			\
90dd269057SNaftali Goldstein FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen, argtype)			\
91dd269057SNaftali Goldstein FWRT_DEBUGFS_READ_WRAPPER(name)						\
9293b167c1SMordechay Goodstein static const struct file_operations iwl_dbgfs_##name##_ops = {		\
9393b167c1SMordechay Goodstein 	.write = _iwl_dbgfs_##name##_write,				\
94dd269057SNaftali Goldstein 	.read = _iwl_dbgfs_##name##_read,				\
95dd269057SNaftali Goldstein 	.open = _iwl_dbgfs_##name##_open,				\
9693b167c1SMordechay Goodstein 	.llseek = generic_file_llseek,					\
97dd269057SNaftali Goldstein 	.release = _iwl_dbgfs_release,					\
9893b167c1SMordechay Goodstein }
9993b167c1SMordechay Goodstein 
100dd269057SNaftali Goldstein #define _FWRT_DEBUGFS_WRITE_FILE_OPS(name, buflen, argtype)		\
101dd269057SNaftali Goldstein FWRT_DEBUGFS_OPEN_WRAPPER(name, buflen, argtype)			\
102dd269057SNaftali Goldstein FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen, argtype)			\
10393b167c1SMordechay Goodstein static const struct file_operations iwl_dbgfs_##name##_ops = {		\
10493b167c1SMordechay Goodstein 	.write = _iwl_dbgfs_##name##_write,				\
105dd269057SNaftali Goldstein 	.open = _iwl_dbgfs_##name##_open,				\
10693b167c1SMordechay Goodstein 	.llseek = generic_file_llseek,					\
107dd269057SNaftali Goldstein 	.release = _iwl_dbgfs_release,					\
10893b167c1SMordechay Goodstein }
10993b167c1SMordechay Goodstein 
110dd269057SNaftali Goldstein #define FWRT_DEBUGFS_READ_FILE_OPS(name, bufsz)				\
111dd269057SNaftali Goldstein 	_FWRT_DEBUGFS_READ_FILE_OPS(name, bufsz, struct iwl_fw_runtime)
112dd269057SNaftali Goldstein 
113dd269057SNaftali Goldstein #define FWRT_DEBUGFS_WRITE_FILE_OPS(name, bufsz)			\
114dd269057SNaftali Goldstein 	_FWRT_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_fw_runtime)
115dd269057SNaftali Goldstein 
116dd269057SNaftali Goldstein #define FWRT_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz)			\
117dd269057SNaftali Goldstein 	_FWRT_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_fw_runtime)
118dd269057SNaftali Goldstein 
11993b167c1SMordechay Goodstein #define FWRT_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {	\
1205987dfdeSGreg Kroah-Hartman 	debugfs_create_file(alias, mode, parent, fwrt,			\
1215987dfdeSGreg Kroah-Hartman 			    &iwl_dbgfs_##name##_ops);			\
12293b167c1SMordechay Goodstein 	} while (0)
12393b167c1SMordechay Goodstein #define FWRT_DEBUGFS_ADD_FILE(name, parent, mode) \
12493b167c1SMordechay Goodstein 	FWRT_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
12593b167c1SMordechay Goodstein 
iwl_dbgfs_enabled_severities_write(struct iwl_fw_runtime * fwrt,char * buf,size_t count)12687f1283bSMordechay Goodstein static int iwl_dbgfs_enabled_severities_write(struct iwl_fw_runtime *fwrt,
12787f1283bSMordechay Goodstein 					      char *buf, size_t count)
12887f1283bSMordechay Goodstein {
12987f1283bSMordechay Goodstein 	struct iwl_dbg_host_event_cfg_cmd event_cfg;
13087f1283bSMordechay Goodstein 	struct iwl_host_cmd hcmd = {
131f0c86427SJohannes Berg 		.id = WIDE_ID(DEBUG_GROUP, HOST_EVENT_CFG),
13287f1283bSMordechay Goodstein 		.flags = CMD_ASYNC,
13387f1283bSMordechay Goodstein 		.data[0] = &event_cfg,
13487f1283bSMordechay Goodstein 		.len[0] = sizeof(event_cfg),
13587f1283bSMordechay Goodstein 	};
13687f1283bSMordechay Goodstein 	u32 enabled_severities;
13787f1283bSMordechay Goodstein 	int ret = kstrtou32(buf, 10, &enabled_severities);
13887f1283bSMordechay Goodstein 
13987f1283bSMordechay Goodstein 	if (ret < 0)
14087f1283bSMordechay Goodstein 		return ret;
14187f1283bSMordechay Goodstein 
14287f1283bSMordechay Goodstein 	event_cfg.enabled_severities = cpu_to_le32(enabled_severities);
14387f1283bSMordechay Goodstein 
14487f1283bSMordechay Goodstein 	ret = iwl_trans_send_cmd(fwrt->trans, &hcmd);
14587f1283bSMordechay Goodstein 	IWL_INFO(fwrt,
14687f1283bSMordechay Goodstein 		 "sent host event cfg with enabled_severities: %u, ret: %d\n",
14787f1283bSMordechay Goodstein 		 enabled_severities, ret);
14887f1283bSMordechay Goodstein 
14987f1283bSMordechay Goodstein 	return ret ?: count;
15087f1283bSMordechay Goodstein }
15187f1283bSMordechay Goodstein 
15287f1283bSMordechay Goodstein FWRT_DEBUGFS_WRITE_FILE_OPS(enabled_severities, 16);
15387f1283bSMordechay Goodstein 
iwl_fw_timestamp_marker_wk(struct work_struct * work)15493b167c1SMordechay Goodstein static void iwl_fw_timestamp_marker_wk(struct work_struct *work)
15593b167c1SMordechay Goodstein {
15693b167c1SMordechay Goodstein 	int ret;
15793b167c1SMordechay Goodstein 	struct iwl_fw_runtime *fwrt =
15893b167c1SMordechay Goodstein 		container_of(work, struct iwl_fw_runtime, timestamp.wk.work);
15993b167c1SMordechay Goodstein 	unsigned long delay = fwrt->timestamp.delay;
16093b167c1SMordechay Goodstein 
16193b167c1SMordechay Goodstein 	ret = iwl_fw_send_timestamp_marker_cmd(fwrt);
16293b167c1SMordechay Goodstein 	if (!ret && delay)
16393b167c1SMordechay Goodstein 		schedule_delayed_work(&fwrt->timestamp.wk,
16493b167c1SMordechay Goodstein 				      round_jiffies_relative(delay));
16593b167c1SMordechay Goodstein 	else
16693b167c1SMordechay Goodstein 		IWL_INFO(fwrt,
16793b167c1SMordechay Goodstein 			 "stopping timestamp_marker, ret: %d, delay: %u\n",
16893b167c1SMordechay Goodstein 			 ret, jiffies_to_msecs(delay) / 1000);
16993b167c1SMordechay Goodstein }
17093b167c1SMordechay Goodstein 
iwl_fw_trigger_timestamp(struct iwl_fw_runtime * fwrt,u32 delay)171759931c7SMordechay Goodstein void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay)
172759931c7SMordechay Goodstein {
173759931c7SMordechay Goodstein 	IWL_INFO(fwrt,
174759931c7SMordechay Goodstein 		 "starting timestamp_marker trigger with delay: %us\n",
175759931c7SMordechay Goodstein 		 delay);
176759931c7SMordechay Goodstein 
177759931c7SMordechay Goodstein 	iwl_fw_cancel_timestamp(fwrt);
178759931c7SMordechay Goodstein 
179759931c7SMordechay Goodstein 	fwrt->timestamp.delay = msecs_to_jiffies(delay * 1000);
180759931c7SMordechay Goodstein 
181759931c7SMordechay Goodstein 	schedule_delayed_work(&fwrt->timestamp.wk,
182759931c7SMordechay Goodstein 			      round_jiffies_relative(fwrt->timestamp.delay));
183759931c7SMordechay Goodstein }
184759931c7SMordechay Goodstein 
iwl_dbgfs_timestamp_marker_write(struct iwl_fw_runtime * fwrt,char * buf,size_t count)18593b167c1SMordechay Goodstein static ssize_t iwl_dbgfs_timestamp_marker_write(struct iwl_fw_runtime *fwrt,
186dd269057SNaftali Goldstein 						char *buf, size_t count)
18793b167c1SMordechay Goodstein {
18893b167c1SMordechay Goodstein 	int ret;
18993b167c1SMordechay Goodstein 	u32 delay;
19093b167c1SMordechay Goodstein 
19193b167c1SMordechay Goodstein 	ret = kstrtou32(buf, 10, &delay);
19293b167c1SMordechay Goodstein 	if (ret < 0)
19393b167c1SMordechay Goodstein 		return ret;
19493b167c1SMordechay Goodstein 
195759931c7SMordechay Goodstein 	iwl_fw_trigger_timestamp(fwrt, delay);
19693b167c1SMordechay Goodstein 
19793b167c1SMordechay Goodstein 	return count;
19893b167c1SMordechay Goodstein }
19993b167c1SMordechay Goodstein 
iwl_dbgfs_timestamp_marker_read(struct iwl_fw_runtime * fwrt,size_t size,char * buf)20075e9947eSMordechay Goodstein static ssize_t iwl_dbgfs_timestamp_marker_read(struct iwl_fw_runtime *fwrt,
20175e9947eSMordechay Goodstein 					       size_t size, char *buf)
20275e9947eSMordechay Goodstein {
20375e9947eSMordechay Goodstein 	u32 delay_secs = jiffies_to_msecs(fwrt->timestamp.delay) / 1000;
20475e9947eSMordechay Goodstein 
20575e9947eSMordechay Goodstein 	return scnprintf(buf, size, "%d\n", delay_secs);
20675e9947eSMordechay Goodstein }
20775e9947eSMordechay Goodstein 
20875e9947eSMordechay Goodstein FWRT_DEBUGFS_READ_WRITE_FILE_OPS(timestamp_marker, 16);
20993b167c1SMordechay Goodstein 
210755384b3SShahar S Matityahu struct hcmd_write_data {
211755384b3SShahar S Matityahu 	__be32 cmd_id;
212755384b3SShahar S Matityahu 	__be32 flags;
213755384b3SShahar S Matityahu 	__be16 length;
21445c21a0eSGustavo A. R. Silva 	u8 data[];
215755384b3SShahar S Matityahu } __packed;
216755384b3SShahar S Matityahu 
iwl_dbgfs_send_hcmd_write(struct iwl_fw_runtime * fwrt,char * buf,size_t count)217755384b3SShahar S Matityahu static ssize_t iwl_dbgfs_send_hcmd_write(struct iwl_fw_runtime *fwrt, char *buf,
218755384b3SShahar S Matityahu 					 size_t count)
219755384b3SShahar S Matityahu {
220755384b3SShahar S Matityahu 	size_t header_size = (sizeof(u32) * 2 + sizeof(u16)) * 2;
221755384b3SShahar S Matityahu 	size_t data_size = (count - 1) / 2;
222755384b3SShahar S Matityahu 	int ret;
223755384b3SShahar S Matityahu 	struct hcmd_write_data *data;
224755384b3SShahar S Matityahu 	struct iwl_host_cmd hcmd = {
225755384b3SShahar S Matityahu 		.len = { 0, },
226755384b3SShahar S Matityahu 		.data = { NULL, },
227755384b3SShahar S Matityahu 	};
228755384b3SShahar S Matityahu 
229755384b3SShahar S Matityahu 	if (fwrt->ops && fwrt->ops->fw_running &&
230755384b3SShahar S Matityahu 	    !fwrt->ops->fw_running(fwrt->ops_ctx))
231755384b3SShahar S Matityahu 		return -EIO;
232755384b3SShahar S Matityahu 
233755384b3SShahar S Matityahu 	if (count < header_size + 1 || count > 1024 * 4)
234755384b3SShahar S Matityahu 		return -EINVAL;
235755384b3SShahar S Matityahu 
236755384b3SShahar S Matityahu 	data = kmalloc(data_size, GFP_KERNEL);
237755384b3SShahar S Matityahu 	if (!data)
238755384b3SShahar S Matityahu 		return -ENOMEM;
239755384b3SShahar S Matityahu 
240755384b3SShahar S Matityahu 	ret = hex2bin((u8 *)data, buf, data_size);
241755384b3SShahar S Matityahu 	if (ret)
242755384b3SShahar S Matityahu 		goto out;
243755384b3SShahar S Matityahu 
244755384b3SShahar S Matityahu 	hcmd.id = be32_to_cpu(data->cmd_id);
245755384b3SShahar S Matityahu 	hcmd.flags = be32_to_cpu(data->flags);
246755384b3SShahar S Matityahu 	hcmd.len[0] = be16_to_cpu(data->length);
247755384b3SShahar S Matityahu 	hcmd.data[0] = data->data;
248755384b3SShahar S Matityahu 
249755384b3SShahar S Matityahu 	if (count != header_size + hcmd.len[0] * 2 + 1) {
250755384b3SShahar S Matityahu 		IWL_ERR(fwrt,
251755384b3SShahar S Matityahu 			"host command data size does not match header length\n");
252755384b3SShahar S Matityahu 		ret = -EINVAL;
253755384b3SShahar S Matityahu 		goto out;
254755384b3SShahar S Matityahu 	}
255755384b3SShahar S Matityahu 
256755384b3SShahar S Matityahu 	if (fwrt->ops && fwrt->ops->send_hcmd)
257755384b3SShahar S Matityahu 		ret = fwrt->ops->send_hcmd(fwrt->ops_ctx, &hcmd);
258755384b3SShahar S Matityahu 	else
259755384b3SShahar S Matityahu 		ret = -EPERM;
260755384b3SShahar S Matityahu 
261755384b3SShahar S Matityahu 	if (ret < 0)
262755384b3SShahar S Matityahu 		goto out;
263755384b3SShahar S Matityahu 
264755384b3SShahar S Matityahu 	if (hcmd.flags & CMD_WANT_SKB)
265755384b3SShahar S Matityahu 		iwl_free_resp(&hcmd);
266755384b3SShahar S Matityahu out:
267755384b3SShahar S Matityahu 	kfree(data);
268755384b3SShahar S Matityahu 	return ret ?: count;
269755384b3SShahar S Matityahu }
270755384b3SShahar S Matityahu 
271755384b3SShahar S Matityahu FWRT_DEBUGFS_WRITE_FILE_OPS(send_hcmd, 512);
272755384b3SShahar S Matityahu 
iwl_dbgfs_fw_dbg_domain_read(struct iwl_fw_runtime * fwrt,size_t size,char * buf)273068893b7SShahar S Matityahu static ssize_t iwl_dbgfs_fw_dbg_domain_read(struct iwl_fw_runtime *fwrt,
274068893b7SShahar S Matityahu 					    size_t size, char *buf)
275068893b7SShahar S Matityahu {
276068893b7SShahar S Matityahu 	return scnprintf(buf, size, "0x%08x\n",
277068893b7SShahar S Matityahu 			 fwrt->trans->dbg.domains_bitmap);
278068893b7SShahar S Matityahu }
279068893b7SShahar S Matityahu 
28010137f07SLuca Coelho FWRT_DEBUGFS_READ_FILE_OPS(fw_dbg_domain, 20);
281068893b7SShahar S Matityahu 
28236dfe9acSMordechay Goodstein struct iwl_dbgfs_fw_info_priv {
28336dfe9acSMordechay Goodstein 	struct iwl_fw_runtime *fwrt;
28436dfe9acSMordechay Goodstein };
28536dfe9acSMordechay Goodstein 
28636dfe9acSMordechay Goodstein struct iwl_dbgfs_fw_info_state {
28736dfe9acSMordechay Goodstein 	loff_t pos;
28836dfe9acSMordechay Goodstein };
28936dfe9acSMordechay Goodstein 
iwl_dbgfs_fw_info_seq_next(struct seq_file * seq,void * v,loff_t * pos)29036dfe9acSMordechay Goodstein static void *iwl_dbgfs_fw_info_seq_next(struct seq_file *seq,
29136dfe9acSMordechay Goodstein 					void *v, loff_t *pos)
29236dfe9acSMordechay Goodstein {
29336dfe9acSMordechay Goodstein 	struct iwl_dbgfs_fw_info_state *state = v;
29436dfe9acSMordechay Goodstein 	struct iwl_dbgfs_fw_info_priv *priv = seq->private;
29536dfe9acSMordechay Goodstein 	const struct iwl_fw *fw = priv->fwrt->fw;
29636dfe9acSMordechay Goodstein 
29736dfe9acSMordechay Goodstein 	*pos = ++state->pos;
2983d90d2f4SJohannes Berg 	if (*pos >= fw->ucode_capa.n_cmd_versions) {
2993d90d2f4SJohannes Berg 		kfree(state);
30036dfe9acSMordechay Goodstein 		return NULL;
3013d90d2f4SJohannes Berg 	}
30236dfe9acSMordechay Goodstein 
30336dfe9acSMordechay Goodstein 	return state;
30436dfe9acSMordechay Goodstein }
30536dfe9acSMordechay Goodstein 
iwl_dbgfs_fw_info_seq_stop(struct seq_file * seq,void * v)30636dfe9acSMordechay Goodstein static void iwl_dbgfs_fw_info_seq_stop(struct seq_file *seq,
30736dfe9acSMordechay Goodstein 				       void *v)
30836dfe9acSMordechay Goodstein {
30936dfe9acSMordechay Goodstein 	kfree(v);
31036dfe9acSMordechay Goodstein }
31136dfe9acSMordechay Goodstein 
iwl_dbgfs_fw_info_seq_start(struct seq_file * seq,loff_t * pos)31236dfe9acSMordechay Goodstein static void *iwl_dbgfs_fw_info_seq_start(struct seq_file *seq, loff_t *pos)
31336dfe9acSMordechay Goodstein {
31436dfe9acSMordechay Goodstein 	struct iwl_dbgfs_fw_info_priv *priv = seq->private;
31536dfe9acSMordechay Goodstein 	const struct iwl_fw *fw = priv->fwrt->fw;
31636dfe9acSMordechay Goodstein 	struct iwl_dbgfs_fw_info_state *state;
31736dfe9acSMordechay Goodstein 
31836dfe9acSMordechay Goodstein 	if (*pos >= fw->ucode_capa.n_cmd_versions)
31936dfe9acSMordechay Goodstein 		return NULL;
32036dfe9acSMordechay Goodstein 
32136dfe9acSMordechay Goodstein 	state = kzalloc(sizeof(*state), GFP_KERNEL);
32236dfe9acSMordechay Goodstein 	if (!state)
32336dfe9acSMordechay Goodstein 		return NULL;
32436dfe9acSMordechay Goodstein 	state->pos = *pos;
32536dfe9acSMordechay Goodstein 	return state;
32636dfe9acSMordechay Goodstein };
32736dfe9acSMordechay Goodstein 
iwl_dbgfs_fw_info_seq_show(struct seq_file * seq,void * v)32836dfe9acSMordechay Goodstein static int iwl_dbgfs_fw_info_seq_show(struct seq_file *seq, void *v)
32936dfe9acSMordechay Goodstein {
33036dfe9acSMordechay Goodstein 	struct iwl_dbgfs_fw_info_state *state = v;
33136dfe9acSMordechay Goodstein 	struct iwl_dbgfs_fw_info_priv *priv = seq->private;
33236dfe9acSMordechay Goodstein 	const struct iwl_fw *fw = priv->fwrt->fw;
33336dfe9acSMordechay Goodstein 	const struct iwl_fw_cmd_version *ver;
33436dfe9acSMordechay Goodstein 	u32 cmd_id;
335*a114c4f5SAlon Giladi 	int has_capa;
33636dfe9acSMordechay Goodstein 
337*a114c4f5SAlon Giladi 	if (!state->pos) {
338*a114c4f5SAlon Giladi 		seq_puts(seq, "fw_capa:\n");
339*a114c4f5SAlon Giladi 		has_capa = fw_has_capa(&fw->ucode_capa,
340*a114c4f5SAlon Giladi 				       IWL_UCODE_TLV_CAPA_PPAG_CHINA_BIOS_SUPPORT) ? 1 : 0;
341*a114c4f5SAlon Giladi 		seq_printf(seq,
342*a114c4f5SAlon Giladi 			   "    %d: %d\n",
343*a114c4f5SAlon Giladi 			   IWL_UCODE_TLV_CAPA_PPAG_CHINA_BIOS_SUPPORT,
344*a114c4f5SAlon Giladi 			   has_capa);
34536dfe9acSMordechay Goodstein 		seq_puts(seq, "fw_api_ver:\n");
346*a114c4f5SAlon Giladi 	}
34736dfe9acSMordechay Goodstein 
34836dfe9acSMordechay Goodstein 	ver = &fw->ucode_capa.cmd_versions[state->pos];
34936dfe9acSMordechay Goodstein 
350f0c86427SJohannes Berg 	cmd_id = WIDE_ID(ver->group, ver->cmd);
35136dfe9acSMordechay Goodstein 
35236dfe9acSMordechay Goodstein 	seq_printf(seq, "  0x%04x:\n", cmd_id);
35336dfe9acSMordechay Goodstein 	seq_printf(seq, "    name: %s\n",
35436dfe9acSMordechay Goodstein 		   iwl_get_cmd_string(priv->fwrt->trans, cmd_id));
35536dfe9acSMordechay Goodstein 	seq_printf(seq, "    cmd_ver: %d\n", ver->cmd_ver);
35636dfe9acSMordechay Goodstein 	seq_printf(seq, "    notif_ver: %d\n", ver->notif_ver);
35736dfe9acSMordechay Goodstein 	return 0;
35836dfe9acSMordechay Goodstein }
35936dfe9acSMordechay Goodstein 
36036dfe9acSMordechay Goodstein static const struct seq_operations iwl_dbgfs_info_seq_ops = {
36136dfe9acSMordechay Goodstein 	.start = iwl_dbgfs_fw_info_seq_start,
36236dfe9acSMordechay Goodstein 	.next = iwl_dbgfs_fw_info_seq_next,
36336dfe9acSMordechay Goodstein 	.stop = iwl_dbgfs_fw_info_seq_stop,
36436dfe9acSMordechay Goodstein 	.show = iwl_dbgfs_fw_info_seq_show,
36536dfe9acSMordechay Goodstein };
36636dfe9acSMordechay Goodstein 
iwl_dbgfs_fw_info_open(struct inode * inode,struct file * filp)36736dfe9acSMordechay Goodstein static int iwl_dbgfs_fw_info_open(struct inode *inode, struct file *filp)
36836dfe9acSMordechay Goodstein {
36936dfe9acSMordechay Goodstein 	struct iwl_dbgfs_fw_info_priv *priv;
37036dfe9acSMordechay Goodstein 
37136dfe9acSMordechay Goodstein 	priv = __seq_open_private(filp, &iwl_dbgfs_info_seq_ops,
37236dfe9acSMordechay Goodstein 				  sizeof(*priv));
37336dfe9acSMordechay Goodstein 
37436dfe9acSMordechay Goodstein 	if (!priv)
37536dfe9acSMordechay Goodstein 		return -ENOMEM;
37636dfe9acSMordechay Goodstein 
37736dfe9acSMordechay Goodstein 	priv->fwrt = inode->i_private;
37836dfe9acSMordechay Goodstein 	return 0;
37936dfe9acSMordechay Goodstein }
38036dfe9acSMordechay Goodstein 
38136dfe9acSMordechay Goodstein static const struct file_operations iwl_dbgfs_fw_info_ops = {
38236dfe9acSMordechay Goodstein 	.owner = THIS_MODULE,
38336dfe9acSMordechay Goodstein 	.open = iwl_dbgfs_fw_info_open,
38436dfe9acSMordechay Goodstein 	.read = seq_read,
38536dfe9acSMordechay Goodstein 	.llseek = seq_lseek,
38636dfe9acSMordechay Goodstein 	.release = seq_release_private,
38736dfe9acSMordechay Goodstein };
38836dfe9acSMordechay Goodstein 
iwl_fwrt_dbgfs_register(struct iwl_fw_runtime * fwrt,struct dentry * dbgfs_dir)3895987dfdeSGreg Kroah-Hartman void iwl_fwrt_dbgfs_register(struct iwl_fw_runtime *fwrt,
39093b167c1SMordechay Goodstein 			    struct dentry *dbgfs_dir)
39193b167c1SMordechay Goodstein {
39293b167c1SMordechay Goodstein 	INIT_DELAYED_WORK(&fwrt->timestamp.wk, iwl_fw_timestamp_marker_wk);
3932ef00c53SJoe Perches 	FWRT_DEBUGFS_ADD_FILE(timestamp_marker, dbgfs_dir, 0200);
39436dfe9acSMordechay Goodstein 	FWRT_DEBUGFS_ADD_FILE(fw_info, dbgfs_dir, 0200);
395755384b3SShahar S Matityahu 	FWRT_DEBUGFS_ADD_FILE(send_hcmd, dbgfs_dir, 0200);
39687f1283bSMordechay Goodstein 	FWRT_DEBUGFS_ADD_FILE(enabled_severities, dbgfs_dir, 0200);
39710137f07SLuca Coelho 	FWRT_DEBUGFS_ADD_FILE(fw_dbg_domain, dbgfs_dir, 0400);
39893b167c1SMordechay Goodstein }
399