xref: /openbmc/linux/drivers/net/wireless/ath/wil6210/debugfs.c (revision 74997a53d257e327699e359b78b3ecfd33f80cab)
1 /*
2  * Copyright (c) 2012-2016 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/module.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20 #include <linux/pci.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/power_supply.h>
23 
24 #include "wil6210.h"
25 #include "wmi.h"
26 #include "txrx.h"
27 #include "pmc.h"
28 
29 /* Nasty hack. Better have per device instances */
30 static u32 mem_addr;
31 static u32 dbg_txdesc_index;
32 static u32 dbg_vring_index; /* 24+ for Rx, 0..23 for Tx */
33 u32 vring_idle_trsh = 16; /* HW fetches up to 16 descriptors at once */
34 
35 enum dbg_off_type {
36 	doff_u32 = 0,
37 	doff_x32 = 1,
38 	doff_ulong = 2,
39 	doff_io32 = 3,
40 	doff_u8 = 4
41 };
42 
43 /* offset to "wil" */
44 struct dbg_off {
45 	const char *name;
46 	umode_t mode;
47 	ulong off;
48 	enum dbg_off_type type;
49 };
50 
51 static void wil_print_vring(struct seq_file *s, struct wil6210_priv *wil,
52 			    const char *name, struct vring *vring,
53 			    char _s, char _h)
54 {
55 	void __iomem *x = wmi_addr(wil, vring->hwtail);
56 	u32 v;
57 
58 	seq_printf(s, "VRING %s = {\n", name);
59 	seq_printf(s, "  pa     = %pad\n", &vring->pa);
60 	seq_printf(s, "  va     = 0x%p\n", vring->va);
61 	seq_printf(s, "  size   = %d\n", vring->size);
62 	seq_printf(s, "  swtail = %d\n", vring->swtail);
63 	seq_printf(s, "  swhead = %d\n", vring->swhead);
64 	seq_printf(s, "  hwtail = [0x%08x] -> ", vring->hwtail);
65 	if (x) {
66 		v = readl(x);
67 		seq_printf(s, "0x%08x = %d\n", v, v);
68 	} else {
69 		seq_puts(s, "???\n");
70 	}
71 
72 	if (vring->va && (vring->size <= (1 << WIL_RING_SIZE_ORDER_MAX))) {
73 		uint i;
74 
75 		for (i = 0; i < vring->size; i++) {
76 			volatile struct vring_tx_desc *d = &vring->va[i].tx;
77 
78 			if ((i % 128) == 0 && (i != 0))
79 				seq_puts(s, "\n");
80 			seq_printf(s, "%c", (d->dma.status & BIT(0)) ?
81 					_s : (vring->ctx[i].skb ? _h : 'h'));
82 		}
83 		seq_puts(s, "\n");
84 	}
85 	seq_puts(s, "}\n");
86 }
87 
88 static int wil_vring_debugfs_show(struct seq_file *s, void *data)
89 {
90 	uint i;
91 	struct wil6210_priv *wil = s->private;
92 
93 	wil_print_vring(s, wil, "rx", &wil->vring_rx, 'S', '_');
94 
95 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
96 		struct vring *vring = &wil->vring_tx[i];
97 		struct vring_tx_data *txdata = &wil->vring_tx_data[i];
98 
99 		if (vring->va) {
100 			int cid = wil->vring2cid_tid[i][0];
101 			int tid = wil->vring2cid_tid[i][1];
102 			u32 swhead = vring->swhead;
103 			u32 swtail = vring->swtail;
104 			int used = (vring->size + swhead - swtail)
105 				   % vring->size;
106 			int avail = vring->size - used - 1;
107 			char name[10];
108 			char sidle[10];
109 			/* performance monitoring */
110 			cycles_t now = get_cycles();
111 			uint64_t idle = txdata->idle * 100;
112 			uint64_t total = now - txdata->begin;
113 
114 			if (total != 0) {
115 				do_div(idle, total);
116 				snprintf(sidle, sizeof(sidle), "%3d%%",
117 					 (int)idle);
118 			} else {
119 				snprintf(sidle, sizeof(sidle), "N/A");
120 			}
121 			txdata->begin = now;
122 			txdata->idle = 0ULL;
123 
124 			snprintf(name, sizeof(name), "tx_%2d", i);
125 
126 			if (cid < WIL6210_MAX_CID)
127 				seq_printf(s,
128 					   "\n%pM CID %d TID %d 1x%s BACK([%u] %u TU A%s) [%3d|%3d] idle %s\n",
129 					   wil->sta[cid].addr, cid, tid,
130 					   txdata->dot1x_open ? "+" : "-",
131 					   txdata->agg_wsize,
132 					   txdata->agg_timeout,
133 					   txdata->agg_amsdu ? "+" : "-",
134 					   used, avail, sidle);
135 			else
136 				seq_printf(s,
137 					   "\nBroadcast 1x%s [%3d|%3d] idle %s\n",
138 					   txdata->dot1x_open ? "+" : "-",
139 					   used, avail, sidle);
140 
141 			wil_print_vring(s, wil, name, vring, '_', 'H');
142 		}
143 	}
144 
145 	return 0;
146 }
147 
148 static int wil_vring_seq_open(struct inode *inode, struct file *file)
149 {
150 	return single_open(file, wil_vring_debugfs_show, inode->i_private);
151 }
152 
153 static const struct file_operations fops_vring = {
154 	.open		= wil_vring_seq_open,
155 	.release	= single_release,
156 	.read		= seq_read,
157 	.llseek		= seq_lseek,
158 };
159 
160 static void wil_seq_hexdump(struct seq_file *s, void *p, int len,
161 			    const char *prefix)
162 {
163 	seq_hex_dump(s, prefix, DUMP_PREFIX_NONE, 16, 1, p, len, false);
164 }
165 
166 static void wil_print_ring(struct seq_file *s, const char *prefix,
167 			   void __iomem *off)
168 {
169 	struct wil6210_priv *wil = s->private;
170 	struct wil6210_mbox_ring r;
171 	int rsize;
172 	uint i;
173 
174 	wil_memcpy_fromio_32(&r, off, sizeof(r));
175 	wil_mbox_ring_le2cpus(&r);
176 	/*
177 	 * we just read memory block from NIC. This memory may be
178 	 * garbage. Check validity before using it.
179 	 */
180 	rsize = r.size / sizeof(struct wil6210_mbox_ring_desc);
181 
182 	seq_printf(s, "ring %s = {\n", prefix);
183 	seq_printf(s, "  base = 0x%08x\n", r.base);
184 	seq_printf(s, "  size = 0x%04x bytes -> %d entries\n", r.size, rsize);
185 	seq_printf(s, "  tail = 0x%08x\n", r.tail);
186 	seq_printf(s, "  head = 0x%08x\n", r.head);
187 	seq_printf(s, "  entry size = %d\n", r.entry_size);
188 
189 	if (r.size % sizeof(struct wil6210_mbox_ring_desc)) {
190 		seq_printf(s, "  ??? size is not multiple of %zd, garbage?\n",
191 			   sizeof(struct wil6210_mbox_ring_desc));
192 		goto out;
193 	}
194 
195 	if (!wmi_addr(wil, r.base) ||
196 	    !wmi_addr(wil, r.tail) ||
197 	    !wmi_addr(wil, r.head)) {
198 		seq_puts(s, "  ??? pointers are garbage?\n");
199 		goto out;
200 	}
201 
202 	for (i = 0; i < rsize; i++) {
203 		struct wil6210_mbox_ring_desc d;
204 		struct wil6210_mbox_hdr hdr;
205 		size_t delta = i * sizeof(d);
206 		void __iomem *x = wil->csr + HOSTADDR(r.base) + delta;
207 
208 		wil_memcpy_fromio_32(&d, x, sizeof(d));
209 
210 		seq_printf(s, "  [%2x] %s %s%s 0x%08x", i,
211 			   d.sync ? "F" : "E",
212 			   (r.tail - r.base == delta) ? "t" : " ",
213 			   (r.head - r.base == delta) ? "h" : " ",
214 			   le32_to_cpu(d.addr));
215 		if (0 == wmi_read_hdr(wil, d.addr, &hdr)) {
216 			u16 len = le16_to_cpu(hdr.len);
217 
218 			seq_printf(s, " -> %04x %04x %04x %02x\n",
219 				   le16_to_cpu(hdr.seq), len,
220 				   le16_to_cpu(hdr.type), hdr.flags);
221 			if (len <= MAX_MBOXITEM_SIZE) {
222 				unsigned char databuf[MAX_MBOXITEM_SIZE];
223 				void __iomem *src = wmi_buffer(wil, d.addr) +
224 					sizeof(struct wil6210_mbox_hdr);
225 				/*
226 				 * No need to check @src for validity -
227 				 * we already validated @d.addr while
228 				 * reading header
229 				 */
230 				wil_memcpy_fromio_32(databuf, src, len);
231 				wil_seq_hexdump(s, databuf, len, "      : ");
232 			}
233 		} else {
234 			seq_puts(s, "\n");
235 		}
236 	}
237  out:
238 	seq_puts(s, "}\n");
239 }
240 
241 static int wil_mbox_debugfs_show(struct seq_file *s, void *data)
242 {
243 	struct wil6210_priv *wil = s->private;
244 
245 	wil_print_ring(s, "tx", wil->csr + HOST_MBOX +
246 		       offsetof(struct wil6210_mbox_ctl, tx));
247 	wil_print_ring(s, "rx", wil->csr + HOST_MBOX +
248 		       offsetof(struct wil6210_mbox_ctl, rx));
249 
250 	return 0;
251 }
252 
253 static int wil_mbox_seq_open(struct inode *inode, struct file *file)
254 {
255 	return single_open(file, wil_mbox_debugfs_show, inode->i_private);
256 }
257 
258 static const struct file_operations fops_mbox = {
259 	.open		= wil_mbox_seq_open,
260 	.release	= single_release,
261 	.read		= seq_read,
262 	.llseek		= seq_lseek,
263 };
264 
265 static int wil_debugfs_iomem_x32_set(void *data, u64 val)
266 {
267 	writel(val, (void __iomem *)data);
268 	wmb(); /* make sure write propagated to HW */
269 
270 	return 0;
271 }
272 
273 static int wil_debugfs_iomem_x32_get(void *data, u64 *val)
274 {
275 	*val = readl((void __iomem *)data);
276 
277 	return 0;
278 }
279 
280 DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, wil_debugfs_iomem_x32_get,
281 			wil_debugfs_iomem_x32_set, "0x%08llx\n");
282 
283 static struct dentry *wil_debugfs_create_iomem_x32(const char *name,
284 						   umode_t mode,
285 						   struct dentry *parent,
286 						   void *value)
287 {
288 	return debugfs_create_file(name, mode, parent, value,
289 				   &fops_iomem_x32);
290 }
291 
292 static int wil_debugfs_ulong_set(void *data, u64 val)
293 {
294 	*(ulong *)data = val;
295 	return 0;
296 }
297 
298 static int wil_debugfs_ulong_get(void *data, u64 *val)
299 {
300 	*val = *(ulong *)data;
301 	return 0;
302 }
303 
304 DEFINE_SIMPLE_ATTRIBUTE(wil_fops_ulong, wil_debugfs_ulong_get,
305 			wil_debugfs_ulong_set, "0x%llx\n");
306 
307 static struct dentry *wil_debugfs_create_ulong(const char *name, umode_t mode,
308 					       struct dentry *parent,
309 					       ulong *value)
310 {
311 	return debugfs_create_file(name, mode, parent, value, &wil_fops_ulong);
312 }
313 
314 /**
315  * wil6210_debugfs_init_offset - create set of debugfs files
316  * @wil - driver's context, used for printing
317  * @dbg - directory on the debugfs, where files will be created
318  * @base - base address used in address calculation
319  * @tbl - table with file descriptions. Should be terminated with empty element.
320  *
321  * Creates files accordingly to the @tbl.
322  */
323 static void wil6210_debugfs_init_offset(struct wil6210_priv *wil,
324 					struct dentry *dbg, void *base,
325 					const struct dbg_off * const tbl)
326 {
327 	int i;
328 
329 	for (i = 0; tbl[i].name; i++) {
330 		struct dentry *f;
331 
332 		switch (tbl[i].type) {
333 		case doff_u32:
334 			f = debugfs_create_u32(tbl[i].name, tbl[i].mode, dbg,
335 					       base + tbl[i].off);
336 			break;
337 		case doff_x32:
338 			f = debugfs_create_x32(tbl[i].name, tbl[i].mode, dbg,
339 					       base + tbl[i].off);
340 			break;
341 		case doff_ulong:
342 			f = wil_debugfs_create_ulong(tbl[i].name, tbl[i].mode,
343 						     dbg, base + tbl[i].off);
344 			break;
345 		case doff_io32:
346 			f = wil_debugfs_create_iomem_x32(tbl[i].name,
347 							 tbl[i].mode, dbg,
348 							 base + tbl[i].off);
349 			break;
350 		case doff_u8:
351 			f = debugfs_create_u8(tbl[i].name, tbl[i].mode, dbg,
352 					      base + tbl[i].off);
353 			break;
354 		default:
355 			f = ERR_PTR(-EINVAL);
356 		}
357 		if (IS_ERR_OR_NULL(f))
358 			wil_err(wil, "Create file \"%s\": err %ld\n",
359 				tbl[i].name, PTR_ERR(f));
360 	}
361 }
362 
363 static const struct dbg_off isr_off[] = {
364 	{"ICC", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICC), doff_io32},
365 	{"ICR", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICR), doff_io32},
366 	{"ICM", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICM), doff_io32},
367 	{"ICS",		  S_IWUSR, offsetof(struct RGF_ICR, ICS), doff_io32},
368 	{"IMV", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, IMV), doff_io32},
369 	{"IMS",		  S_IWUSR, offsetof(struct RGF_ICR, IMS), doff_io32},
370 	{"IMC",		  S_IWUSR, offsetof(struct RGF_ICR, IMC), doff_io32},
371 	{},
372 };
373 
374 static int wil6210_debugfs_create_ISR(struct wil6210_priv *wil,
375 				      const char *name,
376 				      struct dentry *parent, u32 off)
377 {
378 	struct dentry *d = debugfs_create_dir(name, parent);
379 
380 	if (IS_ERR_OR_NULL(d))
381 		return -ENODEV;
382 
383 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr + off,
384 				    isr_off);
385 
386 	return 0;
387 }
388 
389 static const struct dbg_off pseudo_isr_off[] = {
390 	{"CAUSE",   S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE), doff_io32},
391 	{"MASK_SW", S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW), doff_io32},
392 	{"MASK_FW", S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_FW), doff_io32},
393 	{},
394 };
395 
396 static int wil6210_debugfs_create_pseudo_ISR(struct wil6210_priv *wil,
397 					     struct dentry *parent)
398 {
399 	struct dentry *d = debugfs_create_dir("PSEUDO_ISR", parent);
400 
401 	if (IS_ERR_OR_NULL(d))
402 		return -ENODEV;
403 
404 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
405 				    pseudo_isr_off);
406 
407 	return 0;
408 }
409 
410 static const struct dbg_off lgc_itr_cnt_off[] = {
411 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_TRSH), doff_io32},
412 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_DATA), doff_io32},
413 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_CRL), doff_io32},
414 	{},
415 };
416 
417 static const struct dbg_off tx_itr_cnt_off[] = {
418 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_TRSH),
419 	 doff_io32},
420 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_DATA),
421 	 doff_io32},
422 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_CTL),
423 	 doff_io32},
424 	{"IDL_TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_TRSH),
425 	 doff_io32},
426 	{"IDL_DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_DATA),
427 	 doff_io32},
428 	{"IDL_CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_CTL),
429 	 doff_io32},
430 	{},
431 };
432 
433 static const struct dbg_off rx_itr_cnt_off[] = {
434 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_TRSH),
435 	 doff_io32},
436 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_DATA),
437 	 doff_io32},
438 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_CTL),
439 	 doff_io32},
440 	{"IDL_TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_TRSH),
441 	 doff_io32},
442 	{"IDL_DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_DATA),
443 	 doff_io32},
444 	{"IDL_CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_CTL),
445 	 doff_io32},
446 	{},
447 };
448 
449 static int wil6210_debugfs_create_ITR_CNT(struct wil6210_priv *wil,
450 					  struct dentry *parent)
451 {
452 	struct dentry *d, *dtx, *drx;
453 
454 	d = debugfs_create_dir("ITR_CNT", parent);
455 	if (IS_ERR_OR_NULL(d))
456 		return -ENODEV;
457 
458 	dtx = debugfs_create_dir("TX", d);
459 	drx = debugfs_create_dir("RX", d);
460 	if (IS_ERR_OR_NULL(dtx) || IS_ERR_OR_NULL(drx))
461 		return -ENODEV;
462 
463 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
464 				    lgc_itr_cnt_off);
465 
466 	wil6210_debugfs_init_offset(wil, dtx, (void * __force)wil->csr,
467 				    tx_itr_cnt_off);
468 
469 	wil6210_debugfs_init_offset(wil, drx, (void * __force)wil->csr,
470 				    rx_itr_cnt_off);
471 	return 0;
472 }
473 
474 static int wil_memread_debugfs_show(struct seq_file *s, void *data)
475 {
476 	struct wil6210_priv *wil = s->private;
477 	void __iomem *a = wmi_buffer(wil, cpu_to_le32(mem_addr));
478 
479 	if (a)
480 		seq_printf(s, "[0x%08x] = 0x%08x\n", mem_addr, readl(a));
481 	else
482 		seq_printf(s, "[0x%08x] = INVALID\n", mem_addr);
483 
484 	return 0;
485 }
486 
487 static int wil_memread_seq_open(struct inode *inode, struct file *file)
488 {
489 	return single_open(file, wil_memread_debugfs_show, inode->i_private);
490 }
491 
492 static const struct file_operations fops_memread = {
493 	.open		= wil_memread_seq_open,
494 	.release	= single_release,
495 	.read		= seq_read,
496 	.llseek		= seq_lseek,
497 };
498 
499 static ssize_t wil_read_file_ioblob(struct file *file, char __user *user_buf,
500 				    size_t count, loff_t *ppos)
501 {
502 	enum { max_count = 4096 };
503 	struct debugfs_blob_wrapper *blob = file->private_data;
504 	loff_t pos = *ppos;
505 	size_t available = blob->size;
506 	void *buf;
507 	size_t ret;
508 
509 	if (pos < 0)
510 		return -EINVAL;
511 
512 	if (pos >= available || !count)
513 		return 0;
514 
515 	if (count > available - pos)
516 		count = available - pos;
517 	if (count > max_count)
518 		count = max_count;
519 
520 	buf = kmalloc(count, GFP_KERNEL);
521 	if (!buf)
522 		return -ENOMEM;
523 
524 	wil_memcpy_fromio_32(buf, (const volatile void __iomem *)blob->data +
525 			     pos, count);
526 
527 	ret = copy_to_user(user_buf, buf, count);
528 	kfree(buf);
529 	if (ret == count)
530 		return -EFAULT;
531 
532 	count -= ret;
533 	*ppos = pos + count;
534 
535 	return count;
536 }
537 
538 static const struct file_operations fops_ioblob = {
539 	.read =		wil_read_file_ioblob,
540 	.open =		simple_open,
541 	.llseek =	default_llseek,
542 };
543 
544 static
545 struct dentry *wil_debugfs_create_ioblob(const char *name,
546 					 umode_t mode,
547 					 struct dentry *parent,
548 					 struct debugfs_blob_wrapper *blob)
549 {
550 	return debugfs_create_file(name, mode, parent, blob, &fops_ioblob);
551 }
552 
553 /*---reset---*/
554 static ssize_t wil_write_file_reset(struct file *file, const char __user *buf,
555 				    size_t len, loff_t *ppos)
556 {
557 	struct wil6210_priv *wil = file->private_data;
558 	struct net_device *ndev = wil_to_ndev(wil);
559 
560 	/**
561 	 * BUG:
562 	 * this code does NOT sync device state with the rest of system
563 	 * use with care, debug only!!!
564 	 */
565 	rtnl_lock();
566 	dev_close(ndev);
567 	ndev->flags &= ~IFF_UP;
568 	rtnl_unlock();
569 	wil_reset(wil, true);
570 
571 	return len;
572 }
573 
574 static const struct file_operations fops_reset = {
575 	.write = wil_write_file_reset,
576 	.open  = simple_open,
577 };
578 
579 /*---write channel 1..4 to rxon for it, 0 to rxoff---*/
580 static ssize_t wil_write_file_rxon(struct file *file, const char __user *buf,
581 				   size_t len, loff_t *ppos)
582 {
583 	struct wil6210_priv *wil = file->private_data;
584 	int rc;
585 	long channel;
586 	bool on;
587 
588 	char *kbuf = memdup_user_nul(buf, len);
589 
590 	if (IS_ERR(kbuf))
591 		return PTR_ERR(kbuf);
592 	rc = kstrtol(kbuf, 0, &channel);
593 	kfree(kbuf);
594 	if (rc)
595 		return rc;
596 
597 	if ((channel < 0) || (channel > 4)) {
598 		wil_err(wil, "Invalid channel %ld\n", channel);
599 		return -EINVAL;
600 	}
601 	on = !!channel;
602 
603 	if (on) {
604 		rc = wmi_set_channel(wil, (int)channel);
605 		if (rc)
606 			return rc;
607 	}
608 
609 	rc = wmi_rxon(wil, on);
610 	if (rc)
611 		return rc;
612 
613 	return len;
614 }
615 
616 static const struct file_operations fops_rxon = {
617 	.write = wil_write_file_rxon,
618 	.open  = simple_open,
619 };
620 
621 /* block ack control, write:
622  * - "add <ringid> <agg_size> <timeout>" to trigger ADDBA
623  * - "del_tx <ringid> <reason>" to trigger DELBA for Tx side
624  * - "del_rx <CID> <TID> <reason>" to trigger DELBA for Rx side
625  */
626 static ssize_t wil_write_back(struct file *file, const char __user *buf,
627 			      size_t len, loff_t *ppos)
628 {
629 	struct wil6210_priv *wil = file->private_data;
630 	int rc;
631 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
632 	char cmd[9];
633 	int p1, p2, p3;
634 
635 	if (!kbuf)
636 		return -ENOMEM;
637 
638 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
639 	if (rc != len) {
640 		kfree(kbuf);
641 		return rc >= 0 ? -EIO : rc;
642 	}
643 
644 	kbuf[len] = '\0';
645 	rc = sscanf(kbuf, "%8s %d %d %d", cmd, &p1, &p2, &p3);
646 	kfree(kbuf);
647 
648 	if (rc < 0)
649 		return rc;
650 	if (rc < 2)
651 		return -EINVAL;
652 
653 	if (0 == strcmp(cmd, "add")) {
654 		if (rc < 3) {
655 			wil_err(wil, "BACK: add require at least 2 params\n");
656 			return -EINVAL;
657 		}
658 		if (rc < 4)
659 			p3 = 0;
660 		wmi_addba(wil, p1, p2, p3);
661 	} else if (0 == strcmp(cmd, "del_tx")) {
662 		if (rc < 3)
663 			p2 = WLAN_REASON_QSTA_LEAVE_QBSS;
664 		wmi_delba_tx(wil, p1, p2);
665 	} else if (0 == strcmp(cmd, "del_rx")) {
666 		if (rc < 3) {
667 			wil_err(wil,
668 				"BACK: del_rx require at least 2 params\n");
669 			return -EINVAL;
670 		}
671 		if (rc < 4)
672 			p3 = WLAN_REASON_QSTA_LEAVE_QBSS;
673 		wmi_delba_rx(wil, mk_cidxtid(p1, p2), p3);
674 	} else {
675 		wil_err(wil, "BACK: Unrecognized command \"%s\"\n", cmd);
676 		return -EINVAL;
677 	}
678 
679 	return len;
680 }
681 
682 static ssize_t wil_read_back(struct file *file, char __user *user_buf,
683 			     size_t count, loff_t *ppos)
684 {
685 	static const char text[] = "block ack control, write:\n"
686 	" - \"add <ringid> <agg_size> <timeout>\" to trigger ADDBA\n"
687 	"If missing, <timeout> defaults to 0\n"
688 	" - \"del_tx <ringid> <reason>\" to trigger DELBA for Tx side\n"
689 	" - \"del_rx <CID> <TID> <reason>\" to trigger DELBA for Rx side\n"
690 	"If missing, <reason> set to \"STA_LEAVING\" (36)\n";
691 
692 	return simple_read_from_buffer(user_buf, count, ppos, text,
693 				       sizeof(text));
694 }
695 
696 static const struct file_operations fops_back = {
697 	.read = wil_read_back,
698 	.write = wil_write_back,
699 	.open  = simple_open,
700 };
701 
702 /* pmc control, write:
703  * - "alloc <num descriptors> <descriptor_size>" to allocate PMC
704  * - "free" to release memory allocated for PMC
705  */
706 static ssize_t wil_write_pmccfg(struct file *file, const char __user *buf,
707 				size_t len, loff_t *ppos)
708 {
709 	struct wil6210_priv *wil = file->private_data;
710 	int rc;
711 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
712 	char cmd[9];
713 	int num_descs, desc_size;
714 
715 	if (!kbuf)
716 		return -ENOMEM;
717 
718 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
719 	if (rc != len) {
720 		kfree(kbuf);
721 		return rc >= 0 ? -EIO : rc;
722 	}
723 
724 	kbuf[len] = '\0';
725 	rc = sscanf(kbuf, "%8s %d %d", cmd, &num_descs, &desc_size);
726 	kfree(kbuf);
727 
728 	if (rc < 0)
729 		return rc;
730 
731 	if (rc < 1) {
732 		wil_err(wil, "pmccfg: no params given\n");
733 		return -EINVAL;
734 	}
735 
736 	if (0 == strcmp(cmd, "alloc")) {
737 		if (rc != 3) {
738 			wil_err(wil, "pmccfg: alloc requires 2 params\n");
739 			return -EINVAL;
740 		}
741 		wil_pmc_alloc(wil, num_descs, desc_size);
742 	} else if (0 == strcmp(cmd, "free")) {
743 		if (rc != 1) {
744 			wil_err(wil, "pmccfg: free does not have any params\n");
745 			return -EINVAL;
746 		}
747 		wil_pmc_free(wil, true);
748 	} else {
749 		wil_err(wil, "pmccfg: Unrecognized command \"%s\"\n", cmd);
750 		return -EINVAL;
751 	}
752 
753 	return len;
754 }
755 
756 static ssize_t wil_read_pmccfg(struct file *file, char __user *user_buf,
757 			       size_t count, loff_t *ppos)
758 {
759 	struct wil6210_priv *wil = file->private_data;
760 	char text[256];
761 	char help[] = "pmc control, write:\n"
762 	" - \"alloc <num descriptors> <descriptor_size>\" to allocate pmc\n"
763 	" - \"free\" to free memory allocated for pmc\n";
764 
765 	sprintf(text, "Last command status: %d\n\n%s",
766 		wil_pmc_last_cmd_status(wil),
767 		help);
768 
769 	return simple_read_from_buffer(user_buf, count, ppos, text,
770 				       strlen(text) + 1);
771 }
772 
773 static const struct file_operations fops_pmccfg = {
774 	.read = wil_read_pmccfg,
775 	.write = wil_write_pmccfg,
776 	.open  = simple_open,
777 };
778 
779 static const struct file_operations fops_pmcdata = {
780 	.open		= simple_open,
781 	.read		= wil_pmc_read,
782 	.llseek		= wil_pmc_llseek,
783 };
784 
785 /*---tx_mgmt---*/
786 /* Write mgmt frame to this file to send it */
787 static ssize_t wil_write_file_txmgmt(struct file *file, const char __user *buf,
788 				     size_t len, loff_t *ppos)
789 {
790 	struct wil6210_priv *wil = file->private_data;
791 	struct wiphy *wiphy = wil_to_wiphy(wil);
792 	struct wireless_dev *wdev = wil_to_wdev(wil);
793 	struct cfg80211_mgmt_tx_params params;
794 	int rc;
795 	void *frame = kmalloc(len, GFP_KERNEL);
796 
797 	if (!frame)
798 		return -ENOMEM;
799 
800 	if (copy_from_user(frame, buf, len)) {
801 		kfree(frame);
802 		return -EIO;
803 	}
804 
805 	params.buf = frame;
806 	params.len = len;
807 	params.chan = wdev->preset_chandef.chan;
808 
809 	rc = wil_cfg80211_mgmt_tx(wiphy, wdev, &params, NULL);
810 
811 	kfree(frame);
812 	wil_info(wil, "%s() -> %d\n", __func__, rc);
813 
814 	return len;
815 }
816 
817 static const struct file_operations fops_txmgmt = {
818 	.write = wil_write_file_txmgmt,
819 	.open  = simple_open,
820 };
821 
822 /* Write WMI command (w/o mbox header) to this file to send it
823  * WMI starts from wil6210_mbox_hdr_wmi header
824  */
825 static ssize_t wil_write_file_wmi(struct file *file, const char __user *buf,
826 				  size_t len, loff_t *ppos)
827 {
828 	struct wil6210_priv *wil = file->private_data;
829 	struct wil6210_mbox_hdr_wmi *wmi;
830 	void *cmd;
831 	int cmdlen = len - sizeof(struct wil6210_mbox_hdr_wmi);
832 	u16 cmdid;
833 	int rc, rc1;
834 
835 	if (cmdlen <= 0)
836 		return -EINVAL;
837 
838 	wmi = kmalloc(len, GFP_KERNEL);
839 	if (!wmi)
840 		return -ENOMEM;
841 
842 	rc = simple_write_to_buffer(wmi, len, ppos, buf, len);
843 	if (rc < 0) {
844 		kfree(wmi);
845 		return rc;
846 	}
847 
848 	cmd = &wmi[1];
849 	cmdid = le16_to_cpu(wmi->id);
850 
851 	rc1 = wmi_send(wil, cmdid, cmd, cmdlen);
852 	kfree(wmi);
853 
854 	wil_info(wil, "%s(0x%04x[%d]) -> %d\n", __func__, cmdid, cmdlen, rc1);
855 
856 	return rc;
857 }
858 
859 static const struct file_operations fops_wmi = {
860 	.write = wil_write_file_wmi,
861 	.open  = simple_open,
862 };
863 
864 static void wil_seq_print_skb(struct seq_file *s, struct sk_buff *skb)
865 {
866 	int i = 0;
867 	int len = skb_headlen(skb);
868 	void *p = skb->data;
869 	int nr_frags = skb_shinfo(skb)->nr_frags;
870 
871 	seq_printf(s, "    len = %d\n", len);
872 	wil_seq_hexdump(s, p, len, "      : ");
873 
874 	if (nr_frags) {
875 		seq_printf(s, "    nr_frags = %d\n", nr_frags);
876 		for (i = 0; i < nr_frags; i++) {
877 			const struct skb_frag_struct *frag =
878 					&skb_shinfo(skb)->frags[i];
879 
880 			len = skb_frag_size(frag);
881 			p = skb_frag_address_safe(frag);
882 			seq_printf(s, "    [%2d] : len = %d\n", i, len);
883 			wil_seq_hexdump(s, p, len, "      : ");
884 		}
885 	}
886 }
887 
888 /*---------Tx/Rx descriptor------------*/
889 static int wil_txdesc_debugfs_show(struct seq_file *s, void *data)
890 {
891 	struct wil6210_priv *wil = s->private;
892 	struct vring *vring;
893 	bool tx = (dbg_vring_index < WIL6210_MAX_TX_RINGS);
894 
895 	vring = tx ? &wil->vring_tx[dbg_vring_index] : &wil->vring_rx;
896 
897 	if (!vring->va) {
898 		if (tx)
899 			seq_printf(s, "No Tx[%2d] VRING\n", dbg_vring_index);
900 		else
901 			seq_puts(s, "No Rx VRING\n");
902 		return 0;
903 	}
904 
905 	if (dbg_txdesc_index < vring->size) {
906 		/* use struct vring_tx_desc for Rx as well,
907 		 * only field used, .dma.length, is the same
908 		 */
909 		volatile struct vring_tx_desc *d =
910 				&vring->va[dbg_txdesc_index].tx;
911 		volatile u32 *u = (volatile u32 *)d;
912 		struct sk_buff *skb = vring->ctx[dbg_txdesc_index].skb;
913 
914 		if (tx)
915 			seq_printf(s, "Tx[%2d][%3d] = {\n", dbg_vring_index,
916 				   dbg_txdesc_index);
917 		else
918 			seq_printf(s, "Rx[%3d] = {\n", dbg_txdesc_index);
919 		seq_printf(s, "  MAC = 0x%08x 0x%08x 0x%08x 0x%08x\n",
920 			   u[0], u[1], u[2], u[3]);
921 		seq_printf(s, "  DMA = 0x%08x 0x%08x 0x%08x 0x%08x\n",
922 			   u[4], u[5], u[6], u[7]);
923 		seq_printf(s, "  SKB = 0x%p\n", skb);
924 
925 		if (skb) {
926 			skb_get(skb);
927 			wil_seq_print_skb(s, skb);
928 			kfree_skb(skb);
929 		}
930 		seq_puts(s, "}\n");
931 	} else {
932 		if (tx)
933 			seq_printf(s, "[%2d] TxDesc index (%d) >= size (%d)\n",
934 				   dbg_vring_index, dbg_txdesc_index,
935 				   vring->size);
936 		else
937 			seq_printf(s, "RxDesc index (%d) >= size (%d)\n",
938 				   dbg_txdesc_index, vring->size);
939 	}
940 
941 	return 0;
942 }
943 
944 static int wil_txdesc_seq_open(struct inode *inode, struct file *file)
945 {
946 	return single_open(file, wil_txdesc_debugfs_show, inode->i_private);
947 }
948 
949 static const struct file_operations fops_txdesc = {
950 	.open		= wil_txdesc_seq_open,
951 	.release	= single_release,
952 	.read		= seq_read,
953 	.llseek		= seq_lseek,
954 };
955 
956 /*---------beamforming------------*/
957 static char *wil_bfstatus_str(u32 status)
958 {
959 	switch (status) {
960 	case 0:
961 		return "Failed";
962 	case 1:
963 		return "OK";
964 	case 2:
965 		return "Retrying";
966 	default:
967 		return "??";
968 	}
969 }
970 
971 static bool is_all_zeros(void * const x_, size_t sz)
972 {
973 	/* if reply is all-0, ignore this CID */
974 	u32 *x = x_;
975 	int n;
976 
977 	for (n = 0; n < sz / sizeof(*x); n++)
978 		if (x[n])
979 			return false;
980 
981 	return true;
982 }
983 
984 static int wil_bf_debugfs_show(struct seq_file *s, void *data)
985 {
986 	int rc;
987 	int i;
988 	struct wil6210_priv *wil = s->private;
989 	struct wmi_notify_req_cmd cmd = {
990 		.interval_usec = 0,
991 	};
992 	struct {
993 		struct wil6210_mbox_hdr_wmi wmi;
994 		struct wmi_notify_req_done_event evt;
995 	} __packed reply;
996 
997 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
998 		u32 status;
999 
1000 		cmd.cid = i;
1001 		rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
1002 			      WMI_NOTIFY_REQ_DONE_EVENTID, &reply,
1003 			      sizeof(reply), 20);
1004 		/* if reply is all-0, ignore this CID */
1005 		if (rc || is_all_zeros(&reply.evt, sizeof(reply.evt)))
1006 			continue;
1007 
1008 		status = le32_to_cpu(reply.evt.status);
1009 		seq_printf(s, "CID %d {\n"
1010 			   "  TSF = 0x%016llx\n"
1011 			   "  TxMCS = %2d TxTpt = %4d\n"
1012 			   "  SQI = %4d\n"
1013 			   "  Status = 0x%08x %s\n"
1014 			   "  Sectors(rx:tx) my %2d:%2d peer %2d:%2d\n"
1015 			   "  Goodput(rx:tx) %4d:%4d\n"
1016 			   "}\n",
1017 			   i,
1018 			   le64_to_cpu(reply.evt.tsf),
1019 			   le16_to_cpu(reply.evt.bf_mcs),
1020 			   le32_to_cpu(reply.evt.tx_tpt),
1021 			   reply.evt.sqi,
1022 			   status, wil_bfstatus_str(status),
1023 			   le16_to_cpu(reply.evt.my_rx_sector),
1024 			   le16_to_cpu(reply.evt.my_tx_sector),
1025 			   le16_to_cpu(reply.evt.other_rx_sector),
1026 			   le16_to_cpu(reply.evt.other_tx_sector),
1027 			   le32_to_cpu(reply.evt.rx_goodput),
1028 			   le32_to_cpu(reply.evt.tx_goodput));
1029 	}
1030 	return 0;
1031 }
1032 
1033 static int wil_bf_seq_open(struct inode *inode, struct file *file)
1034 {
1035 	return single_open(file, wil_bf_debugfs_show, inode->i_private);
1036 }
1037 
1038 static const struct file_operations fops_bf = {
1039 	.open		= wil_bf_seq_open,
1040 	.release	= single_release,
1041 	.read		= seq_read,
1042 	.llseek		= seq_lseek,
1043 };
1044 
1045 /*---------SSID------------*/
1046 static ssize_t wil_read_file_ssid(struct file *file, char __user *user_buf,
1047 				  size_t count, loff_t *ppos)
1048 {
1049 	struct wil6210_priv *wil = file->private_data;
1050 	struct wireless_dev *wdev = wil_to_wdev(wil);
1051 
1052 	return simple_read_from_buffer(user_buf, count, ppos,
1053 				       wdev->ssid, wdev->ssid_len);
1054 }
1055 
1056 static ssize_t wil_write_file_ssid(struct file *file, const char __user *buf,
1057 				   size_t count, loff_t *ppos)
1058 {
1059 	struct wil6210_priv *wil = file->private_data;
1060 	struct wireless_dev *wdev = wil_to_wdev(wil);
1061 	struct net_device *ndev = wil_to_ndev(wil);
1062 
1063 	if (*ppos != 0) {
1064 		wil_err(wil, "Unable to set SSID substring from [%d]\n",
1065 			(int)*ppos);
1066 		return -EINVAL;
1067 	}
1068 
1069 	if (count > sizeof(wdev->ssid)) {
1070 		wil_err(wil, "SSID too long, len = %d\n", (int)count);
1071 		return -EINVAL;
1072 	}
1073 	if (netif_running(ndev)) {
1074 		wil_err(wil, "Unable to change SSID on running interface\n");
1075 		return -EINVAL;
1076 	}
1077 
1078 	wdev->ssid_len = count;
1079 	return simple_write_to_buffer(wdev->ssid, wdev->ssid_len, ppos,
1080 				      buf, count);
1081 }
1082 
1083 static const struct file_operations fops_ssid = {
1084 	.read = wil_read_file_ssid,
1085 	.write = wil_write_file_ssid,
1086 	.open  = simple_open,
1087 };
1088 
1089 /*---------temp------------*/
1090 static void print_temp(struct seq_file *s, const char *prefix, u32 t)
1091 {
1092 	switch (t) {
1093 	case 0:
1094 	case ~(u32)0:
1095 		seq_printf(s, "%s N/A\n", prefix);
1096 	break;
1097 	default:
1098 		seq_printf(s, "%s %d.%03d\n", prefix, t / 1000, t % 1000);
1099 		break;
1100 	}
1101 }
1102 
1103 static int wil_temp_debugfs_show(struct seq_file *s, void *data)
1104 {
1105 	struct wil6210_priv *wil = s->private;
1106 	u32 t_m, t_r;
1107 	int rc = wmi_get_temperature(wil, &t_m, &t_r);
1108 
1109 	if (rc) {
1110 		seq_puts(s, "Failed\n");
1111 		return 0;
1112 	}
1113 
1114 	print_temp(s, "T_mac   =", t_m);
1115 	print_temp(s, "T_radio =", t_r);
1116 
1117 	return 0;
1118 }
1119 
1120 static int wil_temp_seq_open(struct inode *inode, struct file *file)
1121 {
1122 	return single_open(file, wil_temp_debugfs_show, inode->i_private);
1123 }
1124 
1125 static const struct file_operations fops_temp = {
1126 	.open		= wil_temp_seq_open,
1127 	.release	= single_release,
1128 	.read		= seq_read,
1129 	.llseek		= seq_lseek,
1130 };
1131 
1132 /*---------freq------------*/
1133 static int wil_freq_debugfs_show(struct seq_file *s, void *data)
1134 {
1135 	struct wil6210_priv *wil = s->private;
1136 	struct wireless_dev *wdev = wil_to_wdev(wil);
1137 	u16 freq = wdev->chandef.chan ? wdev->chandef.chan->center_freq : 0;
1138 
1139 	seq_printf(s, "Freq = %d\n", freq);
1140 
1141 	return 0;
1142 }
1143 
1144 static int wil_freq_seq_open(struct inode *inode, struct file *file)
1145 {
1146 	return single_open(file, wil_freq_debugfs_show, inode->i_private);
1147 }
1148 
1149 static const struct file_operations fops_freq = {
1150 	.open		= wil_freq_seq_open,
1151 	.release	= single_release,
1152 	.read		= seq_read,
1153 	.llseek		= seq_lseek,
1154 };
1155 
1156 /*---------link------------*/
1157 static int wil_link_debugfs_show(struct seq_file *s, void *data)
1158 {
1159 	struct wil6210_priv *wil = s->private;
1160 	struct station_info sinfo;
1161 	int i, rc;
1162 
1163 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1164 		struct wil_sta_info *p = &wil->sta[i];
1165 		char *status = "unknown";
1166 
1167 		switch (p->status) {
1168 		case wil_sta_unused:
1169 			status = "unused   ";
1170 			break;
1171 		case wil_sta_conn_pending:
1172 			status = "pending  ";
1173 			break;
1174 		case wil_sta_connected:
1175 			status = "connected";
1176 			break;
1177 		}
1178 		seq_printf(s, "[%d] %pM %s\n", i, p->addr, status);
1179 
1180 		if (p->status == wil_sta_connected) {
1181 			rc = wil_cid_fill_sinfo(wil, i, &sinfo);
1182 			if (rc)
1183 				return rc;
1184 
1185 			seq_printf(s, "  Tx_mcs = %d\n", sinfo.txrate.mcs);
1186 			seq_printf(s, "  Rx_mcs = %d\n", sinfo.rxrate.mcs);
1187 			seq_printf(s, "  SQ     = %d\n", sinfo.signal);
1188 		}
1189 	}
1190 
1191 	return 0;
1192 }
1193 
1194 static int wil_link_seq_open(struct inode *inode, struct file *file)
1195 {
1196 	return single_open(file, wil_link_debugfs_show, inode->i_private);
1197 }
1198 
1199 static const struct file_operations fops_link = {
1200 	.open		= wil_link_seq_open,
1201 	.release	= single_release,
1202 	.read		= seq_read,
1203 	.llseek		= seq_lseek,
1204 };
1205 
1206 /*---------info------------*/
1207 static int wil_info_debugfs_show(struct seq_file *s, void *data)
1208 {
1209 	struct wil6210_priv *wil = s->private;
1210 	struct net_device *ndev = wil_to_ndev(wil);
1211 	int is_ac = power_supply_is_system_supplied();
1212 	int rx = atomic_xchg(&wil->isr_count_rx, 0);
1213 	int tx = atomic_xchg(&wil->isr_count_tx, 0);
1214 	static ulong rxf_old, txf_old;
1215 	ulong rxf = ndev->stats.rx_packets;
1216 	ulong txf = ndev->stats.tx_packets;
1217 	unsigned int i;
1218 
1219 	/* >0 : AC; 0 : battery; <0 : error */
1220 	seq_printf(s, "AC powered : %d\n", is_ac);
1221 	seq_printf(s, "Rx irqs:packets : %8d : %8ld\n", rx, rxf - rxf_old);
1222 	seq_printf(s, "Tx irqs:packets : %8d : %8ld\n", tx, txf - txf_old);
1223 	rxf_old = rxf;
1224 	txf_old = txf;
1225 
1226 #define CHECK_QSTATE(x) (state & BIT(__QUEUE_STATE_ ## x)) ? \
1227 	" " __stringify(x) : ""
1228 
1229 	for (i = 0; i < ndev->num_tx_queues; i++) {
1230 		struct netdev_queue *txq = netdev_get_tx_queue(ndev, i);
1231 		unsigned long state = txq->state;
1232 
1233 		seq_printf(s, "Tx queue[%i] state : 0x%lx%s%s%s\n", i, state,
1234 			   CHECK_QSTATE(DRV_XOFF),
1235 			   CHECK_QSTATE(STACK_XOFF),
1236 			   CHECK_QSTATE(FROZEN)
1237 			  );
1238 	}
1239 #undef CHECK_QSTATE
1240 	return 0;
1241 }
1242 
1243 static int wil_info_seq_open(struct inode *inode, struct file *file)
1244 {
1245 	return single_open(file, wil_info_debugfs_show, inode->i_private);
1246 }
1247 
1248 static const struct file_operations fops_info = {
1249 	.open		= wil_info_seq_open,
1250 	.release	= single_release,
1251 	.read		= seq_read,
1252 	.llseek		= seq_lseek,
1253 };
1254 
1255 /*---------recovery------------*/
1256 /* mode = [manual|auto]
1257  * state = [idle|pending|running]
1258  */
1259 static ssize_t wil_read_file_recovery(struct file *file, char __user *user_buf,
1260 				      size_t count, loff_t *ppos)
1261 {
1262 	struct wil6210_priv *wil = file->private_data;
1263 	char buf[80];
1264 	int n;
1265 	static const char * const sstate[] = {"idle", "pending", "running"};
1266 
1267 	n = snprintf(buf, sizeof(buf), "mode = %s\nstate = %s\n",
1268 		     no_fw_recovery ? "manual" : "auto",
1269 		     sstate[wil->recovery_state]);
1270 
1271 	n = min_t(int, n, sizeof(buf));
1272 
1273 	return simple_read_from_buffer(user_buf, count, ppos,
1274 				       buf, n);
1275 }
1276 
1277 static ssize_t wil_write_file_recovery(struct file *file,
1278 				       const char __user *buf_,
1279 				       size_t count, loff_t *ppos)
1280 {
1281 	struct wil6210_priv *wil = file->private_data;
1282 	static const char run_command[] = "run";
1283 	char buf[sizeof(run_command) + 1]; /* to detect "runx" */
1284 	ssize_t rc;
1285 
1286 	if (wil->recovery_state != fw_recovery_pending) {
1287 		wil_err(wil, "No recovery pending\n");
1288 		return -EINVAL;
1289 	}
1290 
1291 	if (*ppos != 0) {
1292 		wil_err(wil, "Offset [%d]\n", (int)*ppos);
1293 		return -EINVAL;
1294 	}
1295 
1296 	if (count > sizeof(buf)) {
1297 		wil_err(wil, "Input too long, len = %d\n", (int)count);
1298 		return -EINVAL;
1299 	}
1300 
1301 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, buf_, count);
1302 	if (rc < 0)
1303 		return rc;
1304 
1305 	buf[rc] = '\0';
1306 	if (0 == strcmp(buf, run_command))
1307 		wil_set_recovery_state(wil, fw_recovery_running);
1308 	else
1309 		wil_err(wil, "Bad recovery command \"%s\"\n", buf);
1310 
1311 	return rc;
1312 }
1313 
1314 static const struct file_operations fops_recovery = {
1315 	.read = wil_read_file_recovery,
1316 	.write = wil_write_file_recovery,
1317 	.open  = simple_open,
1318 };
1319 
1320 /*---------Station matrix------------*/
1321 static void wil_print_rxtid(struct seq_file *s, struct wil_tid_ampdu_rx *r)
1322 {
1323 	int i;
1324 	u16 index = ((r->head_seq_num - r->ssn) & 0xfff) % r->buf_size;
1325 	unsigned long long drop_dup = r->drop_dup, drop_old = r->drop_old;
1326 
1327 	seq_printf(s, "([%2d] %3d TU) 0x%03x [", r->buf_size, r->timeout,
1328 		   r->head_seq_num);
1329 	for (i = 0; i < r->buf_size; i++) {
1330 		if (i == index)
1331 			seq_printf(s, "%c", r->reorder_buf[i] ? 'O' : '|');
1332 		else
1333 			seq_printf(s, "%c", r->reorder_buf[i] ? '*' : '_');
1334 	}
1335 	seq_printf(s,
1336 		   "] total %llu drop %llu (dup %llu + old %llu) last 0x%03x\n",
1337 		   r->total, drop_dup + drop_old, drop_dup, drop_old,
1338 		   r->ssn_last_drop);
1339 }
1340 
1341 static void wil_print_rxtid_crypto(struct seq_file *s, int tid,
1342 				   struct wil_tid_crypto_rx *c)
1343 {
1344 	int i;
1345 
1346 	for (i = 0; i < 4; i++) {
1347 		struct wil_tid_crypto_rx_single *cc = &c->key_id[i];
1348 
1349 		if (cc->key_set)
1350 			goto has_keys;
1351 	}
1352 	return;
1353 
1354 has_keys:
1355 	if (tid < WIL_STA_TID_NUM)
1356 		seq_printf(s, "  [%2d] PN", tid);
1357 	else
1358 		seq_puts(s, "  [GR] PN");
1359 
1360 	for (i = 0; i < 4; i++) {
1361 		struct wil_tid_crypto_rx_single *cc = &c->key_id[i];
1362 
1363 		seq_printf(s, " [%i%s]%6phN", i, cc->key_set ? "+" : "-",
1364 			   cc->pn);
1365 	}
1366 	seq_puts(s, "\n");
1367 }
1368 
1369 static int wil_sta_debugfs_show(struct seq_file *s, void *data)
1370 __acquires(&p->tid_rx_lock) __releases(&p->tid_rx_lock)
1371 {
1372 	struct wil6210_priv *wil = s->private;
1373 	int i, tid, mcs;
1374 
1375 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1376 		struct wil_sta_info *p = &wil->sta[i];
1377 		char *status = "unknown";
1378 
1379 		switch (p->status) {
1380 		case wil_sta_unused:
1381 			status = "unused   ";
1382 			break;
1383 		case wil_sta_conn_pending:
1384 			status = "pending  ";
1385 			break;
1386 		case wil_sta_connected:
1387 			status = "connected";
1388 			break;
1389 		}
1390 		seq_printf(s, "[%d] %pM %s\n", i, p->addr, status);
1391 
1392 		if (p->status == wil_sta_connected) {
1393 			spin_lock_bh(&p->tid_rx_lock);
1394 			for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1395 				struct wil_tid_ampdu_rx *r = p->tid_rx[tid];
1396 				struct wil_tid_crypto_rx *c =
1397 						&p->tid_crypto_rx[tid];
1398 
1399 				if (r) {
1400 					seq_printf(s, "  [%2d] ", tid);
1401 					wil_print_rxtid(s, r);
1402 				}
1403 
1404 				wil_print_rxtid_crypto(s, tid, c);
1405 			}
1406 			wil_print_rxtid_crypto(s, WIL_STA_TID_NUM,
1407 					       &p->group_crypto_rx);
1408 			spin_unlock_bh(&p->tid_rx_lock);
1409 			seq_printf(s,
1410 				   "Rx invalid frame: non-data %lu, short %lu, large %lu, replay %lu\n",
1411 				   p->stats.rx_non_data_frame,
1412 				   p->stats.rx_short_frame,
1413 				   p->stats.rx_large_frame,
1414 				   p->stats.rx_replay);
1415 
1416 			seq_puts(s, "Rx/MCS:");
1417 			for (mcs = 0; mcs < ARRAY_SIZE(p->stats.rx_per_mcs);
1418 			     mcs++)
1419 				seq_printf(s, " %lld",
1420 					   p->stats.rx_per_mcs[mcs]);
1421 			seq_puts(s, "\n");
1422 		}
1423 	}
1424 
1425 	return 0;
1426 }
1427 
1428 static int wil_sta_seq_open(struct inode *inode, struct file *file)
1429 {
1430 	return single_open(file, wil_sta_debugfs_show, inode->i_private);
1431 }
1432 
1433 static const struct file_operations fops_sta = {
1434 	.open		= wil_sta_seq_open,
1435 	.release	= single_release,
1436 	.read		= seq_read,
1437 	.llseek		= seq_lseek,
1438 };
1439 
1440 /*----------------*/
1441 static void wil6210_debugfs_init_blobs(struct wil6210_priv *wil,
1442 				       struct dentry *dbg)
1443 {
1444 	int i;
1445 	char name[32];
1446 
1447 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
1448 		struct debugfs_blob_wrapper *blob = &wil->blobs[i];
1449 		const struct fw_map *map = &fw_mapping[i];
1450 
1451 		if (!map->name)
1452 			continue;
1453 
1454 		blob->data = (void * __force)wil->csr + HOSTADDR(map->host);
1455 		blob->size = map->to - map->from;
1456 		snprintf(name, sizeof(name), "blob_%s", map->name);
1457 		wil_debugfs_create_ioblob(name, S_IRUGO, dbg, blob);
1458 	}
1459 }
1460 
1461 /* misc files */
1462 static const struct {
1463 	const char *name;
1464 	umode_t mode;
1465 	const struct file_operations *fops;
1466 } dbg_files[] = {
1467 	{"mbox",	S_IRUGO,		&fops_mbox},
1468 	{"vrings",	S_IRUGO,		&fops_vring},
1469 	{"stations",	S_IRUGO,		&fops_sta},
1470 	{"desc",	S_IRUGO,		&fops_txdesc},
1471 	{"bf",		S_IRUGO,		&fops_bf},
1472 	{"ssid",	S_IRUGO | S_IWUSR,	&fops_ssid},
1473 	{"mem_val",	S_IRUGO,		&fops_memread},
1474 	{"reset",		  S_IWUSR,	&fops_reset},
1475 	{"rxon",		  S_IWUSR,	&fops_rxon},
1476 	{"tx_mgmt",		  S_IWUSR,	&fops_txmgmt},
1477 	{"wmi_send",		  S_IWUSR,	&fops_wmi},
1478 	{"back",	S_IRUGO | S_IWUSR,	&fops_back},
1479 	{"pmccfg",	S_IRUGO | S_IWUSR,	&fops_pmccfg},
1480 	{"pmcdata",	S_IRUGO,		&fops_pmcdata},
1481 	{"temp",	S_IRUGO,		&fops_temp},
1482 	{"freq",	S_IRUGO,		&fops_freq},
1483 	{"link",	S_IRUGO,		&fops_link},
1484 	{"info",	S_IRUGO,		&fops_info},
1485 	{"recovery",	S_IRUGO | S_IWUSR,	&fops_recovery},
1486 };
1487 
1488 static void wil6210_debugfs_init_files(struct wil6210_priv *wil,
1489 				       struct dentry *dbg)
1490 {
1491 	int i;
1492 
1493 	for (i = 0; i < ARRAY_SIZE(dbg_files); i++)
1494 		debugfs_create_file(dbg_files[i].name, dbg_files[i].mode, dbg,
1495 				    wil, dbg_files[i].fops);
1496 }
1497 
1498 /* interrupt control blocks */
1499 static const struct {
1500 	const char *name;
1501 	u32 icr_off;
1502 } dbg_icr[] = {
1503 	{"USER_ICR",		HOSTADDR(RGF_USER_USER_ICR)},
1504 	{"DMA_EP_TX_ICR",	HOSTADDR(RGF_DMA_EP_TX_ICR)},
1505 	{"DMA_EP_RX_ICR",	HOSTADDR(RGF_DMA_EP_RX_ICR)},
1506 	{"DMA_EP_MISC_ICR",	HOSTADDR(RGF_DMA_EP_MISC_ICR)},
1507 };
1508 
1509 static void wil6210_debugfs_init_isr(struct wil6210_priv *wil,
1510 				     struct dentry *dbg)
1511 {
1512 	int i;
1513 
1514 	for (i = 0; i < ARRAY_SIZE(dbg_icr); i++)
1515 		wil6210_debugfs_create_ISR(wil, dbg_icr[i].name, dbg,
1516 					   dbg_icr[i].icr_off);
1517 }
1518 
1519 #define WIL_FIELD(name, mode, type) { __stringify(name), mode, \
1520 	offsetof(struct wil6210_priv, name), type}
1521 
1522 /* fields in struct wil6210_priv */
1523 static const struct dbg_off dbg_wil_off[] = {
1524 	WIL_FIELD(privacy,	S_IRUGO,		doff_u32),
1525 	WIL_FIELD(status[0],	S_IRUGO | S_IWUSR,	doff_ulong),
1526 	WIL_FIELD(fw_version,	S_IRUGO,		doff_u32),
1527 	WIL_FIELD(hw_version,	S_IRUGO,		doff_x32),
1528 	WIL_FIELD(recovery_count, S_IRUGO,		doff_u32),
1529 	WIL_FIELD(ap_isolate,	S_IRUGO,		doff_u32),
1530 	WIL_FIELD(discovery_mode, S_IRUGO | S_IWUSR,	doff_u8),
1531 	{},
1532 };
1533 
1534 static const struct dbg_off dbg_wil_regs[] = {
1535 	{"RGF_MAC_MTRL_COUNTER_0", S_IRUGO, HOSTADDR(RGF_MAC_MTRL_COUNTER_0),
1536 		doff_io32},
1537 	{"RGF_USER_USAGE_1", S_IRUGO, HOSTADDR(RGF_USER_USAGE_1), doff_io32},
1538 	{},
1539 };
1540 
1541 /* static parameters */
1542 static const struct dbg_off dbg_statics[] = {
1543 	{"desc_index",	S_IRUGO | S_IWUSR, (ulong)&dbg_txdesc_index, doff_u32},
1544 	{"vring_index",	S_IRUGO | S_IWUSR, (ulong)&dbg_vring_index, doff_u32},
1545 	{"mem_addr",	S_IRUGO | S_IWUSR, (ulong)&mem_addr, doff_u32},
1546 	{"vring_idle_trsh", S_IRUGO | S_IWUSR, (ulong)&vring_idle_trsh,
1547 	 doff_u32},
1548 	{},
1549 };
1550 
1551 int wil6210_debugfs_init(struct wil6210_priv *wil)
1552 {
1553 	struct dentry *dbg = wil->debug = debugfs_create_dir(WIL_NAME,
1554 			wil_to_wiphy(wil)->debugfsdir);
1555 
1556 	if (IS_ERR_OR_NULL(dbg))
1557 		return -ENODEV;
1558 
1559 	wil_pmc_init(wil);
1560 
1561 	wil6210_debugfs_init_files(wil, dbg);
1562 	wil6210_debugfs_init_isr(wil, dbg);
1563 	wil6210_debugfs_init_blobs(wil, dbg);
1564 	wil6210_debugfs_init_offset(wil, dbg, wil, dbg_wil_off);
1565 	wil6210_debugfs_init_offset(wil, dbg, (void * __force)wil->csr,
1566 				    dbg_wil_regs);
1567 	wil6210_debugfs_init_offset(wil, dbg, NULL, dbg_statics);
1568 
1569 	wil6210_debugfs_create_pseudo_ISR(wil, dbg);
1570 
1571 	wil6210_debugfs_create_ITR_CNT(wil, dbg);
1572 
1573 	return 0;
1574 }
1575 
1576 void wil6210_debugfs_remove(struct wil6210_priv *wil)
1577 {
1578 	debugfs_remove_recursive(wil->debug);
1579 	wil->debug = NULL;
1580 
1581 	/* free pmc memory without sending command to fw, as it will
1582 	 * be reset on the way down anyway
1583 	 */
1584 	wil_pmc_free(wil, false);
1585 }
1586