Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * debugfs code for HSR & PRP
4 * Copyright (C) 2019 Texas Instruments Incorporated
5 *
6 * Author(s):
7 * Murali Karicheri <m-karicheri2@ti.com>
8 */
9#include <linux/module.h>
10#include <linux/errno.h>
11#include <linux/debugfs.h>
12#include "hsr_main.h"
13#include "hsr_framereg.h"
14
15static struct dentry *hsr_debugfs_root_dir;
16
17/* hsr_node_table_show - Formats and prints node_table entries */
18static int
19hsr_node_table_show(struct seq_file *sfp, void *data)
20{
21 struct hsr_priv *priv = (struct hsr_priv *)sfp->private;
22 struct hsr_node *node;
23
24 seq_printf(sfp, "Node Table entries for (%s) device\n",
25 (priv->prot_version == PRP_V1 ? "PRP" : "HSR"));
26 seq_puts(sfp, "MAC-Address-A, MAC-Address-B, time_in[A], ");
27 seq_puts(sfp, "time_in[B], Address-B port, ");
28 if (priv->prot_version == PRP_V1)
29 seq_puts(sfp, "SAN-A, SAN-B, DAN-P\n");
30 else
31 seq_puts(sfp, "DAN-H\n");
32
33 rcu_read_lock();
34 list_for_each_entry_rcu(node, &priv->node_db, mac_list) {
35 /* skip self node */
36 if (hsr_addr_is_self(priv, node->macaddress_A))
37 continue;
38 seq_printf(sfp, "%pM ", &node->macaddress_A[0]);
39 seq_printf(sfp, "%pM ", &node->macaddress_B[0]);
40 seq_printf(sfp, "%10lx, ", node->time_in[HSR_PT_SLAVE_A]);
41 seq_printf(sfp, "%10lx, ", node->time_in[HSR_PT_SLAVE_B]);
42 seq_printf(sfp, "%14x, ", node->addr_B_port);
43
44 if (priv->prot_version == PRP_V1)
45 seq_printf(sfp, "%5x, %5x, %5x\n",
46 node->san_a, node->san_b,
47 (node->san_a == 0 && node->san_b == 0));
48 else
49 seq_printf(sfp, "%5x\n", 1);
50 }
51 rcu_read_unlock();
52 return 0;
53}
54
55DEFINE_SHOW_ATTRIBUTE(hsr_node_table);
56
57void hsr_debugfs_rename(struct net_device *dev)
58{
59 struct hsr_priv *priv = netdev_priv(dev);
60 struct dentry *d;
61
62 d = debugfs_rename(hsr_debugfs_root_dir, priv->node_tbl_root,
63 hsr_debugfs_root_dir, dev->name);
64 if (IS_ERR(d))
65 netdev_warn(dev, "failed to rename\n");
66 else
67 priv->node_tbl_root = d;
68}
69
70/* hsr_debugfs_init - create hsr node_table file for dumping
71 * the node table
72 *
73 * Description:
74 * When debugfs is configured this routine sets up the node_table file per
75 * hsr device for dumping the node_table entries
76 */
77void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev)
78{
79 struct dentry *de = NULL;
80
81 de = debugfs_create_dir(hsr_dev->name, hsr_debugfs_root_dir);
82 if (IS_ERR(de)) {
83 pr_err("Cannot create hsr debugfs directory\n");
84 return;
85 }
86
87 priv->node_tbl_root = de;
88
89 de = debugfs_create_file("node_table", S_IFREG | 0444,
90 priv->node_tbl_root, priv,
91 &hsr_node_table_fops);
92 if (IS_ERR(de)) {
93 pr_err("Cannot create hsr node_table file\n");
94 debugfs_remove(priv->node_tbl_root);
95 priv->node_tbl_root = NULL;
96 return;
97 }
98}
99
100/* hsr_debugfs_term - Tear down debugfs intrastructure
101 *
102 * Description:
103 * When Debugfs is configured this routine removes debugfs file system
104 * elements that are specific to hsr
105 */
106void
107hsr_debugfs_term(struct hsr_priv *priv)
108{
109 debugfs_remove_recursive(priv->node_tbl_root);
110 priv->node_tbl_root = NULL;
111}
112
113void hsr_debugfs_create_root(void)
114{
115 hsr_debugfs_root_dir = debugfs_create_dir("hsr", NULL);
116 if (IS_ERR(hsr_debugfs_root_dir)) {
117 pr_err("Cannot create hsr debugfs root directory\n");
118 hsr_debugfs_root_dir = NULL;
119 }
120}
121
122void hsr_debugfs_remove_root(void)
123{
124 /* debugfs_remove() internally checks NULL and ERROR */
125 debugfs_remove(hsr_debugfs_root_dir);
126}
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 "hsr_main.h"
21#include "hsr_framereg.h"
22
23static struct dentry *hsr_debugfs_root_dir;
24
25/* hsr_node_table_show - Formats and prints node_table entries */
26static int
27hsr_node_table_show(struct seq_file *sfp, void *data)
28{
29 struct hsr_priv *priv = (struct hsr_priv *)sfp->private;
30 struct hsr_node *node;
31
32 seq_printf(sfp, "Node Table entries for (%s) device\n",
33 (priv->prot_version == PRP_V1 ? "PRP" : "HSR"));
34 seq_puts(sfp, "MAC-Address-A, MAC-Address-B, time_in[A], ");
35 seq_puts(sfp, "time_in[B], Address-B port, ");
36 if (priv->prot_version == PRP_V1)
37 seq_puts(sfp, "SAN-A, SAN-B, DAN-P\n");
38 else
39 seq_puts(sfp, "DAN-H\n");
40
41 rcu_read_lock();
42 list_for_each_entry_rcu(node, &priv->node_db, mac_list) {
43 /* skip self node */
44 if (hsr_addr_is_self(priv, node->macaddress_A))
45 continue;
46 seq_printf(sfp, "%pM ", &node->macaddress_A[0]);
47 seq_printf(sfp, "%pM ", &node->macaddress_B[0]);
48 seq_printf(sfp, "%10lx, ", node->time_in[HSR_PT_SLAVE_A]);
49 seq_printf(sfp, "%10lx, ", node->time_in[HSR_PT_SLAVE_B]);
50 seq_printf(sfp, "%14x, ", node->addr_B_port);
51
52 if (priv->prot_version == PRP_V1)
53 seq_printf(sfp, "%5x, %5x, %5x\n",
54 node->san_a, node->san_b,
55 (node->san_a == 0 && node->san_b == 0));
56 else
57 seq_printf(sfp, "%5x\n", 1);
58 }
59 rcu_read_unlock();
60 return 0;
61}
62
63/* hsr_node_table_open - Open the node_table file
64 *
65 * Description:
66 * This routine opens a debugfs file node_table of specific hsr
67 * or prp device
68 */
69static int
70hsr_node_table_open(struct inode *inode, struct file *filp)
71{
72 return single_open(filp, hsr_node_table_show, inode->i_private);
73}
74
75void hsr_debugfs_rename(struct net_device *dev)
76{
77 struct hsr_priv *priv = netdev_priv(dev);
78 struct dentry *d;
79
80 d = debugfs_rename(hsr_debugfs_root_dir, priv->node_tbl_root,
81 hsr_debugfs_root_dir, dev->name);
82 if (IS_ERR(d))
83 netdev_warn(dev, "failed to rename\n");
84 else
85 priv->node_tbl_root = d;
86}
87
88static const struct file_operations hsr_fops = {
89 .open = hsr_node_table_open,
90 .read = seq_read,
91 .llseek = seq_lseek,
92 .release = single_release,
93};
94
95/* hsr_debugfs_init - create hsr node_table file for dumping
96 * the node table
97 *
98 * Description:
99 * When debugfs is configured this routine sets up the node_table file per
100 * hsr device for dumping the node_table entries
101 */
102void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev)
103{
104 struct dentry *de = NULL;
105
106 de = debugfs_create_dir(hsr_dev->name, hsr_debugfs_root_dir);
107 if (IS_ERR(de)) {
108 pr_err("Cannot create hsr debugfs directory\n");
109 return;
110 }
111
112 priv->node_tbl_root = de;
113
114 de = debugfs_create_file("node_table", S_IFREG | 0444,
115 priv->node_tbl_root, priv,
116 &hsr_fops);
117 if (IS_ERR(de)) {
118 pr_err("Cannot create hsr node_table file\n");
119 debugfs_remove(priv->node_tbl_root);
120 priv->node_tbl_root = NULL;
121 return;
122 }
123}
124
125/* hsr_debugfs_term - Tear down debugfs intrastructure
126 *
127 * Description:
128 * When Debufs is configured this routine removes debugfs file system
129 * elements that are specific to hsr
130 */
131void
132hsr_debugfs_term(struct hsr_priv *priv)
133{
134 debugfs_remove_recursive(priv->node_tbl_root);
135 priv->node_tbl_root = NULL;
136}
137
138void hsr_debugfs_create_root(void)
139{
140 hsr_debugfs_root_dir = debugfs_create_dir("hsr", NULL);
141 if (IS_ERR(hsr_debugfs_root_dir)) {
142 pr_err("Cannot create hsr debugfs root directory\n");
143 hsr_debugfs_root_dir = NULL;
144 }
145}
146
147void hsr_debugfs_remove_root(void)
148{
149 /* debugfs_remove() internally checks NULL and ERROR */
150 debugfs_remove(hsr_debugfs_root_dir);
151}