xref: /openbmc/linux/net/hsr/hsr_debugfs.c (revision 74de3792)
1 /*
2  * debugfs code for HSR & PRP
3  * Copyright (C) 2019 Texas Instruments Incorporated
4  *
5  * Author(s):
6  *	Murali Karicheri <m-karicheri2@ti.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation version 2.
11  *
12  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13  * kind, whether express or implied; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/debugfs.h>
20 #include <linux/jhash.h>
21 #include "hsr_main.h"
22 #include "hsr_framereg.h"
23 
24 static struct dentry *hsr_debugfs_root_dir;
25 
26 /* hsr_node_table_show - Formats and prints node_table entries */
27 static int
28 hsr_node_table_show(struct seq_file *sfp, void *data)
29 {
30 	struct hsr_priv *priv = (struct hsr_priv *)sfp->private;
31 	struct hsr_node *node;
32 	int i;
33 
34 	seq_printf(sfp, "Node Table entries for (%s) device\n",
35 		   (priv->prot_version == PRP_V1 ? "PRP" : "HSR"));
36 	seq_puts(sfp, "MAC-Address-A,    MAC-Address-B,    time_in[A], ");
37 	seq_puts(sfp, "time_in[B], Address-B port, ");
38 	if (priv->prot_version == PRP_V1)
39 		seq_puts(sfp, "SAN-A, SAN-B, DAN-P\n");
40 	else
41 		seq_puts(sfp, "DAN-H\n");
42 
43 	rcu_read_lock();
44 
45 	for (i = 0 ; i < priv->hash_buckets; i++) {
46 		hlist_for_each_entry_rcu(node, &priv->node_db[i], mac_list) {
47 			/* skip self node */
48 			if (hsr_addr_is_self(priv, node->macaddress_A))
49 				continue;
50 			seq_printf(sfp, "%pM ", &node->macaddress_A[0]);
51 			seq_printf(sfp, "%pM ", &node->macaddress_B[0]);
52 			seq_printf(sfp, "%10lx, ",
53 				   node->time_in[HSR_PT_SLAVE_A]);
54 			seq_printf(sfp, "%10lx, ",
55 				   node->time_in[HSR_PT_SLAVE_B]);
56 			seq_printf(sfp, "%14x, ", node->addr_B_port);
57 
58 			if (priv->prot_version == PRP_V1)
59 				seq_printf(sfp, "%5x, %5x, %5x\n",
60 					   node->san_a, node->san_b,
61 					   (node->san_a == 0 &&
62 					    node->san_b == 0));
63 			else
64 				seq_printf(sfp, "%5x\n", 1);
65 		}
66 	}
67 	rcu_read_unlock();
68 	return 0;
69 }
70 
71 DEFINE_SHOW_ATTRIBUTE(hsr_node_table);
72 
73 void hsr_debugfs_rename(struct net_device *dev)
74 {
75 	struct hsr_priv *priv = netdev_priv(dev);
76 	struct dentry *d;
77 
78 	d = debugfs_rename(hsr_debugfs_root_dir, priv->node_tbl_root,
79 			   hsr_debugfs_root_dir, dev->name);
80 	if (IS_ERR(d))
81 		netdev_warn(dev, "failed to rename\n");
82 	else
83 		priv->node_tbl_root = d;
84 }
85 
86 /* hsr_debugfs_init - create hsr node_table file for dumping
87  * the node table
88  *
89  * Description:
90  * When debugfs is configured this routine sets up the node_table file per
91  * hsr device for dumping the node_table entries
92  */
93 void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev)
94 {
95 	struct dentry *de = NULL;
96 
97 	de = debugfs_create_dir(hsr_dev->name, hsr_debugfs_root_dir);
98 	if (IS_ERR(de)) {
99 		pr_err("Cannot create hsr debugfs directory\n");
100 		return;
101 	}
102 
103 	priv->node_tbl_root = de;
104 
105 	de = debugfs_create_file("node_table", S_IFREG | 0444,
106 				 priv->node_tbl_root, priv,
107 				 &hsr_node_table_fops);
108 	if (IS_ERR(de)) {
109 		pr_err("Cannot create hsr node_table file\n");
110 		debugfs_remove(priv->node_tbl_root);
111 		priv->node_tbl_root = NULL;
112 		return;
113 	}
114 }
115 
116 /* hsr_debugfs_term - Tear down debugfs intrastructure
117  *
118  * Description:
119  * When Debugfs is configured this routine removes debugfs file system
120  * elements that are specific to hsr
121  */
122 void
123 hsr_debugfs_term(struct hsr_priv *priv)
124 {
125 	debugfs_remove_recursive(priv->node_tbl_root);
126 	priv->node_tbl_root = NULL;
127 }
128 
129 void hsr_debugfs_create_root(void)
130 {
131 	hsr_debugfs_root_dir = debugfs_create_dir("hsr", NULL);
132 	if (IS_ERR(hsr_debugfs_root_dir)) {
133 		pr_err("Cannot create hsr debugfs root directory\n");
134 		hsr_debugfs_root_dir = NULL;
135 	}
136 }
137 
138 void hsr_debugfs_remove_root(void)
139 {
140 	/* debugfs_remove() internally checks NULL and ERROR */
141 	debugfs_remove(hsr_debugfs_root_dir);
142 }
143