hsr_prp_debugfs.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * hsr_prp_debugfs code
  3. * Copyright (C) 2017 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_prp_main.h"
  21. #include "hsr_prp_framereg.h"
  22. static void print_mac_address(struct seq_file *sfp, unsigned char *mac)
  23. {
  24. seq_printf(sfp, "%02x:%02x:%02x:%02x:%02x:%02x:",
  25. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  26. }
  27. /* hsr_prp_node_table_show - Formats and prints node_table entries
  28. */
  29. static int
  30. hsr_prp_node_table_show(struct seq_file *sfp, void *data)
  31. {
  32. struct hsr_prp_priv *priv = (struct hsr_prp_priv *)sfp->private;
  33. struct hsr_prp_node *node;
  34. seq_puts(sfp, "Node Table entries\n");
  35. seq_puts(sfp, "MAC-Address-A, MAC-Address-B, time_in[A], ");
  36. seq_puts(sfp, "time_in[B], Address-B port");
  37. if (priv->prot_version == PRP_V1)
  38. seq_puts(sfp, ", san_a, san_b\n");
  39. else
  40. seq_puts(sfp, "\n");
  41. rcu_read_lock();
  42. list_for_each_entry_rcu(node, &priv->node_db, mac_list) {
  43. /* skip self node */
  44. if (hsr_prp_addr_is_self(priv, node->mac_address_a))
  45. continue;
  46. print_mac_address(sfp, &node->mac_address_a[0]);
  47. seq_puts(sfp, " ");
  48. print_mac_address(sfp, &node->mac_address_b[0]);
  49. seq_printf(sfp, "0x%lx, ", node->time_in[HSR_PRP_PT_SLAVE_A]);
  50. seq_printf(sfp, "0x%lx ", node->time_in[HSR_PRP_PT_SLAVE_B]);
  51. seq_printf(sfp, "0x%x", node->addr_b_port);
  52. if (priv->prot_version == PRP_V1)
  53. seq_printf(sfp, ", %x, %x\n", node->san_a, node->san_b);
  54. else
  55. seq_puts(sfp, "\n");
  56. }
  57. rcu_read_unlock();
  58. return 0;
  59. }
  60. /* hsr_prp_node_table_open - Open the node_table file
  61. *
  62. * Description:
  63. * This routine opens a debugfs file node_table of specific hsr
  64. * or prp device
  65. */
  66. static int
  67. hsr_prp_node_table_open(struct inode *inode, struct file *filp)
  68. {
  69. return single_open(filp, hsr_prp_node_table_show, inode->i_private);
  70. }
  71. static const struct file_operations hsr_prp_node_table_fops = {
  72. .owner = THIS_MODULE,
  73. .open = hsr_prp_node_table_open,
  74. .read = seq_read,
  75. .llseek = seq_lseek,
  76. .release = single_release,
  77. };
  78. /* hsr_prp_stats_show - Formats and prints stats in the device
  79. */
  80. static int
  81. hsr_prp_stats_show(struct seq_file *sfp, void *data)
  82. {
  83. struct hsr_prp_priv *priv = (struct hsr_prp_priv *)sfp->private;
  84. struct hsr_prp_port *master;
  85. rcu_read_lock();
  86. master = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
  87. rcu_read_unlock();
  88. seq_puts(sfp, "LRE Stats entries\n");
  89. seq_printf(sfp, "cnt_tx_a = %d\n", priv->stats.cnt_tx_a);
  90. seq_printf(sfp, "cnt_tx_b = %d\n", priv->stats.cnt_tx_b);
  91. /* actually lre_tx_c is whatever sent to the application interface. So
  92. * same as rx_packets
  93. */
  94. seq_printf(sfp, "cnt_tx_c = %ld\n", master->dev->stats.rx_packets);
  95. seq_printf(sfp, "cnt_tx_sup = %d\n", priv->stats.cnt_tx_sup);
  96. seq_printf(sfp, "cnt_rx_wrong_lan_a = %d\n",
  97. priv->stats.cnt_rx_wrong_lan_a);
  98. seq_printf(sfp, "cnt_rx_wrong_lan_b = %d\n",
  99. priv->stats.cnt_rx_wrong_lan_b);
  100. seq_printf(sfp, "cnt_rx_a = %d\n", priv->stats.cnt_rx_a);
  101. seq_printf(sfp, "cnt_rx_b = %d\n", priv->stats.cnt_rx_b);
  102. /* actually lre_rx_c is whatever received from the application
  103. * interface, So same as tx_packets
  104. */
  105. seq_printf(sfp, "cnt_rx_c = %ld\n", master->dev->stats.tx_packets);
  106. seq_printf(sfp, "cnt_rx_errors_a = %d\n", priv->stats.cnt_rx_errors_a);
  107. seq_printf(sfp, "cnt_rx_errors_b = %d\n", priv->stats.cnt_rx_errors_b);
  108. if (priv->prot_version <= HSR_V1) {
  109. seq_printf(sfp, "cnt_own_rx_a = %d\n",
  110. priv->stats.cnt_own_rx_a);
  111. seq_printf(sfp, "cnt_own_rx_b = %d\n",
  112. priv->stats.cnt_own_rx_b);
  113. }
  114. seq_puts(sfp, "\n");
  115. return 0;
  116. }
  117. /* hsr_prp_stats_open - open stats file
  118. *
  119. * Description:
  120. * This routine opens a debugfs file stats of specific hsr or
  121. * prp device
  122. */
  123. static int
  124. hsr_prp_stats_open(struct inode *inode, struct file *filp)
  125. {
  126. return single_open(filp, hsr_prp_stats_show, inode->i_private);
  127. }
  128. static const struct file_operations hsr_prp_stats_fops = {
  129. .owner = THIS_MODULE,
  130. .open = hsr_prp_stats_open,
  131. .read = seq_read,
  132. .llseek = seq_lseek,
  133. .release = single_release,
  134. };
  135. /* dd_mode_show - print the value of duplicate_discard debugfs file
  136. * for prp device
  137. */
  138. static int
  139. dd_mode_show(struct seq_file *sfp, void *data)
  140. {
  141. struct hsr_prp_priv *priv = (struct hsr_prp_priv *)sfp->private;
  142. seq_printf(sfp, "%u\n", priv->dup_discard_mode);
  143. return 0;
  144. }
  145. /* dd_mode_write - write the user provided value to duplicate_discard debugfs
  146. * file
  147. */
  148. static ssize_t
  149. dd_mode_write(struct file *file, const char __user *user_buf,
  150. size_t count, loff_t *ppos)
  151. {
  152. struct hsr_prp_priv *priv =
  153. ((struct seq_file *)(file->private_data))->private;
  154. unsigned long mode;
  155. int err;
  156. err = kstrtoul_from_user(user_buf, count, 0, &mode);
  157. if (err)
  158. return err;
  159. if (priv->dup_discard_mode < IEC62439_3_PRP_DA ||
  160. priv->dup_discard_mode > IEC62439_3_PRP_DD)
  161. return -EINVAL;
  162. priv->dup_discard_mode = mode;
  163. return count;
  164. }
  165. /* dd_mode_open - Open the duplicate discard mode debugfs file
  166. *
  167. * Description:
  168. * This routine opens a debugfs file duplicate_discard for prp device
  169. */
  170. static int
  171. dd_mode_open(struct inode *inode, struct file *filp)
  172. {
  173. return single_open(filp, dd_mode_show, inode->i_private);
  174. }
  175. static const struct file_operations dd_mode_fops = {
  176. .owner = THIS_MODULE,
  177. .open = dd_mode_open,
  178. .read = seq_read,
  179. .write = dd_mode_write,
  180. .llseek = seq_lseek,
  181. .release = single_release,
  182. };
  183. /* hsr_mode_show - print the value of hsr_mode debugfs file
  184. * for hsr device
  185. */
  186. static int
  187. hsr_mode_show(struct seq_file *sfp, void *data)
  188. {
  189. struct hsr_prp_priv *priv = (struct hsr_prp_priv *)sfp->private;
  190. seq_printf(sfp, "%u\n", priv->hsr_mode);
  191. return 0;
  192. }
  193. /* hsr_mode_write - write the user provided value to
  194. * hsr_mode debugfs file
  195. */
  196. static ssize_t
  197. hsr_mode_write(struct file *file, const char __user *user_buf,
  198. size_t count, loff_t *ppos)
  199. {
  200. struct hsr_prp_priv *priv =
  201. ((struct seq_file *)(file->private_data))->private;
  202. unsigned long mode;
  203. int err;
  204. err = kstrtoul_from_user(user_buf, count, 0, &mode);
  205. if (err)
  206. return err;
  207. /* Support mode change only if offloaded as more change
  208. * is needed to support non offloaded case
  209. */
  210. if (!(priv->rx_offloaded && priv->l2_fwd_offloaded))
  211. return -EPERM;
  212. if (mode < IEC62439_3_HSR_MODE_H || mode > IEC62439_3_HSR_MODE_M)
  213. return -EINVAL;
  214. priv->hsr_mode = mode;
  215. return count;
  216. }
  217. /* hsr_mode_open - Open the hsr_mode debugfs file
  218. *
  219. * Description:
  220. * This routine opens a debugfs file hsr_mode for hsr device
  221. */
  222. static int
  223. hsr_mode_open(struct inode *inode, struct file *filp)
  224. {
  225. return single_open(filp, hsr_mode_show, inode->i_private);
  226. }
  227. static const struct file_operations hsr_mode_fops = {
  228. .owner = THIS_MODULE,
  229. .open = hsr_mode_open,
  230. .read = seq_read,
  231. .write = hsr_mode_write,
  232. .llseek = seq_lseek,
  233. .release = single_release,
  234. };
  235. /* hsr_prp_debugfs_init - create hsr-prp node_table file for dumping
  236. * the node table
  237. *
  238. * Description:
  239. * When debugfs is configured this routine sets up the node_table file per
  240. * hsr/prp device for dumping the node_table entries
  241. */
  242. int hsr_prp_debugfs_init(struct hsr_prp_priv *priv,
  243. struct net_device *hsr_prp_dev)
  244. {
  245. int rc = -1;
  246. struct dentry *de = NULL;
  247. #ifdef TODO
  248. de = debugfs_create_dir(hsr_prp_dev->name, NULL);
  249. #else
  250. if (priv->prot_version <= HSR_V1)
  251. de = debugfs_create_dir("hsr", NULL);
  252. else
  253. de = debugfs_create_dir("prp", NULL);
  254. #endif
  255. if (!de) {
  256. netdev_err(hsr_prp_dev, "Cannot create hsr-prp debugfs root\n");
  257. return rc;
  258. }
  259. priv->root_dir = de;
  260. de = debugfs_create_file("node_table", S_IFREG | S_IRUGO,
  261. priv->root_dir, priv,
  262. &hsr_prp_node_table_fops);
  263. if (!de) {
  264. netdev_err(hsr_prp_dev,
  265. "Cannot create hsr-prp node_table directory\n");
  266. return rc;
  267. }
  268. priv->node_tbl_file = de;
  269. de = debugfs_create_file("stats", S_IFREG | S_IRUGO,
  270. priv->root_dir, priv,
  271. &hsr_prp_stats_fops);
  272. if (!de) {
  273. netdev_err(hsr_prp_dev,
  274. "Cannot create hsr-prp stats file\n");
  275. return rc;
  276. }
  277. priv->stats_file = de;
  278. if (priv->prot_version == HSR_V1) {
  279. de = debugfs_create_file("hsr_mode", 0444,
  280. priv->root_dir, priv,
  281. &hsr_mode_fops);
  282. if (!de) {
  283. netdev_err(hsr_prp_dev,
  284. "Cannot create hsr-prp hsr_mode file\n");
  285. return rc;
  286. }
  287. priv->hsr_mode_file = de;
  288. }
  289. if (priv->prot_version == PRP_V1) {
  290. de = debugfs_create_file("duplicate_discard", 0444,
  291. priv->root_dir, priv,
  292. &dd_mode_fops);
  293. if (!de) {
  294. netdev_err(hsr_prp_dev,
  295. "Can't create duplcate_discard mode file\n");
  296. return rc;
  297. }
  298. priv->dd_mode_file = de;
  299. }
  300. return 0;
  301. } /* end of hst_prp_debugfs_init */
  302. /* hsr_prp_debugfs_term - Tear down debugfs intrastructure
  303. *
  304. * Description:
  305. * When Debufs is configured this routine removes debugfs file system
  306. * elements that are specific to hsr-prp
  307. */
  308. void
  309. hsr_prp_debugfs_term(struct hsr_prp_priv *priv)
  310. {
  311. debugfs_remove(priv->node_tbl_file);
  312. priv->node_tbl_file = NULL;
  313. debugfs_remove(priv->stats_file);
  314. priv->stats_file = NULL;
  315. if (priv->prot_version == HSR_V1)
  316. debugfs_remove(priv->hsr_mode_file);
  317. priv->hsr_mode_file = NULL;
  318. if (priv->prot_version == PRP_V1)
  319. debugfs_remove(priv->dd_mode_file);
  320. priv->dd_mode_file = NULL;
  321. debugfs_remove(priv->root_dir);
  322. priv->root_dir = NULL;
  323. }