1 /*
2  * Copyright (c) 2012 Broadcom Corporation
3  * Copyright (c) 2012 Canonical Ltd.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #include <linux/debugfs.h>
18 #include <linux/if_ether.h>
19 #include <linux/if.h>
20 #include <linux/net.h>
21 #include <linux/netdevice.h>
22 #include <linux/ieee80211.h>
23 #include <linux/module.h>
24 #include <net/mac80211.h>
25 
26 #include <defs.h>
27 #include <brcmu_wifi.h>
28 #include <brcmu_utils.h>
29 #include "types.h"
30 #include "main.h"
31 #include "debug.h"
32 #include "brcms_trace_events.h"
33 #include "phy/phy_int.h"
34 
35 static struct dentry *root_folder;
36 
37 void brcms_debugfs_init(void)
38 {
39 	root_folder = debugfs_create_dir(KBUILD_MODNAME, NULL);
40 	if (IS_ERR(root_folder))
41 		root_folder = NULL;
42 }
43 
44 void brcms_debugfs_exit(void)
45 {
46 	if (!root_folder)
47 		return;
48 
49 	debugfs_remove_recursive(root_folder);
50 	root_folder = NULL;
51 }
52 
53 int brcms_debugfs_attach(struct brcms_pub *drvr)
54 {
55 	if (!root_folder)
56 		return -ENODEV;
57 
58 	drvr->dbgfs_dir = debugfs_create_dir(
59 		 dev_name(&drvr->wlc->hw->d11core->dev), root_folder);
60 	return PTR_ERR_OR_ZERO(drvr->dbgfs_dir);
61 }
62 
63 void brcms_debugfs_detach(struct brcms_pub *drvr)
64 {
65 	debugfs_remove_recursive(drvr->dbgfs_dir);
66 }
67 
68 struct dentry *brcms_debugfs_get_devdir(struct brcms_pub *drvr)
69 {
70 	return drvr->dbgfs_dir;
71 }
72 
73 static
74 int brcms_debugfs_hardware_read(struct seq_file *s, void *data)
75 {
76 	struct brcms_pub *drvr = s->private;
77 	struct brcms_hardware *hw = drvr->wlc->hw;
78 	struct bcma_device *core = hw->d11core;
79 	struct bcma_bus *bus = core->bus;
80 	char boardrev[BRCMU_BOARDREV_LEN];
81 
82 	seq_printf(s, "chipnum 0x%x\n"
83 		   "chiprev 0x%x\n"
84 		   "chippackage 0x%x\n"
85 		   "corerev 0x%x\n"
86 		   "boardid 0x%x\n"
87 		   "boardvendor 0x%x\n"
88 		   "boardrev %s\n"
89 		   "boardflags 0x%x\n"
90 		   "boardflags2 0x%x\n"
91 		   "ucoderev 0x%x\n"
92 		   "radiorev 0x%x\n"
93 		   "phytype 0x%x\n"
94 		   "phyrev 0x%x\n"
95 		   "anarev 0x%x\n"
96 		   "nvramrev %d\n",
97 		   bus->chipinfo.id, bus->chipinfo.rev, bus->chipinfo.pkg,
98 		   core->id.rev, bus->boardinfo.type, bus->boardinfo.vendor,
99 		   brcmu_boardrev_str(hw->boardrev, boardrev),
100 		   drvr->wlc->hw->boardflags, drvr->wlc->hw->boardflags2,
101 		   drvr->wlc->ucode_rev, hw->band->radiorev,
102 		   hw->band->phytype, hw->band->phyrev, hw->band->pi->ana_rev,
103 		   hw->sromrev);
104 	return 0;
105 }
106 
107 static int brcms_debugfs_macstat_read(struct seq_file *s, void *data)
108 {
109 	struct brcms_pub *drvr = s->private;
110 	struct brcms_info *wl = drvr->ieee_hw->priv;
111 	struct macstat stats;
112 	int i;
113 
114 	spin_lock_bh(&wl->lock);
115 	stats = *(drvr->wlc->core->macstat_snapshot);
116 	spin_unlock_bh(&wl->lock);
117 
118 	seq_printf(s, "txallfrm: %d\n", stats.txallfrm);
119 	seq_printf(s, "txrtsfrm: %d\n", stats.txrtsfrm);
120 	seq_printf(s, "txctsfrm: %d\n", stats.txctsfrm);
121 	seq_printf(s, "txackfrm: %d\n", stats.txackfrm);
122 	seq_printf(s, "txdnlfrm: %d\n", stats.txdnlfrm);
123 	seq_printf(s, "txbcnfrm: %d\n", stats.txbcnfrm);
124 	seq_printf(s, "txfunfl[8]:");
125 	for (i = 0; i < ARRAY_SIZE(stats.txfunfl); i++)
126 		seq_printf(s, " %d", stats.txfunfl[i]);
127 	seq_printf(s, "\ntxtplunfl: %d\n", stats.txtplunfl);
128 	seq_printf(s, "txphyerr: %d\n", stats.txphyerr);
129 	seq_printf(s, "pktengrxducast: %d\n", stats.pktengrxducast);
130 	seq_printf(s, "pktengrxdmcast: %d\n", stats.pktengrxdmcast);
131 	seq_printf(s, "rxfrmtoolong: %d\n", stats.rxfrmtoolong);
132 	seq_printf(s, "rxfrmtooshrt: %d\n", stats.rxfrmtooshrt);
133 	seq_printf(s, "rxinvmachdr: %d\n", stats.rxinvmachdr);
134 	seq_printf(s, "rxbadfcs: %d\n", stats.rxbadfcs);
135 	seq_printf(s, "rxbadplcp: %d\n", stats.rxbadplcp);
136 	seq_printf(s, "rxcrsglitch: %d\n", stats.rxcrsglitch);
137 	seq_printf(s, "rxstrt: %d\n", stats.rxstrt);
138 	seq_printf(s, "rxdfrmucastmbss: %d\n", stats.rxdfrmucastmbss);
139 	seq_printf(s, "rxmfrmucastmbss: %d\n", stats.rxmfrmucastmbss);
140 	seq_printf(s, "rxcfrmucast: %d\n", stats.rxcfrmucast);
141 	seq_printf(s, "rxrtsucast: %d\n", stats.rxrtsucast);
142 	seq_printf(s, "rxctsucast: %d\n", stats.rxctsucast);
143 	seq_printf(s, "rxackucast: %d\n", stats.rxackucast);
144 	seq_printf(s, "rxdfrmocast: %d\n", stats.rxdfrmocast);
145 	seq_printf(s, "rxmfrmocast: %d\n", stats.rxmfrmocast);
146 	seq_printf(s, "rxcfrmocast: %d\n", stats.rxcfrmocast);
147 	seq_printf(s, "rxrtsocast: %d\n", stats.rxrtsocast);
148 	seq_printf(s, "rxctsocast: %d\n", stats.rxctsocast);
149 	seq_printf(s, "rxdfrmmcast: %d\n", stats.rxdfrmmcast);
150 	seq_printf(s, "rxmfrmmcast: %d\n", stats.rxmfrmmcast);
151 	seq_printf(s, "rxcfrmmcast: %d\n", stats.rxcfrmmcast);
152 	seq_printf(s, "rxbeaconmbss: %d\n", stats.rxbeaconmbss);
153 	seq_printf(s, "rxdfrmucastobss: %d\n", stats.rxdfrmucastobss);
154 	seq_printf(s, "rxbeaconobss: %d\n", stats.rxbeaconobss);
155 	seq_printf(s, "rxrsptmout: %d\n", stats.rxrsptmout);
156 	seq_printf(s, "bcntxcancl: %d\n", stats.bcntxcancl);
157 	seq_printf(s, "rxf0ovfl: %d\n", stats.rxf0ovfl);
158 	seq_printf(s, "rxf1ovfl: %d\n", stats.rxf1ovfl);
159 	seq_printf(s, "rxf2ovfl: %d\n", stats.rxf2ovfl);
160 	seq_printf(s, "txsfovfl: %d\n", stats.txsfovfl);
161 	seq_printf(s, "pmqovfl: %d\n", stats.pmqovfl);
162 	seq_printf(s, "rxcgprqfrm: %d\n", stats.rxcgprqfrm);
163 	seq_printf(s, "rxcgprsqovfl: %d\n", stats.rxcgprsqovfl);
164 	seq_printf(s, "txcgprsfail: %d\n", stats.txcgprsfail);
165 	seq_printf(s, "txcgprssuc: %d\n", stats.txcgprssuc);
166 	seq_printf(s, "prs_timeout: %d\n", stats.prs_timeout);
167 	seq_printf(s, "rxnack: %d\n", stats.rxnack);
168 	seq_printf(s, "frmscons: %d\n", stats.frmscons);
169 	seq_printf(s, "txnack: %d\n", stats.txnack);
170 	seq_printf(s, "txglitch_nack: %d\n", stats.txglitch_nack);
171 	seq_printf(s, "txburst: %d\n", stats.txburst);
172 	seq_printf(s, "bphy_rxcrsglitch: %d\n", stats.bphy_rxcrsglitch);
173 	seq_printf(s, "phywatchdog: %d\n", stats.phywatchdog);
174 	seq_printf(s, "bphy_badplcp: %d\n", stats.bphy_badplcp);
175 	return 0;
176 }
177 
178 struct brcms_debugfs_entry {
179 	int (*read)(struct seq_file *seq, void *data);
180 	struct brcms_pub *drvr;
181 };
182 
183 static int brcms_debugfs_entry_open(struct inode *inode, struct file *f)
184 {
185 	struct brcms_debugfs_entry *entry = inode->i_private;
186 
187 	return single_open(f, entry->read, entry->drvr);
188 }
189 
190 static const struct file_operations brcms_debugfs_def_ops = {
191 	.owner = THIS_MODULE,
192 	.open = brcms_debugfs_entry_open,
193 	.release = single_release,
194 	.read = seq_read,
195 	.llseek = seq_lseek
196 };
197 
198 static int
199 brcms_debugfs_add_entry(struct brcms_pub *drvr, const char *fn,
200 			int (*read_fn)(struct seq_file *seq, void *data))
201 {
202 	struct device *dev = &drvr->wlc->hw->d11core->dev;
203 	struct dentry *dentry =  drvr->dbgfs_dir;
204 	struct brcms_debugfs_entry *entry;
205 
206 	if (IS_ERR_OR_NULL(dentry))
207 		return -ENOENT;
208 
209 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
210 	if (!entry)
211 		return -ENOMEM;
212 
213 	entry->read = read_fn;
214 	entry->drvr = drvr;
215 
216 	dentry = debugfs_create_file(fn, 0444, dentry, entry,
217 				     &brcms_debugfs_def_ops);
218 
219 	return PTR_ERR_OR_ZERO(dentry);
220 }
221 
222 void brcms_debugfs_create_files(struct brcms_pub *drvr)
223 {
224 	if (IS_ERR_OR_NULL(drvr->dbgfs_dir))
225 		return;
226 
227 	brcms_debugfs_add_entry(drvr, "hardware", brcms_debugfs_hardware_read);
228 	brcms_debugfs_add_entry(drvr, "macstat", brcms_debugfs_macstat_read);
229 }
230 
231 #define __brcms_fn(fn)						\
232 void __brcms_ ##fn(struct device *dev, const char *fmt, ...)	\
233 {								\
234 	struct va_format vaf = {				\
235 		.fmt = fmt,					\
236 	};							\
237 	va_list args;						\
238 								\
239 	va_start(args, fmt);					\
240 	vaf.va = &args;						\
241 	dev_ ##fn(dev, "%pV", &vaf);				\
242 	trace_brcms_ ##fn(&vaf);				\
243 	va_end(args);						\
244 }
245 
246 __brcms_fn(info)
247 __brcms_fn(warn)
248 __brcms_fn(err)
249 __brcms_fn(crit)
250 
251 #if defined(CONFIG_BRCMDBG) || defined(CONFIG_BRCM_TRACING)
252 void __brcms_dbg(struct device *dev, u32 level, const char *func,
253 		 const char *fmt, ...)
254 {
255 	struct va_format vaf = {
256 		.fmt = fmt,
257 	};
258 	va_list args;
259 
260 	va_start(args, fmt);
261 	vaf.va = &args;
262 #ifdef CONFIG_BRCMDBG
263 	if ((brcm_msg_level & level) && net_ratelimit())
264 		dev_err(dev, "%s %pV", func, &vaf);
265 #endif
266 	trace_brcms_dbg(level, func, &vaf);
267 	va_end(args);
268 }
269 #endif
270