diff mbox series

[v2,3/7] qla2xxx: Add debugfs create/delete helpers

Message ID 20220826102559.17474-4-njavali@marvell.com
State New
Headers show
Series qla2xxx driver features | expand

Commit Message

Nilesh Javali Aug. 26, 2022, 10:25 a.m. UTC
From: Arun Easi <aeasi@marvell.com>

Define a few helpful macros for creating debugfs files.

Signed-off-by: Arun Easi <aeasi@marvell.com>
Signed-off-by: Nilesh Javali <njavali@marvell.com>
---
 drivers/scsi/qla2xxx/qla_def.h |  5 ++
 drivers/scsi/qla2xxx/qla_dfs.c | 93 ++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+)

Comments

Himanshu Madhani Aug. 29, 2022, 4:10 p.m. UTC | #1
> On Aug 26, 2022, at 3:25 AM, Nilesh Javali <njavali@marvell.com> wrote:
> 
> From: Arun Easi <aeasi@marvell.com>
> 
> Define a few helpful macros for creating debugfs files.
> 
> Signed-off-by: Arun Easi <aeasi@marvell.com>
> Signed-off-by: Nilesh Javali <njavali@marvell.com>
> ---
> drivers/scsi/qla2xxx/qla_def.h |  5 ++
> drivers/scsi/qla2xxx/qla_dfs.c | 93 ++++++++++++++++++++++++++++++++++
> 2 files changed, 98 insertions(+)
> 
> diff --git a/drivers/scsi/qla2xxx/qla_def.h b/drivers/scsi/qla2xxx/qla_def.h
> index 3ec6a200942e..22274b405d01 100644
> --- a/drivers/scsi/qla2xxx/qla_def.h
> +++ b/drivers/scsi/qla2xxx/qla_def.h
> @@ -35,6 +35,11 @@
> 
> #include <uapi/scsi/fc/fc_els.h>
> 
> +#define QLA_DFS_DEFINE_DENTRY(_debugfs_file_name) \
> +	struct dentry *dfs_##_debugfs_file_name
> +#define QLA_DFS_ROOT_DEFINE_DENTRY(_debugfs_file_name) \
> +	struct dentry *qla_dfs_##_debugfs_file_name
> +
> /* Big endian Fibre Channel S_ID (source ID) or D_ID (destination ID). */
> typedef struct {
> 	uint8_t domain;
> diff --git a/drivers/scsi/qla2xxx/qla_dfs.c b/drivers/scsi/qla2xxx/qla_dfs.c
> index 85bd0e468d43..777808af5634 100644
> --- a/drivers/scsi/qla2xxx/qla_dfs.c
> +++ b/drivers/scsi/qla2xxx/qla_dfs.c
> @@ -489,6 +489,99 @@ qla_dfs_naqp_show(struct seq_file *s, void *unused)
> 	return 0;
> }
> 
> +/*
> + * Helper macros for setting up debugfs entries.
> + * _name: The name of the debugfs entry
> + * _ctx_struct: The context that was passed when creating the debugfs file
> + *
> + * QLA_DFS_SETUP_RD could be used when there is only a show function.
> + * - show function take the name qla_dfs_<sysfs-name>_show
> + *
> + * QLA_DFS_SETUP_RW could be used when there are both show and write functions.
> + * - show function take the name  qla_dfs_<sysfs-name>_show
> + * - write function take the name qla_dfs_<sysfs-name>_write
> + *
> + * To have a new debugfs entry, do:
> + * 1. Create a "struct dentry *" in the appropriate structure in the format
> + * dfs_<sysfs-name>
> + * 2. Setup debugfs entries using QLA_DFS_SETUP_RD / QLA_DFS_SETUP_RW
> + * 3. Create debugfs file in qla2x00_dfs_setup() using QLA_DFS_CREATE_FILE
> + * or QLA_DFS_ROOT_CREATE_FILE
> + * 4. Remove debugfs file in qla2x00_dfs_remove() using QLA_DFS_REMOVE_FILE
> + * or QLA_DFS_ROOT_REMOVE_FILE
> + *
> + * Example for creating "TEST" sysfs file:
> + * 1. struct qla_hw_data { ... struct dentry *dfs_TEST; }
> + * 2. QLA_DFS_SETUP_RD(TEST, scsi_qla_host_t);
> + * 3. In qla2x00_dfs_setup():
> + * QLA_DFS_CREATE_FILE(ha, TEST, 0600, ha->dfs_dir, vha);
> + * 4. In qla2x00_dfs_remove():
> + * QLA_DFS_REMOVE_FILE(ha, TEST);
> + */
> +#define QLA_DFS_SETUP_RD(_name, _ctx_struct)				\
> +static int								\
> +qla_dfs_##_name##_open(struct inode *inode, struct file *file)		\
> +{									\
> +	_ctx_struct *__ctx = inode->i_private;				\
> +									\
> +	return single_open(file, qla_dfs_##_name##_show, __ctx);	\
> +}									\
> +									\
> +static const struct file_operations qla_dfs_##_name##_ops = {		\
> +	.open           = qla_dfs_##_name##_open,			\
> +	.read           = seq_read,					\
> +	.llseek         = seq_lseek,					\
> +	.release        = single_release,				\
> +};
> +
> +#define QLA_DFS_SETUP_RW(_name, _ctx_struct)				\
> +static int								\
> +qla_dfs_##_name##_open(struct inode *inode, struct file *file)		\
> +{									\
> +	_ctx_struct *__ctx = inode->i_private;				\
> +									\
> +	return single_open(file, qla_dfs_##_name##_show, __ctx);	\
> +}									\
> +									\
> +static const struct file_operations qla_dfs_##_name##_ops = {		\
> +	.open           = qla_dfs_##_name##_open,			\
> +	.read           = seq_read,					\
> +	.llseek         = seq_lseek,					\
> +	.release        = single_release,				\
> +	.write		= qla_dfs_##_name##_write,			\
> +};
> +
> +#define QLA_DFS_ROOT_CREATE_FILE(_name, _perm, _ctx)			\
> +	do {								\
> +		if (!qla_dfs_##_name)					\
> +			qla_dfs_##_name = debugfs_create_file(#_name,	\
> +					_perm, qla2x00_dfs_root, _ctx,	\
> +					&qla_dfs_##_name##_ops);	\
> +	} while (0)
> +
> +#define QLA_DFS_ROOT_REMOVE_FILE(_name)					\
> +	do {								\
> +		if (qla_dfs_##_name) {					\
> +			debugfs_remove(qla_dfs_##_name);		\
> +			qla_dfs_##_name = NULL;				\
> +		}							\
> +	} while (0)
> +
> +#define QLA_DFS_CREATE_FILE(_struct, _name, _perm, _parent, _ctx)	\
> +	do {								\
> +		(_struct)->dfs_##_name = debugfs_create_file(#_name,	\
> +					_perm, _parent, _ctx,		\
> +					&qla_dfs_##_name##_ops)		\
> +	} while (0)
> +
> +#define QLA_DFS_REMOVE_FILE(_struct, _name)				\
> +	do {								\
> +		if ((_struct)->dfs_##_name) {				\
> +			debugfs_remove((_struct)->dfs_##_name);		\
> +			(_struct)->dfs_##_name = NULL;			\
> +		}							\
> +	} while (0)
> +
> static int
> qla_dfs_naqp_open(struct inode *inode, struct file *file)
> {
> -- 
> 2.19.0.rc0
> 

Reviewed-by: Himanshu Madhani <himanshu.madhani@oracle.com>
diff mbox series

Patch

diff --git a/drivers/scsi/qla2xxx/qla_def.h b/drivers/scsi/qla2xxx/qla_def.h
index 3ec6a200942e..22274b405d01 100644
--- a/drivers/scsi/qla2xxx/qla_def.h
+++ b/drivers/scsi/qla2xxx/qla_def.h
@@ -35,6 +35,11 @@ 
 
 #include <uapi/scsi/fc/fc_els.h>
 
+#define QLA_DFS_DEFINE_DENTRY(_debugfs_file_name) \
+	struct dentry *dfs_##_debugfs_file_name
+#define QLA_DFS_ROOT_DEFINE_DENTRY(_debugfs_file_name) \
+	struct dentry *qla_dfs_##_debugfs_file_name
+
 /* Big endian Fibre Channel S_ID (source ID) or D_ID (destination ID). */
 typedef struct {
 	uint8_t domain;
diff --git a/drivers/scsi/qla2xxx/qla_dfs.c b/drivers/scsi/qla2xxx/qla_dfs.c
index 85bd0e468d43..777808af5634 100644
--- a/drivers/scsi/qla2xxx/qla_dfs.c
+++ b/drivers/scsi/qla2xxx/qla_dfs.c
@@ -489,6 +489,99 @@  qla_dfs_naqp_show(struct seq_file *s, void *unused)
 	return 0;
 }
 
+/*
+ * Helper macros for setting up debugfs entries.
+ * _name: The name of the debugfs entry
+ * _ctx_struct: The context that was passed when creating the debugfs file
+ *
+ * QLA_DFS_SETUP_RD could be used when there is only a show function.
+ * - show function take the name qla_dfs_<sysfs-name>_show
+ *
+ * QLA_DFS_SETUP_RW could be used when there are both show and write functions.
+ * - show function take the name  qla_dfs_<sysfs-name>_show
+ * - write function take the name qla_dfs_<sysfs-name>_write
+ *
+ * To have a new debugfs entry, do:
+ * 1. Create a "struct dentry *" in the appropriate structure in the format
+ * dfs_<sysfs-name>
+ * 2. Setup debugfs entries using QLA_DFS_SETUP_RD / QLA_DFS_SETUP_RW
+ * 3. Create debugfs file in qla2x00_dfs_setup() using QLA_DFS_CREATE_FILE
+ * or QLA_DFS_ROOT_CREATE_FILE
+ * 4. Remove debugfs file in qla2x00_dfs_remove() using QLA_DFS_REMOVE_FILE
+ * or QLA_DFS_ROOT_REMOVE_FILE
+ *
+ * Example for creating "TEST" sysfs file:
+ * 1. struct qla_hw_data { ... struct dentry *dfs_TEST; }
+ * 2. QLA_DFS_SETUP_RD(TEST, scsi_qla_host_t);
+ * 3. In qla2x00_dfs_setup():
+ * QLA_DFS_CREATE_FILE(ha, TEST, 0600, ha->dfs_dir, vha);
+ * 4. In qla2x00_dfs_remove():
+ * QLA_DFS_REMOVE_FILE(ha, TEST);
+ */
+#define QLA_DFS_SETUP_RD(_name, _ctx_struct)				\
+static int								\
+qla_dfs_##_name##_open(struct inode *inode, struct file *file)		\
+{									\
+	_ctx_struct *__ctx = inode->i_private;				\
+									\
+	return single_open(file, qla_dfs_##_name##_show, __ctx);	\
+}									\
+									\
+static const struct file_operations qla_dfs_##_name##_ops = {		\
+	.open           = qla_dfs_##_name##_open,			\
+	.read           = seq_read,					\
+	.llseek         = seq_lseek,					\
+	.release        = single_release,				\
+};
+
+#define QLA_DFS_SETUP_RW(_name, _ctx_struct)				\
+static int								\
+qla_dfs_##_name##_open(struct inode *inode, struct file *file)		\
+{									\
+	_ctx_struct *__ctx = inode->i_private;				\
+									\
+	return single_open(file, qla_dfs_##_name##_show, __ctx);	\
+}									\
+									\
+static const struct file_operations qla_dfs_##_name##_ops = {		\
+	.open           = qla_dfs_##_name##_open,			\
+	.read           = seq_read,					\
+	.llseek         = seq_lseek,					\
+	.release        = single_release,				\
+	.write		= qla_dfs_##_name##_write,			\
+};
+
+#define QLA_DFS_ROOT_CREATE_FILE(_name, _perm, _ctx)			\
+	do {								\
+		if (!qla_dfs_##_name)					\
+			qla_dfs_##_name = debugfs_create_file(#_name,	\
+					_perm, qla2x00_dfs_root, _ctx,	\
+					&qla_dfs_##_name##_ops);	\
+	} while (0)
+
+#define QLA_DFS_ROOT_REMOVE_FILE(_name)					\
+	do {								\
+		if (qla_dfs_##_name) {					\
+			debugfs_remove(qla_dfs_##_name);		\
+			qla_dfs_##_name = NULL;				\
+		}							\
+	} while (0)
+
+#define QLA_DFS_CREATE_FILE(_struct, _name, _perm, _parent, _ctx)	\
+	do {								\
+		(_struct)->dfs_##_name = debugfs_create_file(#_name,	\
+					_perm, _parent, _ctx,		\
+					&qla_dfs_##_name##_ops)		\
+	} while (0)
+
+#define QLA_DFS_REMOVE_FILE(_struct, _name)				\
+	do {								\
+		if ((_struct)->dfs_##_name) {				\
+			debugfs_remove((_struct)->dfs_##_name);		\
+			(_struct)->dfs_##_name = NULL;			\
+		}							\
+	} while (0)
+
 static int
 qla_dfs_naqp_open(struct inode *inode, struct file *file)
 {