Loading...
1/*
2 * hsr_debugfs code
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 void print_mac_address(struct seq_file *sfp, unsigned char *mac)
24{
25 seq_printf(sfp, "%02x:%02x:%02x:%02x:%02x:%02x:",
26 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
27}
28
29/* hsr_node_table_show - Formats and prints node_table entries */
30static int
31hsr_node_table_show(struct seq_file *sfp, void *data)
32{
33 struct hsr_priv *priv = (struct hsr_priv *)sfp->private;
34 struct hsr_node *node;
35
36 seq_puts(sfp, "Node Table entries\n");
37 seq_puts(sfp, "MAC-Address-A, MAC-Address-B, time_in[A], ");
38 seq_puts(sfp, "time_in[B], Address-B port\n");
39 rcu_read_lock();
40 list_for_each_entry_rcu(node, &priv->node_db, mac_list) {
41 /* skip self node */
42 if (hsr_addr_is_self(priv, node->macaddress_A))
43 continue;
44 print_mac_address(sfp, &node->macaddress_A[0]);
45 seq_puts(sfp, " ");
46 print_mac_address(sfp, &node->macaddress_B[0]);
47 seq_printf(sfp, "0x%lx, ", node->time_in[HSR_PT_SLAVE_A]);
48 seq_printf(sfp, "0x%lx ", node->time_in[HSR_PT_SLAVE_B]);
49 seq_printf(sfp, "0x%x\n", node->addr_B_port);
50 }
51 rcu_read_unlock();
52 return 0;
53}
54
55/* hsr_node_table_open - Open the node_table file
56 *
57 * Description:
58 * This routine opens a debugfs file node_table of specific hsr device
59 */
60static int
61hsr_node_table_open(struct inode *inode, struct file *filp)
62{
63 return single_open(filp, hsr_node_table_show, inode->i_private);
64}
65
66static const struct file_operations hsr_fops = {
67 .owner = THIS_MODULE,
68 .open = hsr_node_table_open,
69 .read = seq_read,
70 .llseek = seq_lseek,
71 .release = single_release,
72};
73
74/* hsr_debugfs_init - create hsr node_table file for dumping
75 * the node table
76 *
77 * Description:
78 * When debugfs is configured this routine sets up the node_table file per
79 * hsr device for dumping the node_table entries
80 */
81int hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev)
82{
83 int rc = -1;
84 struct dentry *de = NULL;
85
86 de = debugfs_create_dir(hsr_dev->name, NULL);
87 if (!de) {
88 pr_err("Cannot create hsr debugfs root\n");
89 return rc;
90 }
91
92 priv->node_tbl_root = de;
93
94 de = debugfs_create_file("node_table", S_IFREG | 0444,
95 priv->node_tbl_root, priv,
96 &hsr_fops);
97 if (!de) {
98 pr_err("Cannot create hsr node_table directory\n");
99 return rc;
100 }
101 priv->node_tbl_file = de;
102
103 return 0;
104}
105
106/* hsr_debugfs_term - Tear down debugfs intrastructure
107 *
108 * Description:
109 * When Debufs is configured this routine removes debugfs file system
110 * elements that are specific to hsr
111 */
112void
113hsr_debugfs_term(struct hsr_priv *priv)
114{
115 debugfs_remove(priv->node_tbl_file);
116 priv->node_tbl_file = NULL;
117 debugfs_remove(priv->node_tbl_root);
118 priv->node_tbl_root = NULL;
119}
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
63DEFINE_SHOW_ATTRIBUTE(hsr_node_table);
64
65void hsr_debugfs_rename(struct net_device *dev)
66{
67 struct hsr_priv *priv = netdev_priv(dev);
68 struct dentry *d;
69
70 d = debugfs_rename(hsr_debugfs_root_dir, priv->node_tbl_root,
71 hsr_debugfs_root_dir, dev->name);
72 if (IS_ERR(d))
73 netdev_warn(dev, "failed to rename\n");
74 else
75 priv->node_tbl_root = d;
76}
77
78/* hsr_debugfs_init - create hsr node_table file for dumping
79 * the node table
80 *
81 * Description:
82 * When debugfs is configured this routine sets up the node_table file per
83 * hsr device for dumping the node_table entries
84 */
85void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev)
86{
87 struct dentry *de = NULL;
88
89 de = debugfs_create_dir(hsr_dev->name, hsr_debugfs_root_dir);
90 if (IS_ERR(de)) {
91 pr_err("Cannot create hsr debugfs directory\n");
92 return;
93 }
94
95 priv->node_tbl_root = de;
96
97 de = debugfs_create_file("node_table", S_IFREG | 0444,
98 priv->node_tbl_root, priv,
99 &hsr_node_table_fops);
100 if (IS_ERR(de)) {
101 pr_err("Cannot create hsr node_table file\n");
102 debugfs_remove(priv->node_tbl_root);
103 priv->node_tbl_root = NULL;
104 return;
105 }
106}
107
108/* hsr_debugfs_term - Tear down debugfs intrastructure
109 *
110 * Description:
111 * When Debugfs is configured this routine removes debugfs file system
112 * elements that are specific to hsr
113 */
114void
115hsr_debugfs_term(struct hsr_priv *priv)
116{
117 debugfs_remove_recursive(priv->node_tbl_root);
118 priv->node_tbl_root = NULL;
119}
120
121void hsr_debugfs_create_root(void)
122{
123 hsr_debugfs_root_dir = debugfs_create_dir("hsr", NULL);
124 if (IS_ERR(hsr_debugfs_root_dir)) {
125 pr_err("Cannot create hsr debugfs root directory\n");
126 hsr_debugfs_root_dir = NULL;
127 }
128}
129
130void hsr_debugfs_remove_root(void)
131{
132 /* debugfs_remove() internally checks NULL and ERROR */
133 debugfs_remove(hsr_debugfs_root_dir);
134}