xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_debugfs.c (revision 975ef7ff81bb000af6e6c8e63e81f89f3468dcf7)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2017-2018 Broadcom Limited
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/debugfs.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include "bnxt_hsi.h"
14 #include <linux/net_dim.h>
15 #include "bnxt.h"
16 #include "bnxt_debugfs.h"
17 
18 static struct dentry *bnxt_debug_mnt;
19 
20 static ssize_t debugfs_dim_read(struct file *filep,
21 				char __user *buffer,
22 				size_t count, loff_t *ppos)
23 {
24 	struct net_dim *dim = filep->private_data;
25 	int len;
26 	char *buf;
27 
28 	if (*ppos)
29 		return 0;
30 	if (!dim)
31 		return -ENODEV;
32 	buf = kasprintf(GFP_KERNEL,
33 			"state = %d\n" \
34 			"profile_ix = %d\n" \
35 			"mode = %d\n" \
36 			"tune_state = %d\n" \
37 			"steps_right = %d\n" \
38 			"steps_left = %d\n" \
39 			"tired = %d\n",
40 			dim->state,
41 			dim->profile_ix,
42 			dim->mode,
43 			dim->tune_state,
44 			dim->steps_right,
45 			dim->steps_left,
46 			dim->tired);
47 	if (!buf)
48 		return -ENOMEM;
49 	if (count < strlen(buf)) {
50 		kfree(buf);
51 		return -ENOSPC;
52 	}
53 	len = simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
54 	kfree(buf);
55 	return len;
56 }
57 
58 static const struct file_operations debugfs_dim_fops = {
59 	.owner = THIS_MODULE,
60 	.open = simple_open,
61 	.read = debugfs_dim_read,
62 };
63 
64 static struct dentry *debugfs_dim_ring_init(struct net_dim *dim, int ring_idx,
65 					    struct dentry *dd)
66 {
67 	static char qname[16];
68 
69 	snprintf(qname, 10, "%d", ring_idx);
70 	return debugfs_create_file(qname, 0600, dd,
71 				   dim, &debugfs_dim_fops);
72 }
73 
74 void bnxt_debug_dev_init(struct bnxt *bp)
75 {
76 	const char *pname = pci_name(bp->pdev);
77 	struct dentry *pdevf;
78 	int i;
79 
80 	bp->debugfs_pdev = debugfs_create_dir(pname, bnxt_debug_mnt);
81 	if (bp->debugfs_pdev) {
82 		pdevf = debugfs_create_dir("dim", bp->debugfs_pdev);
83 		if (!pdevf) {
84 			pr_err("failed to create debugfs entry %s/dim\n",
85 			       pname);
86 			return;
87 		}
88 		bp->debugfs_dim = pdevf;
89 		/* create files for each rx ring */
90 		for (i = 0; i < bp->cp_nr_rings; i++) {
91 			struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
92 
93 			if (cpr && bp->bnapi[i]->rx_ring) {
94 				pdevf = debugfs_dim_ring_init(&cpr->dim, i,
95 							      bp->debugfs_dim);
96 				if (!pdevf)
97 					pr_err("failed to create debugfs entry %s/dim/%d\n",
98 					       pname, i);
99 			}
100 		}
101 	} else {
102 		pr_err("failed to create debugfs entry %s\n", pname);
103 	}
104 }
105 
106 void bnxt_debug_dev_exit(struct bnxt *bp)
107 {
108 	if (bp) {
109 		debugfs_remove_recursive(bp->debugfs_pdev);
110 		bp->debugfs_pdev = NULL;
111 	}
112 }
113 
114 void bnxt_debug_init(void)
115 {
116 	bnxt_debug_mnt = debugfs_create_dir("bnxt_en", NULL);
117 	if (!bnxt_debug_mnt)
118 		pr_err("failed to init bnxt_en debugfs\n");
119 }
120 
121 void bnxt_debug_exit(void)
122 {
123 	debugfs_remove_recursive(bnxt_debug_mnt);
124 }
125