blob: 94447974a3c05d735cad2d26648f432a45e3697a [file] [log] [blame]
Murali Karicherifc4ecae2019-04-05 13:31:35 -04001/*
Murali Karicheri9c5f8a192019-04-15 11:36:01 -04002 * hsr_debugfs code
Murali Karicheri32712732019-04-15 11:36:02 -04003 * Copyright (C) 2019 Texas Instruments Incorporated
Murali Karicherifc4ecae2019-04-05 13:31:35 -04004 *
5 * Author(s):
Murali Karicheri32712732019-04-15 11:36:02 -04006 * Murali Karicheri <m-karicheri2@ti.com>
Murali Karicherifc4ecae2019-04-05 13:31:35 -04007 *
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
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040029/* hsr_node_table_show - Formats and prints node_table entries */
Murali Karicherifc4ecae2019-04-05 13:31:35 -040030static int
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040031hsr_node_table_show(struct seq_file *sfp, void *data)
Murali Karicherifc4ecae2019-04-05 13:31:35 -040032{
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
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040055/* hsr_node_table_open - Open the node_table file
Murali Karicherifc4ecae2019-04-05 13:31:35 -040056 *
57 * Description:
58 * This routine opens a debugfs file node_table of specific hsr device
59 */
60static int
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040061hsr_node_table_open(struct inode *inode, struct file *filp)
Murali Karicherifc4ecae2019-04-05 13:31:35 -040062{
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040063 return single_open(filp, hsr_node_table_show, inode->i_private);
Murali Karicherifc4ecae2019-04-05 13:31:35 -040064}
65
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040066static const struct file_operations hsr_fops = {
Murali Karicherifc4ecae2019-04-05 13:31:35 -040067 .owner = THIS_MODULE,
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040068 .open = hsr_node_table_open,
Murali Karicherifc4ecae2019-04-05 13:31:35 -040069 .read = seq_read,
70 .llseek = seq_lseek,
71 .release = single_release,
72};
73
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040074/* hsr_debugfs_init - create hsr node_table file for dumping
Murali Karicherifc4ecae2019-04-05 13:31:35 -040075 * the node table
76 *
77 * Description:
78 * When debugfs is configured this routine sets up the node_table file per
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040079 * hsr device for dumping the node_table entries
Murali Karicherifc4ecae2019-04-05 13:31:35 -040080 */
Murali Karicheri32712732019-04-15 11:36:02 -040081int hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev)
Murali Karicherifc4ecae2019-04-05 13:31:35 -040082{
83 int rc = -1;
84 struct dentry *de = NULL;
85
Murali Karicheri32712732019-04-15 11:36:02 -040086 de = debugfs_create_dir(hsr_dev->name, NULL);
Murali Karicherifc4ecae2019-04-05 13:31:35 -040087 if (!de) {
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040088 pr_err("Cannot create hsr debugfs root\n");
Murali Karicherifc4ecae2019-04-05 13:31:35 -040089 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,
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040096 &hsr_fops);
Murali Karicherifc4ecae2019-04-05 13:31:35 -040097 if (!de) {
Murali Karicheri9c5f8a192019-04-15 11:36:01 -040098 pr_err("Cannot create hsr node_table directory\n");
Murali Karicherifc4ecae2019-04-05 13:31:35 -040099 return rc;
100 }
101 priv->node_tbl_file = de;
Murali Karicherifc4ecae2019-04-05 13:31:35 -0400102
Murali Karicheri32712732019-04-15 11:36:02 -0400103 return 0;
Murali Karicherifc4ecae2019-04-05 13:31:35 -0400104}
105
Murali Karicheri9c5f8a192019-04-15 11:36:01 -0400106/* hsr_debugfs_term - Tear down debugfs intrastructure
Murali Karicherifc4ecae2019-04-05 13:31:35 -0400107 *
108 * Description:
109 * When Debufs is configured this routine removes debugfs file system
Murali Karicheri9c5f8a192019-04-15 11:36:01 -0400110 * elements that are specific to hsr
Murali Karicherifc4ecae2019-04-05 13:31:35 -0400111 */
112void
Murali Karicheri9c5f8a192019-04-15 11:36:01 -0400113hsr_debugfs_term(struct hsr_priv *priv)
Murali Karicherifc4ecae2019-04-05 13:31:35 -0400114{
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}