Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 1 | /* |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 2 | * hsr_debugfs code |
Murali Karicheri | 3271273 | 2019-04-15 11:36:02 -0400 | [diff] [blame] | 3 | * Copyright (C) 2019 Texas Instruments Incorporated |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 4 | * |
| 5 | * Author(s): |
Murali Karicheri | 3271273 | 2019-04-15 11:36:02 -0400 | [diff] [blame] | 6 | * Murali Karicheri <m-karicheri2@ti.com> |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 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 | |
Taehee Yoo | c6c4ccd | 2019-12-22 11:26:27 +0000 | [diff] [blame] | 23 | static struct dentry *hsr_debugfs_root_dir; |
| 24 | |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 25 | static void print_mac_address(struct seq_file *sfp, unsigned char *mac) |
| 26 | { |
| 27 | seq_printf(sfp, "%02x:%02x:%02x:%02x:%02x:%02x:", |
| 28 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 29 | } |
| 30 | |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 31 | /* hsr_node_table_show - Formats and prints node_table entries */ |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 32 | static int |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 33 | hsr_node_table_show(struct seq_file *sfp, void *data) |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 34 | { |
| 35 | struct hsr_priv *priv = (struct hsr_priv *)sfp->private; |
| 36 | struct hsr_node *node; |
| 37 | |
| 38 | seq_puts(sfp, "Node Table entries\n"); |
| 39 | seq_puts(sfp, "MAC-Address-A, MAC-Address-B, time_in[A], "); |
| 40 | seq_puts(sfp, "time_in[B], Address-B port\n"); |
| 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 | print_mac_address(sfp, &node->macaddress_A[0]); |
| 47 | seq_puts(sfp, " "); |
| 48 | print_mac_address(sfp, &node->macaddress_B[0]); |
| 49 | seq_printf(sfp, "0x%lx, ", node->time_in[HSR_PT_SLAVE_A]); |
| 50 | seq_printf(sfp, "0x%lx ", node->time_in[HSR_PT_SLAVE_B]); |
| 51 | seq_printf(sfp, "0x%x\n", node->addr_B_port); |
| 52 | } |
| 53 | rcu_read_unlock(); |
| 54 | return 0; |
| 55 | } |
| 56 | |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 57 | /* hsr_node_table_open - Open the node_table file |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 58 | * |
| 59 | * Description: |
| 60 | * This routine opens a debugfs file node_table of specific hsr device |
| 61 | */ |
| 62 | static int |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 63 | hsr_node_table_open(struct inode *inode, struct file *filp) |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 64 | { |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 65 | return single_open(filp, hsr_node_table_show, inode->i_private); |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Taehee Yoo | 4c2d5e3 | 2019-12-22 11:26:39 +0000 | [diff] [blame^] | 68 | void hsr_debugfs_rename(struct net_device *dev) |
| 69 | { |
| 70 | struct hsr_priv *priv = netdev_priv(dev); |
| 71 | struct dentry *d; |
| 72 | |
| 73 | d = debugfs_rename(hsr_debugfs_root_dir, priv->node_tbl_root, |
| 74 | hsr_debugfs_root_dir, dev->name); |
| 75 | if (IS_ERR(d)) |
| 76 | netdev_warn(dev, "failed to rename\n"); |
| 77 | else |
| 78 | priv->node_tbl_root = d; |
| 79 | } |
| 80 | |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 81 | static const struct file_operations hsr_fops = { |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 82 | .open = hsr_node_table_open, |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 83 | .read = seq_read, |
| 84 | .llseek = seq_lseek, |
| 85 | .release = single_release, |
| 86 | }; |
| 87 | |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 88 | /* hsr_debugfs_init - create hsr node_table file for dumping |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 89 | * the node table |
| 90 | * |
| 91 | * Description: |
| 92 | * When debugfs is configured this routine sets up the node_table file per |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 93 | * hsr device for dumping the node_table entries |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 94 | */ |
Taehee Yoo | 1d19e2d | 2019-12-22 11:26:15 +0000 | [diff] [blame] | 95 | void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev) |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 96 | { |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 97 | struct dentry *de = NULL; |
| 98 | |
Taehee Yoo | c6c4ccd | 2019-12-22 11:26:27 +0000 | [diff] [blame] | 99 | de = debugfs_create_dir(hsr_dev->name, hsr_debugfs_root_dir); |
Taehee Yoo | 1d19e2d | 2019-12-22 11:26:15 +0000 | [diff] [blame] | 100 | if (IS_ERR(de)) { |
Taehee Yoo | c6c4ccd | 2019-12-22 11:26:27 +0000 | [diff] [blame] | 101 | pr_err("Cannot create hsr debugfs directory\n"); |
Taehee Yoo | 1d19e2d | 2019-12-22 11:26:15 +0000 | [diff] [blame] | 102 | return; |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | priv->node_tbl_root = de; |
| 106 | |
| 107 | de = debugfs_create_file("node_table", S_IFREG | 0444, |
| 108 | priv->node_tbl_root, priv, |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 109 | &hsr_fops); |
Taehee Yoo | 1d19e2d | 2019-12-22 11:26:15 +0000 | [diff] [blame] | 110 | if (IS_ERR(de)) { |
Taehee Yoo | c6c4ccd | 2019-12-22 11:26:27 +0000 | [diff] [blame] | 111 | pr_err("Cannot create hsr node_table file\n"); |
Taehee Yoo | 1d19e2d | 2019-12-22 11:26:15 +0000 | [diff] [blame] | 112 | debugfs_remove(priv->node_tbl_root); |
| 113 | priv->node_tbl_root = NULL; |
| 114 | return; |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 115 | } |
| 116 | priv->node_tbl_file = de; |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 119 | /* hsr_debugfs_term - Tear down debugfs intrastructure |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 120 | * |
| 121 | * Description: |
| 122 | * When Debufs is configured this routine removes debugfs file system |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 123 | * elements that are specific to hsr |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 124 | */ |
| 125 | void |
Murali Karicheri | 9c5f8a19 | 2019-04-15 11:36:01 -0400 | [diff] [blame] | 126 | hsr_debugfs_term(struct hsr_priv *priv) |
Murali Karicheri | fc4ecae | 2019-04-05 13:31:35 -0400 | [diff] [blame] | 127 | { |
| 128 | debugfs_remove(priv->node_tbl_file); |
| 129 | priv->node_tbl_file = NULL; |
| 130 | debugfs_remove(priv->node_tbl_root); |
| 131 | priv->node_tbl_root = NULL; |
| 132 | } |
Taehee Yoo | c6c4ccd | 2019-12-22 11:26:27 +0000 | [diff] [blame] | 133 | |
| 134 | void hsr_debugfs_create_root(void) |
| 135 | { |
| 136 | hsr_debugfs_root_dir = debugfs_create_dir("hsr", NULL); |
| 137 | if (IS_ERR(hsr_debugfs_root_dir)) { |
| 138 | pr_err("Cannot create hsr debugfs root directory\n"); |
| 139 | hsr_debugfs_root_dir = NULL; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void hsr_debugfs_remove_root(void) |
| 144 | { |
| 145 | /* debugfs_remove() internally checks NULL and ERROR */ |
| 146 | debugfs_remove(hsr_debugfs_root_dir); |
| 147 | } |