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