xref: /linux/drivers/infiniband/hw/hns/hns_roce_debugfs.c (revision daa2be74b1b2302004945b2a5e32424e177cc7da)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2023 Hisilicon Limited.
4  */
5 
6 #include <linux/debugfs.h>
7 #include <linux/device.h>
8 
9 #include "hns_roce_device.h"
10 
11 static struct dentry *hns_roce_dbgfs_root;
12 
13 static int hns_debugfs_seqfile_open(struct inode *inode, struct file *f)
14 {
15 	struct hns_debugfs_seqfile *seqfile = inode->i_private;
16 
17 	return single_open(f, seqfile->read, seqfile->data);
18 }
19 
20 static const struct file_operations hns_debugfs_seqfile_fops = {
21 	.owner = THIS_MODULE,
22 	.open = hns_debugfs_seqfile_open,
23 	.release = single_release,
24 	.read = seq_read,
25 	.llseek = seq_lseek
26 };
27 
28 static void init_debugfs_seqfile(struct hns_debugfs_seqfile *seq,
29 				 const char *name, struct dentry *parent,
30 				 int (*read_fn)(struct seq_file *, void *),
31 				 void *data)
32 {
33 	debugfs_create_file(name, 0400, parent, seq, &hns_debugfs_seqfile_fops);
34 
35 	seq->read = read_fn;
36 	seq->data = data;
37 }
38 
39 static const char * const sw_stat_info[] = {
40 	[HNS_ROCE_DFX_AEQE_CNT] = "aeqe",
41 	[HNS_ROCE_DFX_CEQE_CNT] = "ceqe",
42 	[HNS_ROCE_DFX_CMDS_CNT] = "cmds",
43 	[HNS_ROCE_DFX_CMDS_ERR_CNT] = "cmds_err",
44 	[HNS_ROCE_DFX_MBX_POSTED_CNT] = "posted_mbx",
45 	[HNS_ROCE_DFX_MBX_POLLED_CNT] = "polled_mbx",
46 	[HNS_ROCE_DFX_MBX_EVENT_CNT] = "mbx_event",
47 	[HNS_ROCE_DFX_QP_CREATE_ERR_CNT] = "qp_create_err",
48 	[HNS_ROCE_DFX_QP_MODIFY_ERR_CNT] = "qp_modify_err",
49 	[HNS_ROCE_DFX_CQ_CREATE_ERR_CNT] = "cq_create_err",
50 	[HNS_ROCE_DFX_CQ_MODIFY_ERR_CNT] = "cq_modify_err",
51 	[HNS_ROCE_DFX_SRQ_CREATE_ERR_CNT] = "srq_create_err",
52 	[HNS_ROCE_DFX_SRQ_MODIFY_ERR_CNT] = "srq_modify_err",
53 	[HNS_ROCE_DFX_XRCD_ALLOC_ERR_CNT] = "xrcd_alloc_err",
54 	[HNS_ROCE_DFX_MR_REG_ERR_CNT] = "mr_reg_err",
55 	[HNS_ROCE_DFX_MR_REREG_ERR_CNT] = "mr_rereg_err",
56 	[HNS_ROCE_DFX_AH_CREATE_ERR_CNT] = "ah_create_err",
57 	[HNS_ROCE_DFX_MMAP_ERR_CNT] = "mmap_err",
58 	[HNS_ROCE_DFX_UCTX_ALLOC_ERR_CNT] = "uctx_alloc_err",
59 };
60 
61 static int sw_stat_debugfs_show(struct seq_file *file, void *offset)
62 {
63 	struct hns_roce_dev *hr_dev = file->private;
64 	int i;
65 
66 	for (i = 0; i < HNS_ROCE_DFX_CNT_TOTAL; i++)
67 		seq_printf(file, "%-20s --- %lld\n", sw_stat_info[i],
68 			   atomic64_read(&hr_dev->dfx_cnt[i]));
69 
70 	return 0;
71 }
72 
73 static void create_sw_stat_debugfs(struct hns_roce_dev *hr_dev,
74 				   struct dentry *parent)
75 {
76 	struct hns_sw_stat_debugfs *dbgfs = &hr_dev->dbgfs.sw_stat_root;
77 
78 	dbgfs->root = debugfs_create_dir("sw_stat", parent);
79 
80 	init_debugfs_seqfile(&dbgfs->sw_stat, "sw_stat", dbgfs->root,
81 			     sw_stat_debugfs_show, hr_dev);
82 }
83 
84 /* debugfs for device */
85 void hns_roce_register_debugfs(struct hns_roce_dev *hr_dev)
86 {
87 	struct hns_roce_dev_debugfs *dbgfs = &hr_dev->dbgfs;
88 
89 	dbgfs->root = debugfs_create_dir(dev_name(&hr_dev->ib_dev.dev),
90 					 hns_roce_dbgfs_root);
91 
92 	create_sw_stat_debugfs(hr_dev, dbgfs->root);
93 }
94 
95 void hns_roce_unregister_debugfs(struct hns_roce_dev *hr_dev)
96 {
97 	debugfs_remove_recursive(hr_dev->dbgfs.root);
98 }
99 
100 /* debugfs for hns module */
101 void hns_roce_init_debugfs(void)
102 {
103 	hns_roce_dbgfs_root = debugfs_create_dir("hns_roce", NULL);
104 }
105 
106 void hns_roce_cleanup_debugfs(void)
107 {
108 	debugfs_remove_recursive(hns_roce_dbgfs_root);
109 	hns_roce_dbgfs_root = NULL;
110 }
111