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 23 static struct dentry *hsr_debugfs_root_dir; 24 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 31 /* hsr_node_table_show - Formats and prints node_table entries */ 32 static int 33 hsr_node_table_show(struct seq_file *sfp, void *data) 34 { 35 struct hsr_priv *priv = (struct hsr_priv *)sfp->private; 36 struct hsr_node *node; 37 38 seq_printf(sfp, "Node Table entries for (%s) device\n", 39 (priv->prot_version == PRP_V1 ? "PRP" : "HSR")); 40 seq_puts(sfp, "MAC-Address-A, MAC-Address-B, time_in[A], "); 41 seq_puts(sfp, "time_in[B], Address-B port, "); 42 if (priv->prot_version == PRP_V1) 43 seq_puts(sfp, "SAN-A, SAN-B, DAN-P\n"); 44 else 45 seq_puts(sfp, "DAN-H\n"); 46 47 rcu_read_lock(); 48 list_for_each_entry_rcu(node, &priv->node_db, mac_list) { 49 /* skip self node */ 50 if (hsr_addr_is_self(priv, node->macaddress_A)) 51 continue; 52 print_mac_address(sfp, &node->macaddress_A[0]); 53 print_mac_address(sfp, &node->macaddress_B[0]); 54 seq_printf(sfp, "%10lx, ", node->time_in[HSR_PT_SLAVE_A]); 55 seq_printf(sfp, "%10lx, ", 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 && node->san_b == 0)); 62 else 63 seq_printf(sfp, "%5x\n", 1); 64 } 65 rcu_read_unlock(); 66 return 0; 67 } 68 69 /* hsr_node_table_open - Open the node_table file 70 * 71 * Description: 72 * This routine opens a debugfs file node_table of specific hsr 73 * or prp device 74 */ 75 static int 76 hsr_node_table_open(struct inode *inode, struct file *filp) 77 { 78 return single_open(filp, hsr_node_table_show, inode->i_private); 79 } 80 81 void hsr_debugfs_rename(struct net_device *dev) 82 { 83 struct hsr_priv *priv = netdev_priv(dev); 84 struct dentry *d; 85 86 d = debugfs_rename(hsr_debugfs_root_dir, priv->node_tbl_root, 87 hsr_debugfs_root_dir, dev->name); 88 if (IS_ERR(d)) 89 netdev_warn(dev, "failed to rename\n"); 90 else 91 priv->node_tbl_root = d; 92 } 93 94 static const struct file_operations hsr_fops = { 95 .open = hsr_node_table_open, 96 .read = seq_read, 97 .llseek = seq_lseek, 98 .release = single_release, 99 }; 100 101 /* hsr_debugfs_init - create hsr node_table file for dumping 102 * the node table 103 * 104 * Description: 105 * When debugfs is configured this routine sets up the node_table file per 106 * hsr device for dumping the node_table entries 107 */ 108 void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev) 109 { 110 struct dentry *de = NULL; 111 112 de = debugfs_create_dir(hsr_dev->name, hsr_debugfs_root_dir); 113 if (IS_ERR(de)) { 114 pr_err("Cannot create hsr debugfs directory\n"); 115 return; 116 } 117 118 priv->node_tbl_root = de; 119 120 de = debugfs_create_file("node_table", S_IFREG | 0444, 121 priv->node_tbl_root, priv, 122 &hsr_fops); 123 if (IS_ERR(de)) { 124 pr_err("Cannot create hsr node_table file\n"); 125 debugfs_remove(priv->node_tbl_root); 126 priv->node_tbl_root = NULL; 127 return; 128 } 129 } 130 131 /* hsr_debugfs_term - Tear down debugfs intrastructure 132 * 133 * Description: 134 * When Debufs is configured this routine removes debugfs file system 135 * elements that are specific to hsr 136 */ 137 void 138 hsr_debugfs_term(struct hsr_priv *priv) 139 { 140 debugfs_remove_recursive(priv->node_tbl_root); 141 priv->node_tbl_root = NULL; 142 } 143 144 void hsr_debugfs_create_root(void) 145 { 146 hsr_debugfs_root_dir = debugfs_create_dir("hsr", NULL); 147 if (IS_ERR(hsr_debugfs_root_dir)) { 148 pr_err("Cannot create hsr debugfs root directory\n"); 149 hsr_debugfs_root_dir = NULL; 150 } 151 } 152 153 void hsr_debugfs_remove_root(void) 154 { 155 /* debugfs_remove() internally checks NULL and ERROR */ 156 debugfs_remove(hsr_debugfs_root_dir); 157 } 158