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