xref: /linux/drivers/net/wireless/ath/wil6210/debugfs.c (revision 8ea06188fd8cb4c6bb85663b6dedcab22f7ffba7)
12be7d22fSVladimir Kondratiev /*
2230d8442SVladimir Kondratiev  * Copyright (c) 2012-2015 Qualcomm Atheros, Inc.
32be7d22fSVladimir Kondratiev  *
42be7d22fSVladimir Kondratiev  * Permission to use, copy, modify, and/or distribute this software for any
52be7d22fSVladimir Kondratiev  * purpose with or without fee is hereby granted, provided that the above
62be7d22fSVladimir Kondratiev  * copyright notice and this permission notice appear in all copies.
72be7d22fSVladimir Kondratiev  *
82be7d22fSVladimir Kondratiev  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
92be7d22fSVladimir Kondratiev  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
102be7d22fSVladimir Kondratiev  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
112be7d22fSVladimir Kondratiev  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
122be7d22fSVladimir Kondratiev  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
132be7d22fSVladimir Kondratiev  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
142be7d22fSVladimir Kondratiev  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
152be7d22fSVladimir Kondratiev  */
162be7d22fSVladimir Kondratiev 
172be7d22fSVladimir Kondratiev #include <linux/module.h>
182be7d22fSVladimir Kondratiev #include <linux/debugfs.h>
192be7d22fSVladimir Kondratiev #include <linux/seq_file.h>
202be7d22fSVladimir Kondratiev #include <linux/pci.h>
212be7d22fSVladimir Kondratiev #include <linux/rtnetlink.h>
2284bb29b7SVladimir Kondratiev #include <linux/power_supply.h>
232be7d22fSVladimir Kondratiev 
242be7d22fSVladimir Kondratiev #include "wil6210.h"
2536345ac3SVladimir Kondratiev #include "wmi.h"
262be7d22fSVladimir Kondratiev #include "txrx.h"
27dc16427bSVladimir Kondratiev #include "pmc.h"
282be7d22fSVladimir Kondratiev 
292be7d22fSVladimir Kondratiev /* Nasty hack. Better have per device instances */
302be7d22fSVladimir Kondratiev static u32 mem_addr;
312be7d22fSVladimir Kondratiev static u32 dbg_txdesc_index;
32af31cb5aSVladimir Kondratiev static u32 dbg_vring_index; /* 24+ for Rx, 0..23 for Tx */
330436fd9aSVladimir Shulman u32 vring_idle_trsh = 16; /* HW fetches up to 16 descriptors at once */
342be7d22fSVladimir Kondratiev 
35b7cde470SVladimir Kondratiev enum dbg_off_type {
36b7cde470SVladimir Kondratiev 	doff_u32 = 0,
37b7cde470SVladimir Kondratiev 	doff_x32 = 1,
38b7cde470SVladimir Kondratiev 	doff_ulong = 2,
39b7cde470SVladimir Kondratiev 	doff_io32 = 3,
40b7cde470SVladimir Kondratiev };
41b7cde470SVladimir Kondratiev 
42b7cde470SVladimir Kondratiev /* offset to "wil" */
43b7cde470SVladimir Kondratiev struct dbg_off {
44b7cde470SVladimir Kondratiev 	const char *name;
45b7cde470SVladimir Kondratiev 	umode_t mode;
46b7cde470SVladimir Kondratiev 	ulong off;
47b7cde470SVladimir Kondratiev 	enum dbg_off_type type;
48b7cde470SVladimir Kondratiev };
49b7cde470SVladimir Kondratiev 
502be7d22fSVladimir Kondratiev static void wil_print_vring(struct seq_file *s, struct wil6210_priv *wil,
5159f7c0a9SVladimir Kondratiev 			    const char *name, struct vring *vring,
5259f7c0a9SVladimir Kondratiev 			    char _s, char _h)
532be7d22fSVladimir Kondratiev {
542be7d22fSVladimir Kondratiev 	void __iomem *x = wmi_addr(wil, vring->hwtail);
55995cdd0eSVladimir Kondratiev 	u32 v;
562be7d22fSVladimir Kondratiev 
572be7d22fSVladimir Kondratiev 	seq_printf(s, "VRING %s = {\n", name);
5839c52ee8SVladimir Kondratiev 	seq_printf(s, "  pa     = %pad\n", &vring->pa);
592be7d22fSVladimir Kondratiev 	seq_printf(s, "  va     = 0x%p\n", vring->va);
602be7d22fSVladimir Kondratiev 	seq_printf(s, "  size   = %d\n", vring->size);
612be7d22fSVladimir Kondratiev 	seq_printf(s, "  swtail = %d\n", vring->swtail);
622be7d22fSVladimir Kondratiev 	seq_printf(s, "  swhead = %d\n", vring->swhead);
632be7d22fSVladimir Kondratiev 	seq_printf(s, "  hwtail = [0x%08x] -> ", vring->hwtail);
64995cdd0eSVladimir Kondratiev 	if (x) {
65995cdd0eSVladimir Kondratiev 		v = ioread32(x);
66995cdd0eSVladimir Kondratiev 		seq_printf(s, "0x%08x = %d\n", v, v);
67995cdd0eSVladimir Kondratiev 	} else {
688fe59627SVladimir Kondratiev 		seq_puts(s, "???\n");
69995cdd0eSVladimir Kondratiev 	}
702be7d22fSVladimir Kondratiev 
712be7d22fSVladimir Kondratiev 	if (vring->va && (vring->size < 1025)) {
722be7d22fSVladimir Kondratiev 		uint i;
738fe59627SVladimir Kondratiev 
742be7d22fSVladimir Kondratiev 		for (i = 0; i < vring->size; i++) {
752be7d22fSVladimir Kondratiev 			volatile struct vring_tx_desc *d = &vring->va[i].tx;
768fe59627SVladimir Kondratiev 
772be7d22fSVladimir Kondratiev 			if ((i % 64) == 0 && (i != 0))
788fe59627SVladimir Kondratiev 				seq_puts(s, "\n");
7959f7c0a9SVladimir Kondratiev 			seq_printf(s, "%c", (d->dma.status & BIT(0)) ?
8059f7c0a9SVladimir Kondratiev 					_s : (vring->ctx[i].skb ? _h : 'h'));
812be7d22fSVladimir Kondratiev 		}
828fe59627SVladimir Kondratiev 		seq_puts(s, "\n");
832be7d22fSVladimir Kondratiev 	}
848fe59627SVladimir Kondratiev 	seq_puts(s, "}\n");
852be7d22fSVladimir Kondratiev }
862be7d22fSVladimir Kondratiev 
872be7d22fSVladimir Kondratiev static int wil_vring_debugfs_show(struct seq_file *s, void *data)
882be7d22fSVladimir Kondratiev {
892be7d22fSVladimir Kondratiev 	uint i;
902be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
912be7d22fSVladimir Kondratiev 
9259f7c0a9SVladimir Kondratiev 	wil_print_vring(s, wil, "rx", &wil->vring_rx, 'S', '_');
932be7d22fSVladimir Kondratiev 
942be7d22fSVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
958fe59627SVladimir Kondratiev 		struct vring *vring = &wil->vring_tx[i];
967c0acf86SVladimir Kondratiev 		struct vring_tx_data *txdata = &wil->vring_tx_data[i];
977c0acf86SVladimir Kondratiev 
982be7d22fSVladimir Kondratiev 		if (vring->va) {
993df2cd36SVladimir Kondratiev 			int cid = wil->vring2cid_tid[i][0];
1003df2cd36SVladimir Kondratiev 			int tid = wil->vring2cid_tid[i][1];
10167c3e1b4SVladimir Kondratiev 			u32 swhead = vring->swhead;
10267c3e1b4SVladimir Kondratiev 			u32 swtail = vring->swtail;
10367c3e1b4SVladimir Kondratiev 			int used = (vring->size + swhead - swtail)
10467c3e1b4SVladimir Kondratiev 				   % vring->size;
10567c3e1b4SVladimir Kondratiev 			int avail = vring->size - used - 1;
1062be7d22fSVladimir Kondratiev 			char name[10];
1078f55cbecSBoris Sorochkin 			char sidle[10];
1087c0acf86SVladimir Kondratiev 			/* performance monitoring */
1097c0acf86SVladimir Kondratiev 			cycles_t now = get_cycles();
110c20e7789SChen Gang 			uint64_t idle = txdata->idle * 100;
111c20e7789SChen Gang 			uint64_t total = now - txdata->begin;
1127c0acf86SVladimir Kondratiev 
1138f55cbecSBoris Sorochkin 			if (total != 0) {
114e48b1790SVladimir Kondratiev 				do_div(idle, total);
1158f55cbecSBoris Sorochkin 				snprintf(sidle, sizeof(sidle), "%3d%%",
1168f55cbecSBoris Sorochkin 					 (int)idle);
1178f55cbecSBoris Sorochkin 			} else {
1188f55cbecSBoris Sorochkin 				snprintf(sidle, sizeof(sidle), "N/A");
1198f55cbecSBoris Sorochkin 			}
1207c0acf86SVladimir Kondratiev 			txdata->begin = now;
1217c0acf86SVladimir Kondratiev 			txdata->idle = 0ULL;
1227c0acf86SVladimir Kondratiev 
1232be7d22fSVladimir Kondratiev 			snprintf(name, sizeof(name), "tx_%2d", i);
1243df2cd36SVladimir Kondratiev 
12541d6b093SVladimir Kondratiev 			if (cid < WIL6210_MAX_CID)
1263277213fSVladimir Kondratiev 				seq_printf(s,
127230d8442SVladimir Kondratiev 					   "\n%pM CID %d TID %d 1x%s BACK([%u] %u TU A%s) [%3d|%3d] idle %s\n",
1283277213fSVladimir Kondratiev 					   wil->sta[cid].addr, cid, tid,
129230d8442SVladimir Kondratiev 					   txdata->dot1x_open ? "+" : "-",
13041d6b093SVladimir Kondratiev 					   txdata->agg_wsize,
13141d6b093SVladimir Kondratiev 					   txdata->agg_timeout,
132cbcf5866SVladimir Kondratiev 					   txdata->agg_amsdu ? "+" : "-",
1338f55cbecSBoris Sorochkin 					   used, avail, sidle);
13441d6b093SVladimir Kondratiev 			else
13541d6b093SVladimir Kondratiev 				seq_printf(s,
136230d8442SVladimir Kondratiev 					   "\nBroadcast 1x%s [%3d|%3d] idle %s\n",
137230d8442SVladimir Kondratiev 					   txdata->dot1x_open ? "+" : "-",
13841d6b093SVladimir Kondratiev 					   used, avail, sidle);
1397c0acf86SVladimir Kondratiev 
14059f7c0a9SVladimir Kondratiev 			wil_print_vring(s, wil, name, vring, '_', 'H');
1412be7d22fSVladimir Kondratiev 		}
1422be7d22fSVladimir Kondratiev 	}
1432be7d22fSVladimir Kondratiev 
1442be7d22fSVladimir Kondratiev 	return 0;
1452be7d22fSVladimir Kondratiev }
1462be7d22fSVladimir Kondratiev 
1472be7d22fSVladimir Kondratiev static int wil_vring_seq_open(struct inode *inode, struct file *file)
1482be7d22fSVladimir Kondratiev {
1492be7d22fSVladimir Kondratiev 	return single_open(file, wil_vring_debugfs_show, inode->i_private);
1502be7d22fSVladimir Kondratiev }
1512be7d22fSVladimir Kondratiev 
1522be7d22fSVladimir Kondratiev static const struct file_operations fops_vring = {
1532be7d22fSVladimir Kondratiev 	.open		= wil_vring_seq_open,
1542be7d22fSVladimir Kondratiev 	.release	= single_release,
1552be7d22fSVladimir Kondratiev 	.read		= seq_read,
1562be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
1572be7d22fSVladimir Kondratiev };
1582be7d22fSVladimir Kondratiev 
1592be7d22fSVladimir Kondratiev static void wil_print_ring(struct seq_file *s, const char *prefix,
1602be7d22fSVladimir Kondratiev 			   void __iomem *off)
1612be7d22fSVladimir Kondratiev {
1622be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
1632be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring r;
1642be7d22fSVladimir Kondratiev 	int rsize;
1652be7d22fSVladimir Kondratiev 	uint i;
1662be7d22fSVladimir Kondratiev 
1672be7d22fSVladimir Kondratiev 	wil_memcpy_fromio_32(&r, off, sizeof(r));
1682be7d22fSVladimir Kondratiev 	wil_mbox_ring_le2cpus(&r);
1692be7d22fSVladimir Kondratiev 	/*
1702be7d22fSVladimir Kondratiev 	 * we just read memory block from NIC. This memory may be
1712be7d22fSVladimir Kondratiev 	 * garbage. Check validity before using it.
1722be7d22fSVladimir Kondratiev 	 */
1732be7d22fSVladimir Kondratiev 	rsize = r.size / sizeof(struct wil6210_mbox_ring_desc);
1742be7d22fSVladimir Kondratiev 
1752be7d22fSVladimir Kondratiev 	seq_printf(s, "ring %s = {\n", prefix);
1762be7d22fSVladimir Kondratiev 	seq_printf(s, "  base = 0x%08x\n", r.base);
1772be7d22fSVladimir Kondratiev 	seq_printf(s, "  size = 0x%04x bytes -> %d entries\n", r.size, rsize);
1782be7d22fSVladimir Kondratiev 	seq_printf(s, "  tail = 0x%08x\n", r.tail);
1792be7d22fSVladimir Kondratiev 	seq_printf(s, "  head = 0x%08x\n", r.head);
1802be7d22fSVladimir Kondratiev 	seq_printf(s, "  entry size = %d\n", r.entry_size);
1812be7d22fSVladimir Kondratiev 
1822be7d22fSVladimir Kondratiev 	if (r.size % sizeof(struct wil6210_mbox_ring_desc)) {
1832be7d22fSVladimir Kondratiev 		seq_printf(s, "  ??? size is not multiple of %zd, garbage?\n",
1842be7d22fSVladimir Kondratiev 			   sizeof(struct wil6210_mbox_ring_desc));
1852be7d22fSVladimir Kondratiev 		goto out;
1862be7d22fSVladimir Kondratiev 	}
1872be7d22fSVladimir Kondratiev 
1882be7d22fSVladimir Kondratiev 	if (!wmi_addr(wil, r.base) ||
1892be7d22fSVladimir Kondratiev 	    !wmi_addr(wil, r.tail) ||
1902be7d22fSVladimir Kondratiev 	    !wmi_addr(wil, r.head)) {
1918fe59627SVladimir Kondratiev 		seq_puts(s, "  ??? pointers are garbage?\n");
1922be7d22fSVladimir Kondratiev 		goto out;
1932be7d22fSVladimir Kondratiev 	}
1942be7d22fSVladimir Kondratiev 
1952be7d22fSVladimir Kondratiev 	for (i = 0; i < rsize; i++) {
1962be7d22fSVladimir Kondratiev 		struct wil6210_mbox_ring_desc d;
1972be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr hdr;
1982be7d22fSVladimir Kondratiev 		size_t delta = i * sizeof(d);
1992be7d22fSVladimir Kondratiev 		void __iomem *x = wil->csr + HOSTADDR(r.base) + delta;
2002be7d22fSVladimir Kondratiev 
2012be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(&d, x, sizeof(d));
2022be7d22fSVladimir Kondratiev 
2032be7d22fSVladimir Kondratiev 		seq_printf(s, "  [%2x] %s %s%s 0x%08x", i,
2042be7d22fSVladimir Kondratiev 			   d.sync ? "F" : "E",
2052be7d22fSVladimir Kondratiev 			   (r.tail - r.base == delta) ? "t" : " ",
2062be7d22fSVladimir Kondratiev 			   (r.head - r.base == delta) ? "h" : " ",
2072be7d22fSVladimir Kondratiev 			   le32_to_cpu(d.addr));
2082be7d22fSVladimir Kondratiev 		if (0 == wmi_read_hdr(wil, d.addr, &hdr)) {
2092be7d22fSVladimir Kondratiev 			u16 len = le16_to_cpu(hdr.len);
2108fe59627SVladimir Kondratiev 
2112be7d22fSVladimir Kondratiev 			seq_printf(s, " -> %04x %04x %04x %02x\n",
2122be7d22fSVladimir Kondratiev 				   le16_to_cpu(hdr.seq), len,
2132be7d22fSVladimir Kondratiev 				   le16_to_cpu(hdr.type), hdr.flags);
2142be7d22fSVladimir Kondratiev 			if (len <= MAX_MBOXITEM_SIZE) {
2152be7d22fSVladimir Kondratiev 				int n = 0;
2165d21608aSLarry Finger 				char printbuf[16 * 3 + 2];
2172be7d22fSVladimir Kondratiev 				unsigned char databuf[MAX_MBOXITEM_SIZE];
2182be7d22fSVladimir Kondratiev 				void __iomem *src = wmi_buffer(wil, d.addr) +
2192be7d22fSVladimir Kondratiev 					sizeof(struct wil6210_mbox_hdr);
2202be7d22fSVladimir Kondratiev 				/*
2212be7d22fSVladimir Kondratiev 				 * No need to check @src for validity -
2222be7d22fSVladimir Kondratiev 				 * we already validated @d.addr while
2232be7d22fSVladimir Kondratiev 				 * reading header
2242be7d22fSVladimir Kondratiev 				 */
2252be7d22fSVladimir Kondratiev 				wil_memcpy_fromio_32(databuf, src, len);
2262be7d22fSVladimir Kondratiev 				while (n < len) {
2272be7d22fSVladimir Kondratiev 					int l = min(len - n, 16);
2288fe59627SVladimir Kondratiev 
2292be7d22fSVladimir Kondratiev 					hex_dump_to_buffer(databuf + n, l,
2302be7d22fSVladimir Kondratiev 							   16, 1, printbuf,
2312be7d22fSVladimir Kondratiev 							   sizeof(printbuf),
2322be7d22fSVladimir Kondratiev 							   false);
2332be7d22fSVladimir Kondratiev 					seq_printf(s, "      : %s\n", printbuf);
2342be7d22fSVladimir Kondratiev 					n += l;
2352be7d22fSVladimir Kondratiev 				}
2362be7d22fSVladimir Kondratiev 			}
2372be7d22fSVladimir Kondratiev 		} else {
2388fe59627SVladimir Kondratiev 			seq_puts(s, "\n");
2392be7d22fSVladimir Kondratiev 		}
2402be7d22fSVladimir Kondratiev 	}
2412be7d22fSVladimir Kondratiev  out:
2428fe59627SVladimir Kondratiev 	seq_puts(s, "}\n");
2432be7d22fSVladimir Kondratiev }
2442be7d22fSVladimir Kondratiev 
2452be7d22fSVladimir Kondratiev static int wil_mbox_debugfs_show(struct seq_file *s, void *data)
2462be7d22fSVladimir Kondratiev {
2472be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
2482be7d22fSVladimir Kondratiev 
2492be7d22fSVladimir Kondratiev 	wil_print_ring(s, "tx", wil->csr + HOST_MBOX +
2502be7d22fSVladimir Kondratiev 		       offsetof(struct wil6210_mbox_ctl, tx));
2512be7d22fSVladimir Kondratiev 	wil_print_ring(s, "rx", wil->csr + HOST_MBOX +
2522be7d22fSVladimir Kondratiev 		       offsetof(struct wil6210_mbox_ctl, rx));
2532be7d22fSVladimir Kondratiev 
2542be7d22fSVladimir Kondratiev 	return 0;
2552be7d22fSVladimir Kondratiev }
2562be7d22fSVladimir Kondratiev 
2572be7d22fSVladimir Kondratiev static int wil_mbox_seq_open(struct inode *inode, struct file *file)
2582be7d22fSVladimir Kondratiev {
2592be7d22fSVladimir Kondratiev 	return single_open(file, wil_mbox_debugfs_show, inode->i_private);
2602be7d22fSVladimir Kondratiev }
2612be7d22fSVladimir Kondratiev 
2622be7d22fSVladimir Kondratiev static const struct file_operations fops_mbox = {
2632be7d22fSVladimir Kondratiev 	.open		= wil_mbox_seq_open,
2642be7d22fSVladimir Kondratiev 	.release	= single_release,
2652be7d22fSVladimir Kondratiev 	.read		= seq_read,
2662be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
2672be7d22fSVladimir Kondratiev };
2682be7d22fSVladimir Kondratiev 
2692be7d22fSVladimir Kondratiev static int wil_debugfs_iomem_x32_set(void *data, u64 val)
2702be7d22fSVladimir Kondratiev {
2712be7d22fSVladimir Kondratiev 	iowrite32(val, (void __iomem *)data);
2722be7d22fSVladimir Kondratiev 	wmb(); /* make sure write propagated to HW */
2732be7d22fSVladimir Kondratiev 
2742be7d22fSVladimir Kondratiev 	return 0;
2752be7d22fSVladimir Kondratiev }
2762be7d22fSVladimir Kondratiev 
2772be7d22fSVladimir Kondratiev static int wil_debugfs_iomem_x32_get(void *data, u64 *val)
2782be7d22fSVladimir Kondratiev {
2792be7d22fSVladimir Kondratiev 	*val = ioread32((void __iomem *)data);
2802be7d22fSVladimir Kondratiev 
2812be7d22fSVladimir Kondratiev 	return 0;
2822be7d22fSVladimir Kondratiev }
2832be7d22fSVladimir Kondratiev 
2842be7d22fSVladimir Kondratiev DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, wil_debugfs_iomem_x32_get,
2852be7d22fSVladimir Kondratiev 			wil_debugfs_iomem_x32_set, "0x%08llx\n");
2862be7d22fSVladimir Kondratiev 
2872be7d22fSVladimir Kondratiev static struct dentry *wil_debugfs_create_iomem_x32(const char *name,
2880ecc833bSAl Viro 						   umode_t mode,
2892be7d22fSVladimir Kondratiev 						   struct dentry *parent,
290b7cde470SVladimir Kondratiev 						   void *value)
2912be7d22fSVladimir Kondratiev {
292b7cde470SVladimir Kondratiev 	return debugfs_create_file(name, mode, parent, value,
2932be7d22fSVladimir Kondratiev 				   &fops_iomem_x32);
2942be7d22fSVladimir Kondratiev }
2952be7d22fSVladimir Kondratiev 
2963de6cf20SVladimir Kondratiev static int wil_debugfs_ulong_set(void *data, u64 val)
2973de6cf20SVladimir Kondratiev {
2983de6cf20SVladimir Kondratiev 	*(ulong *)data = val;
2993de6cf20SVladimir Kondratiev 	return 0;
3003de6cf20SVladimir Kondratiev }
3018fe59627SVladimir Kondratiev 
3023de6cf20SVladimir Kondratiev static int wil_debugfs_ulong_get(void *data, u64 *val)
3033de6cf20SVladimir Kondratiev {
3043de6cf20SVladimir Kondratiev 	*val = *(ulong *)data;
3053de6cf20SVladimir Kondratiev 	return 0;
3063de6cf20SVladimir Kondratiev }
3078fe59627SVladimir Kondratiev 
3083de6cf20SVladimir Kondratiev DEFINE_SIMPLE_ATTRIBUTE(wil_fops_ulong, wil_debugfs_ulong_get,
309*8ea06188SVladimir Kondratiev 			wil_debugfs_ulong_set, "0x%llx\n");
3103de6cf20SVladimir Kondratiev 
3113de6cf20SVladimir Kondratiev static struct dentry *wil_debugfs_create_ulong(const char *name, umode_t mode,
3123de6cf20SVladimir Kondratiev 					       struct dentry *parent,
3133de6cf20SVladimir Kondratiev 					       ulong *value)
3143de6cf20SVladimir Kondratiev {
3153de6cf20SVladimir Kondratiev 	return debugfs_create_file(name, mode, parent, value, &wil_fops_ulong);
3163de6cf20SVladimir Kondratiev }
3173de6cf20SVladimir Kondratiev 
318b7cde470SVladimir Kondratiev /**
319b7cde470SVladimir Kondratiev  * wil6210_debugfs_init_offset - create set of debugfs files
320b7cde470SVladimir Kondratiev  * @wil - driver's context, used for printing
321b7cde470SVladimir Kondratiev  * @dbg - directory on the debugfs, where files will be created
322b7cde470SVladimir Kondratiev  * @base - base address used in address calculation
323b7cde470SVladimir Kondratiev  * @tbl - table with file descriptions. Should be terminated with empty element.
324b7cde470SVladimir Kondratiev  *
325b7cde470SVladimir Kondratiev  * Creates files accordingly to the @tbl.
326b7cde470SVladimir Kondratiev  */
327b7cde470SVladimir Kondratiev static void wil6210_debugfs_init_offset(struct wil6210_priv *wil,
328b7cde470SVladimir Kondratiev 					struct dentry *dbg, void *base,
329b7cde470SVladimir Kondratiev 					const struct dbg_off * const tbl)
330b7cde470SVladimir Kondratiev {
331b7cde470SVladimir Kondratiev 	int i;
332b7cde470SVladimir Kondratiev 
333b7cde470SVladimir Kondratiev 	for (i = 0; tbl[i].name; i++) {
334867fa0d4SVladimir Kondratiev 		struct dentry *f;
335b7cde470SVladimir Kondratiev 
336b7cde470SVladimir Kondratiev 		switch (tbl[i].type) {
337b7cde470SVladimir Kondratiev 		case doff_u32:
338b7cde470SVladimir Kondratiev 			f = debugfs_create_u32(tbl[i].name, tbl[i].mode, dbg,
339b7cde470SVladimir Kondratiev 					       base + tbl[i].off);
340b7cde470SVladimir Kondratiev 			break;
341b7cde470SVladimir Kondratiev 		case doff_x32:
342b7cde470SVladimir Kondratiev 			f = debugfs_create_x32(tbl[i].name, tbl[i].mode, dbg,
343b7cde470SVladimir Kondratiev 					       base + tbl[i].off);
344b7cde470SVladimir Kondratiev 			break;
345b7cde470SVladimir Kondratiev 		case doff_ulong:
346b7cde470SVladimir Kondratiev 			f = wil_debugfs_create_ulong(tbl[i].name, tbl[i].mode,
347b7cde470SVladimir Kondratiev 						     dbg, base + tbl[i].off);
348b7cde470SVladimir Kondratiev 			break;
349b7cde470SVladimir Kondratiev 		case doff_io32:
350b7cde470SVladimir Kondratiev 			f = wil_debugfs_create_iomem_x32(tbl[i].name,
351b7cde470SVladimir Kondratiev 							 tbl[i].mode, dbg,
352b7cde470SVladimir Kondratiev 							 base + tbl[i].off);
353b7cde470SVladimir Kondratiev 			break;
354867fa0d4SVladimir Kondratiev 		default:
355867fa0d4SVladimir Kondratiev 			f = ERR_PTR(-EINVAL);
356b7cde470SVladimir Kondratiev 		}
357b7cde470SVladimir Kondratiev 		if (IS_ERR_OR_NULL(f))
358b7cde470SVladimir Kondratiev 			wil_err(wil, "Create file \"%s\": err %ld\n",
359b7cde470SVladimir Kondratiev 				tbl[i].name, PTR_ERR(f));
360b7cde470SVladimir Kondratiev 	}
361b7cde470SVladimir Kondratiev }
362b7cde470SVladimir Kondratiev 
363b7cde470SVladimir Kondratiev static const struct dbg_off isr_off[] = {
364b7cde470SVladimir Kondratiev 	{"ICC", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICC), doff_io32},
365b7cde470SVladimir Kondratiev 	{"ICR", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICR), doff_io32},
366b7cde470SVladimir Kondratiev 	{"ICM", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICM), doff_io32},
367b7cde470SVladimir Kondratiev 	{"ICS",		  S_IWUSR, offsetof(struct RGF_ICR, ICS), doff_io32},
368b7cde470SVladimir Kondratiev 	{"IMV", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, IMV), doff_io32},
369b7cde470SVladimir Kondratiev 	{"IMS",		  S_IWUSR, offsetof(struct RGF_ICR, IMS), doff_io32},
370b7cde470SVladimir Kondratiev 	{"IMC",		  S_IWUSR, offsetof(struct RGF_ICR, IMC), doff_io32},
371b7cde470SVladimir Kondratiev 	{},
372b7cde470SVladimir Kondratiev };
3738fe59627SVladimir Kondratiev 
3742be7d22fSVladimir Kondratiev static int wil6210_debugfs_create_ISR(struct wil6210_priv *wil,
3752be7d22fSVladimir Kondratiev 				      const char *name,
3762be7d22fSVladimir Kondratiev 				      struct dentry *parent, u32 off)
3772be7d22fSVladimir Kondratiev {
3782be7d22fSVladimir Kondratiev 	struct dentry *d = debugfs_create_dir(name, parent);
3792be7d22fSVladimir Kondratiev 
3802be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(d))
3812be7d22fSVladimir Kondratiev 		return -ENODEV;
3822be7d22fSVladimir Kondratiev 
383b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr + off,
384b7cde470SVladimir Kondratiev 				    isr_off);
3852be7d22fSVladimir Kondratiev 
3862be7d22fSVladimir Kondratiev 	return 0;
3872be7d22fSVladimir Kondratiev }
3882be7d22fSVladimir Kondratiev 
389b7cde470SVladimir Kondratiev static const struct dbg_off pseudo_isr_off[] = {
390b7cde470SVladimir Kondratiev 	{"CAUSE",   S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE), doff_io32},
391b7cde470SVladimir Kondratiev 	{"MASK_SW", S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW), doff_io32},
392b7cde470SVladimir Kondratiev 	{"MASK_FW", S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_FW), doff_io32},
393b7cde470SVladimir Kondratiev 	{},
394b7cde470SVladimir Kondratiev };
395b7cde470SVladimir Kondratiev 
3962be7d22fSVladimir Kondratiev static int wil6210_debugfs_create_pseudo_ISR(struct wil6210_priv *wil,
3972be7d22fSVladimir Kondratiev 					     struct dentry *parent)
3982be7d22fSVladimir Kondratiev {
3992be7d22fSVladimir Kondratiev 	struct dentry *d = debugfs_create_dir("PSEUDO_ISR", parent);
4002be7d22fSVladimir Kondratiev 
4012be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(d))
4022be7d22fSVladimir Kondratiev 		return -ENODEV;
4032be7d22fSVladimir Kondratiev 
404b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
405b7cde470SVladimir Kondratiev 				    pseudo_isr_off);
4062be7d22fSVladimir Kondratiev 
4072be7d22fSVladimir Kondratiev 	return 0;
4082be7d22fSVladimir Kondratiev }
4092be7d22fSVladimir Kondratiev 
41078366f69SVladimir Kondratiev static const struct dbg_off lgc_itr_cnt_off[] = {
411b7cde470SVladimir Kondratiev 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_TRSH), doff_io32},
412b7cde470SVladimir Kondratiev 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_DATA), doff_io32},
413b7cde470SVladimir Kondratiev 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_CRL), doff_io32},
414b7cde470SVladimir Kondratiev 	{},
415b7cde470SVladimir Kondratiev };
416b7cde470SVladimir Kondratiev 
41778366f69SVladimir Kondratiev static const struct dbg_off tx_itr_cnt_off[] = {
41878366f69SVladimir Kondratiev 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_TRSH),
41978366f69SVladimir Kondratiev 	 doff_io32},
42078366f69SVladimir Kondratiev 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_DATA),
42178366f69SVladimir Kondratiev 	 doff_io32},
42278366f69SVladimir Kondratiev 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_CTL),
42378366f69SVladimir Kondratiev 	 doff_io32},
42478366f69SVladimir Kondratiev 	{"IDL_TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_TRSH),
42578366f69SVladimir Kondratiev 	 doff_io32},
42678366f69SVladimir Kondratiev 	{"IDL_DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_DATA),
42778366f69SVladimir Kondratiev 	 doff_io32},
42878366f69SVladimir Kondratiev 	{"IDL_CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_CTL),
42978366f69SVladimir Kondratiev 	 doff_io32},
43078366f69SVladimir Kondratiev 	{},
43178366f69SVladimir Kondratiev };
43278366f69SVladimir Kondratiev 
43378366f69SVladimir Kondratiev static const struct dbg_off rx_itr_cnt_off[] = {
43478366f69SVladimir Kondratiev 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_TRSH),
43578366f69SVladimir Kondratiev 	 doff_io32},
43678366f69SVladimir Kondratiev 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_DATA),
43778366f69SVladimir Kondratiev 	 doff_io32},
43878366f69SVladimir Kondratiev 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_CTL),
43978366f69SVladimir Kondratiev 	 doff_io32},
44078366f69SVladimir Kondratiev 	{"IDL_TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_TRSH),
44178366f69SVladimir Kondratiev 	 doff_io32},
44278366f69SVladimir Kondratiev 	{"IDL_DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_DATA),
44378366f69SVladimir Kondratiev 	 doff_io32},
44478366f69SVladimir Kondratiev 	{"IDL_CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_CTL),
44578366f69SVladimir Kondratiev 	 doff_io32},
44678366f69SVladimir Kondratiev 	{},
44778366f69SVladimir Kondratiev };
44878366f69SVladimir Kondratiev 
4492be7d22fSVladimir Kondratiev static int wil6210_debugfs_create_ITR_CNT(struct wil6210_priv *wil,
4502be7d22fSVladimir Kondratiev 					  struct dentry *parent)
4512be7d22fSVladimir Kondratiev {
45278366f69SVladimir Kondratiev 	struct dentry *d, *dtx, *drx;
4532be7d22fSVladimir Kondratiev 
45478366f69SVladimir Kondratiev 	d = debugfs_create_dir("ITR_CNT", parent);
4552be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(d))
4562be7d22fSVladimir Kondratiev 		return -ENODEV;
4572be7d22fSVladimir Kondratiev 
45878366f69SVladimir Kondratiev 	dtx = debugfs_create_dir("TX", d);
45978366f69SVladimir Kondratiev 	drx = debugfs_create_dir("RX", d);
46078366f69SVladimir Kondratiev 	if (IS_ERR_OR_NULL(dtx) || IS_ERR_OR_NULL(drx))
46178366f69SVladimir Kondratiev 		return -ENODEV;
4622be7d22fSVladimir Kondratiev 
46378366f69SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
46478366f69SVladimir Kondratiev 				    lgc_itr_cnt_off);
46578366f69SVladimir Kondratiev 
46678366f69SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dtx, (void * __force)wil->csr,
46778366f69SVladimir Kondratiev 				    tx_itr_cnt_off);
46878366f69SVladimir Kondratiev 
46978366f69SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, drx, (void * __force)wil->csr,
47078366f69SVladimir Kondratiev 				    rx_itr_cnt_off);
4712be7d22fSVladimir Kondratiev 	return 0;
4722be7d22fSVladimir Kondratiev }
4732be7d22fSVladimir Kondratiev 
4742be7d22fSVladimir Kondratiev static int wil_memread_debugfs_show(struct seq_file *s, void *data)
4752be7d22fSVladimir Kondratiev {
4762be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
4772be7d22fSVladimir Kondratiev 	void __iomem *a = wmi_buffer(wil, cpu_to_le32(mem_addr));
4782be7d22fSVladimir Kondratiev 
4792be7d22fSVladimir Kondratiev 	if (a)
4802be7d22fSVladimir Kondratiev 		seq_printf(s, "[0x%08x] = 0x%08x\n", mem_addr, ioread32(a));
4812be7d22fSVladimir Kondratiev 	else
4822be7d22fSVladimir Kondratiev 		seq_printf(s, "[0x%08x] = INVALID\n", mem_addr);
4832be7d22fSVladimir Kondratiev 
4842be7d22fSVladimir Kondratiev 	return 0;
4852be7d22fSVladimir Kondratiev }
4862be7d22fSVladimir Kondratiev 
4872be7d22fSVladimir Kondratiev static int wil_memread_seq_open(struct inode *inode, struct file *file)
4882be7d22fSVladimir Kondratiev {
4892be7d22fSVladimir Kondratiev 	return single_open(file, wil_memread_debugfs_show, inode->i_private);
4902be7d22fSVladimir Kondratiev }
4912be7d22fSVladimir Kondratiev 
4922be7d22fSVladimir Kondratiev static const struct file_operations fops_memread = {
4932be7d22fSVladimir Kondratiev 	.open		= wil_memread_seq_open,
4942be7d22fSVladimir Kondratiev 	.release	= single_release,
4952be7d22fSVladimir Kondratiev 	.read		= seq_read,
4962be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
4972be7d22fSVladimir Kondratiev };
4982be7d22fSVladimir Kondratiev 
4992be7d22fSVladimir Kondratiev static ssize_t wil_read_file_ioblob(struct file *file, char __user *user_buf,
5002be7d22fSVladimir Kondratiev 				    size_t count, loff_t *ppos)
5012be7d22fSVladimir Kondratiev {
5022be7d22fSVladimir Kondratiev 	enum { max_count = 4096 };
5032be7d22fSVladimir Kondratiev 	struct debugfs_blob_wrapper *blob = file->private_data;
5042be7d22fSVladimir Kondratiev 	loff_t pos = *ppos;
5052be7d22fSVladimir Kondratiev 	size_t available = blob->size;
5062be7d22fSVladimir Kondratiev 	void *buf;
5072be7d22fSVladimir Kondratiev 	size_t ret;
5082be7d22fSVladimir Kondratiev 
5092be7d22fSVladimir Kondratiev 	if (pos < 0)
5102be7d22fSVladimir Kondratiev 		return -EINVAL;
5112be7d22fSVladimir Kondratiev 
5122be7d22fSVladimir Kondratiev 	if (pos >= available || !count)
5132be7d22fSVladimir Kondratiev 		return 0;
5142be7d22fSVladimir Kondratiev 
5152be7d22fSVladimir Kondratiev 	if (count > available - pos)
5162be7d22fSVladimir Kondratiev 		count = available - pos;
5172be7d22fSVladimir Kondratiev 	if (count > max_count)
5182be7d22fSVladimir Kondratiev 		count = max_count;
5192be7d22fSVladimir Kondratiev 
5202be7d22fSVladimir Kondratiev 	buf = kmalloc(count, GFP_KERNEL);
5212be7d22fSVladimir Kondratiev 	if (!buf)
5222be7d22fSVladimir Kondratiev 		return -ENOMEM;
5232be7d22fSVladimir Kondratiev 
5242be7d22fSVladimir Kondratiev 	wil_memcpy_fromio_32(buf, (const volatile void __iomem *)blob->data +
5252be7d22fSVladimir Kondratiev 			     pos, count);
5262be7d22fSVladimir Kondratiev 
5272be7d22fSVladimir Kondratiev 	ret = copy_to_user(user_buf, buf, count);
5282be7d22fSVladimir Kondratiev 	kfree(buf);
5292be7d22fSVladimir Kondratiev 	if (ret == count)
5302be7d22fSVladimir Kondratiev 		return -EFAULT;
5312be7d22fSVladimir Kondratiev 
5322be7d22fSVladimir Kondratiev 	count -= ret;
5332be7d22fSVladimir Kondratiev 	*ppos = pos + count;
5342be7d22fSVladimir Kondratiev 
5352be7d22fSVladimir Kondratiev 	return count;
5362be7d22fSVladimir Kondratiev }
5372be7d22fSVladimir Kondratiev 
5382be7d22fSVladimir Kondratiev static const struct file_operations fops_ioblob = {
5392be7d22fSVladimir Kondratiev 	.read =		wil_read_file_ioblob,
54093ecbd64SWei Yongjun 	.open =		simple_open,
5412be7d22fSVladimir Kondratiev 	.llseek =	default_llseek,
5422be7d22fSVladimir Kondratiev };
5432be7d22fSVladimir Kondratiev 
5442be7d22fSVladimir Kondratiev static
5452be7d22fSVladimir Kondratiev struct dentry *wil_debugfs_create_ioblob(const char *name,
5460ecc833bSAl Viro 					 umode_t mode,
5472be7d22fSVladimir Kondratiev 					 struct dentry *parent,
5482be7d22fSVladimir Kondratiev 					 struct debugfs_blob_wrapper *blob)
5492be7d22fSVladimir Kondratiev {
5502be7d22fSVladimir Kondratiev 	return debugfs_create_file(name, mode, parent, blob, &fops_ioblob);
5512be7d22fSVladimir Kondratiev }
5528fe59627SVladimir Kondratiev 
5532be7d22fSVladimir Kondratiev /*---reset---*/
5542be7d22fSVladimir Kondratiev static ssize_t wil_write_file_reset(struct file *file, const char __user *buf,
5552be7d22fSVladimir Kondratiev 				    size_t len, loff_t *ppos)
5562be7d22fSVladimir Kondratiev {
5572be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
5582be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
5592be7d22fSVladimir Kondratiev 
5602be7d22fSVladimir Kondratiev 	/**
5612be7d22fSVladimir Kondratiev 	 * BUG:
5622be7d22fSVladimir Kondratiev 	 * this code does NOT sync device state with the rest of system
5632be7d22fSVladimir Kondratiev 	 * use with care, debug only!!!
5642be7d22fSVladimir Kondratiev 	 */
5652be7d22fSVladimir Kondratiev 	rtnl_lock();
5662be7d22fSVladimir Kondratiev 	dev_close(ndev);
5672be7d22fSVladimir Kondratiev 	ndev->flags &= ~IFF_UP;
5682be7d22fSVladimir Kondratiev 	rtnl_unlock();
5692cd0f021SVladimir Kondratiev 	wil_reset(wil, true);
5702be7d22fSVladimir Kondratiev 
5712be7d22fSVladimir Kondratiev 	return len;
5722be7d22fSVladimir Kondratiev }
5732be7d22fSVladimir Kondratiev 
5742be7d22fSVladimir Kondratiev static const struct file_operations fops_reset = {
5752be7d22fSVladimir Kondratiev 	.write = wil_write_file_reset,
57693ecbd64SWei Yongjun 	.open  = simple_open,
5772be7d22fSVladimir Kondratiev };
5788fe59627SVladimir Kondratiev 
5790b39aaf2SVladimir Kondratiev /*---write channel 1..4 to rxon for it, 0 to rxoff---*/
5800b39aaf2SVladimir Kondratiev static ssize_t wil_write_file_rxon(struct file *file, const char __user *buf,
5810b39aaf2SVladimir Kondratiev 				   size_t len, loff_t *ppos)
5820b39aaf2SVladimir Kondratiev {
5830b39aaf2SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
5840b39aaf2SVladimir Kondratiev 	int rc;
5850b39aaf2SVladimir Kondratiev 	long channel;
5860b39aaf2SVladimir Kondratiev 	bool on;
5870b39aaf2SVladimir Kondratiev 
5880b39aaf2SVladimir Kondratiev 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
5898fe59627SVladimir Kondratiev 
5900b39aaf2SVladimir Kondratiev 	if (!kbuf)
5910b39aaf2SVladimir Kondratiev 		return -ENOMEM;
592359ee627SVladimir Kondratiev 	if (copy_from_user(kbuf, buf, len)) {
593359ee627SVladimir Kondratiev 		kfree(kbuf);
5940b39aaf2SVladimir Kondratiev 		return -EIO;
595359ee627SVladimir Kondratiev 	}
5960b39aaf2SVladimir Kondratiev 
5970b39aaf2SVladimir Kondratiev 	kbuf[len] = '\0';
5980b39aaf2SVladimir Kondratiev 	rc = kstrtol(kbuf, 0, &channel);
5990b39aaf2SVladimir Kondratiev 	kfree(kbuf);
6000b39aaf2SVladimir Kondratiev 	if (rc)
6010b39aaf2SVladimir Kondratiev 		return rc;
6020b39aaf2SVladimir Kondratiev 
6030b39aaf2SVladimir Kondratiev 	if ((channel < 0) || (channel > 4)) {
6040b39aaf2SVladimir Kondratiev 		wil_err(wil, "Invalid channel %ld\n", channel);
6050b39aaf2SVladimir Kondratiev 		return -EINVAL;
6060b39aaf2SVladimir Kondratiev 	}
6070b39aaf2SVladimir Kondratiev 	on = !!channel;
6080b39aaf2SVladimir Kondratiev 
6090b39aaf2SVladimir Kondratiev 	if (on) {
6100b39aaf2SVladimir Kondratiev 		rc = wmi_set_channel(wil, (int)channel);
6110b39aaf2SVladimir Kondratiev 		if (rc)
6120b39aaf2SVladimir Kondratiev 			return rc;
6130b39aaf2SVladimir Kondratiev 	}
6140b39aaf2SVladimir Kondratiev 
6150b39aaf2SVladimir Kondratiev 	rc = wmi_rxon(wil, on);
6160b39aaf2SVladimir Kondratiev 	if (rc)
6170b39aaf2SVladimir Kondratiev 		return rc;
6180b39aaf2SVladimir Kondratiev 
6190b39aaf2SVladimir Kondratiev 	return len;
6200b39aaf2SVladimir Kondratiev }
6210b39aaf2SVladimir Kondratiev 
6220b39aaf2SVladimir Kondratiev static const struct file_operations fops_rxon = {
6230b39aaf2SVladimir Kondratiev 	.write = wil_write_file_rxon,
6240b39aaf2SVladimir Kondratiev 	.open  = simple_open,
6250b39aaf2SVladimir Kondratiev };
6268fe59627SVladimir Kondratiev 
62749cb5dfbSVladimir Kondratiev /* block ack control, write:
62849cb5dfbSVladimir Kondratiev  * - "add <ringid> <agg_size> <timeout>" to trigger ADDBA
62949cb5dfbSVladimir Kondratiev  * - "del_tx <ringid> <reason>" to trigger DELBA for Tx side
63049cb5dfbSVladimir Kondratiev  * - "del_rx <CID> <TID> <reason>" to trigger DELBA for Rx side
6313277213fSVladimir Kondratiev  */
63249cb5dfbSVladimir Kondratiev static ssize_t wil_write_back(struct file *file, const char __user *buf,
6333277213fSVladimir Kondratiev 			      size_t len, loff_t *ppos)
6343277213fSVladimir Kondratiev {
6353277213fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
6363277213fSVladimir Kondratiev 	int rc;
6373277213fSVladimir Kondratiev 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
6382a19f776SColin Ian King 	char cmd[9];
63949cb5dfbSVladimir Kondratiev 	int p1, p2, p3;
6403277213fSVladimir Kondratiev 
6413277213fSVladimir Kondratiev 	if (!kbuf)
6423277213fSVladimir Kondratiev 		return -ENOMEM;
6433277213fSVladimir Kondratiev 
6443277213fSVladimir Kondratiev 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
6453277213fSVladimir Kondratiev 	if (rc != len) {
6463277213fSVladimir Kondratiev 		kfree(kbuf);
6473277213fSVladimir Kondratiev 		return rc >= 0 ? -EIO : rc;
6483277213fSVladimir Kondratiev 	}
6493277213fSVladimir Kondratiev 
6503277213fSVladimir Kondratiev 	kbuf[len] = '\0';
65149cb5dfbSVladimir Kondratiev 	rc = sscanf(kbuf, "%8s %d %d %d", cmd, &p1, &p2, &p3);
6523277213fSVladimir Kondratiev 	kfree(kbuf);
6533277213fSVladimir Kondratiev 
65449cb5dfbSVladimir Kondratiev 	if (rc < 0)
6553277213fSVladimir Kondratiev 		return rc;
65649cb5dfbSVladimir Kondratiev 	if (rc < 2)
6573277213fSVladimir Kondratiev 		return -EINVAL;
6583277213fSVladimir Kondratiev 
65949cb5dfbSVladimir Kondratiev 	if (0 == strcmp(cmd, "add")) {
66049cb5dfbSVladimir Kondratiev 		if (rc < 3) {
66149cb5dfbSVladimir Kondratiev 			wil_err(wil, "BACK: add require at least 2 params\n");
66249cb5dfbSVladimir Kondratiev 			return -EINVAL;
66349cb5dfbSVladimir Kondratiev 		}
66449cb5dfbSVladimir Kondratiev 		if (rc < 4)
66549cb5dfbSVladimir Kondratiev 			p3 = 0;
66649cb5dfbSVladimir Kondratiev 		wmi_addba(wil, p1, p2, p3);
66749cb5dfbSVladimir Kondratiev 	} else if (0 == strcmp(cmd, "del_tx")) {
66849cb5dfbSVladimir Kondratiev 		if (rc < 3)
66949cb5dfbSVladimir Kondratiev 			p2 = WLAN_REASON_QSTA_LEAVE_QBSS;
67049cb5dfbSVladimir Kondratiev 		wmi_delba_tx(wil, p1, p2);
67149cb5dfbSVladimir Kondratiev 	} else if (0 == strcmp(cmd, "del_rx")) {
67249cb5dfbSVladimir Kondratiev 		if (rc < 3) {
67349cb5dfbSVladimir Kondratiev 			wil_err(wil,
67449cb5dfbSVladimir Kondratiev 				"BACK: del_rx require at least 2 params\n");
67549cb5dfbSVladimir Kondratiev 			return -EINVAL;
67649cb5dfbSVladimir Kondratiev 		}
67749cb5dfbSVladimir Kondratiev 		if (rc < 4)
67849cb5dfbSVladimir Kondratiev 			p3 = WLAN_REASON_QSTA_LEAVE_QBSS;
67949cb5dfbSVladimir Kondratiev 		wmi_delba_rx(wil, mk_cidxtid(p1, p2), p3);
68049cb5dfbSVladimir Kondratiev 	} else {
68149cb5dfbSVladimir Kondratiev 		wil_err(wil, "BACK: Unrecognized command \"%s\"\n", cmd);
68249cb5dfbSVladimir Kondratiev 		return -EINVAL;
68349cb5dfbSVladimir Kondratiev 	}
6843277213fSVladimir Kondratiev 
6853277213fSVladimir Kondratiev 	return len;
6863277213fSVladimir Kondratiev }
6873277213fSVladimir Kondratiev 
68849cb5dfbSVladimir Kondratiev static ssize_t wil_read_back(struct file *file, char __user *user_buf,
68949cb5dfbSVladimir Kondratiev 			     size_t count, loff_t *ppos)
69049cb5dfbSVladimir Kondratiev {
69149cb5dfbSVladimir Kondratiev 	static const char text[] = "block ack control, write:\n"
69249cb5dfbSVladimir Kondratiev 	" - \"add <ringid> <agg_size> <timeout>\" to trigger ADDBA\n"
69349cb5dfbSVladimir Kondratiev 	"If missing, <timeout> defaults to 0\n"
69449cb5dfbSVladimir Kondratiev 	" - \"del_tx <ringid> <reason>\" to trigger DELBA for Tx side\n"
69549cb5dfbSVladimir Kondratiev 	" - \"del_rx <CID> <TID> <reason>\" to trigger DELBA for Rx side\n"
69649cb5dfbSVladimir Kondratiev 	"If missing, <reason> set to \"STA_LEAVING\" (36)\n";
69749cb5dfbSVladimir Kondratiev 
69849cb5dfbSVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos, text,
69949cb5dfbSVladimir Kondratiev 				       sizeof(text));
70049cb5dfbSVladimir Kondratiev }
70149cb5dfbSVladimir Kondratiev 
70249cb5dfbSVladimir Kondratiev static const struct file_operations fops_back = {
70349cb5dfbSVladimir Kondratiev 	.read = wil_read_back,
70449cb5dfbSVladimir Kondratiev 	.write = wil_write_back,
7053277213fSVladimir Kondratiev 	.open  = simple_open,
7063277213fSVladimir Kondratiev };
7073277213fSVladimir Kondratiev 
708dc16427bSVladimir Kondratiev /* pmc control, write:
709dc16427bSVladimir Kondratiev  * - "alloc <num descriptors> <descriptor_size>" to allocate PMC
710dc16427bSVladimir Kondratiev  * - "free" to release memory allocated for PMC
711dc16427bSVladimir Kondratiev  */
712dc16427bSVladimir Kondratiev static ssize_t wil_write_pmccfg(struct file *file, const char __user *buf,
713dc16427bSVladimir Kondratiev 				size_t len, loff_t *ppos)
714dc16427bSVladimir Kondratiev {
715dc16427bSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
716dc16427bSVladimir Kondratiev 	int rc;
717dc16427bSVladimir Kondratiev 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
718dc16427bSVladimir Kondratiev 	char cmd[9];
719dc16427bSVladimir Kondratiev 	int num_descs, desc_size;
720dc16427bSVladimir Kondratiev 
721dc16427bSVladimir Kondratiev 	if (!kbuf)
722dc16427bSVladimir Kondratiev 		return -ENOMEM;
723dc16427bSVladimir Kondratiev 
724dc16427bSVladimir Kondratiev 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
725dc16427bSVladimir Kondratiev 	if (rc != len) {
726dc16427bSVladimir Kondratiev 		kfree(kbuf);
727dc16427bSVladimir Kondratiev 		return rc >= 0 ? -EIO : rc;
728dc16427bSVladimir Kondratiev 	}
729dc16427bSVladimir Kondratiev 
730dc16427bSVladimir Kondratiev 	kbuf[len] = '\0';
731dc16427bSVladimir Kondratiev 	rc = sscanf(kbuf, "%8s %d %d", cmd, &num_descs, &desc_size);
732dc16427bSVladimir Kondratiev 	kfree(kbuf);
733dc16427bSVladimir Kondratiev 
734dc16427bSVladimir Kondratiev 	if (rc < 0)
735dc16427bSVladimir Kondratiev 		return rc;
736dc16427bSVladimir Kondratiev 
737dc16427bSVladimir Kondratiev 	if (rc < 1) {
738dc16427bSVladimir Kondratiev 		wil_err(wil, "pmccfg: no params given\n");
739dc16427bSVladimir Kondratiev 		return -EINVAL;
740dc16427bSVladimir Kondratiev 	}
741dc16427bSVladimir Kondratiev 
742dc16427bSVladimir Kondratiev 	if (0 == strcmp(cmd, "alloc")) {
743dc16427bSVladimir Kondratiev 		if (rc != 3) {
744dc16427bSVladimir Kondratiev 			wil_err(wil, "pmccfg: alloc requires 2 params\n");
745dc16427bSVladimir Kondratiev 			return -EINVAL;
746dc16427bSVladimir Kondratiev 		}
747dc16427bSVladimir Kondratiev 		wil_pmc_alloc(wil, num_descs, desc_size);
748dc16427bSVladimir Kondratiev 	} else if (0 == strcmp(cmd, "free")) {
749dc16427bSVladimir Kondratiev 		if (rc != 1) {
750dc16427bSVladimir Kondratiev 			wil_err(wil, "pmccfg: free does not have any params\n");
751dc16427bSVladimir Kondratiev 			return -EINVAL;
752dc16427bSVladimir Kondratiev 		}
753dc16427bSVladimir Kondratiev 		wil_pmc_free(wil, true);
754dc16427bSVladimir Kondratiev 	} else {
755dc16427bSVladimir Kondratiev 		wil_err(wil, "pmccfg: Unrecognized command \"%s\"\n", cmd);
756dc16427bSVladimir Kondratiev 		return -EINVAL;
757dc16427bSVladimir Kondratiev 	}
758dc16427bSVladimir Kondratiev 
759dc16427bSVladimir Kondratiev 	return len;
760dc16427bSVladimir Kondratiev }
761dc16427bSVladimir Kondratiev 
762dc16427bSVladimir Kondratiev static ssize_t wil_read_pmccfg(struct file *file, char __user *user_buf,
763dc16427bSVladimir Kondratiev 			       size_t count, loff_t *ppos)
764dc16427bSVladimir Kondratiev {
765dc16427bSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
766dc16427bSVladimir Kondratiev 	char text[256];
767dc16427bSVladimir Kondratiev 	char help[] = "pmc control, write:\n"
768dc16427bSVladimir Kondratiev 	" - \"alloc <num descriptors> <descriptor_size>\" to allocate pmc\n"
769dc16427bSVladimir Kondratiev 	" - \"free\" to free memory allocated for pmc\n";
770dc16427bSVladimir Kondratiev 
771dc16427bSVladimir Kondratiev 	sprintf(text, "Last command status: %d\n\n%s",
772dc16427bSVladimir Kondratiev 		wil_pmc_last_cmd_status(wil),
773dc16427bSVladimir Kondratiev 		help);
774dc16427bSVladimir Kondratiev 
775dc16427bSVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos, text,
776dc16427bSVladimir Kondratiev 				       strlen(text) + 1);
777dc16427bSVladimir Kondratiev }
778dc16427bSVladimir Kondratiev 
779dc16427bSVladimir Kondratiev static const struct file_operations fops_pmccfg = {
780dc16427bSVladimir Kondratiev 	.read = wil_read_pmccfg,
781dc16427bSVladimir Kondratiev 	.write = wil_write_pmccfg,
782dc16427bSVladimir Kondratiev 	.open  = simple_open,
783dc16427bSVladimir Kondratiev };
784dc16427bSVladimir Kondratiev 
785dc16427bSVladimir Kondratiev static const struct file_operations fops_pmcdata = {
786dc16427bSVladimir Kondratiev 	.open		= simple_open,
787dc16427bSVladimir Kondratiev 	.read		= wil_pmc_read,
788dc16427bSVladimir Kondratiev 	.llseek		= wil_pmc_llseek,
789dc16427bSVladimir Kondratiev };
790dc16427bSVladimir Kondratiev 
7910b39aaf2SVladimir Kondratiev /*---tx_mgmt---*/
7920b39aaf2SVladimir Kondratiev /* Write mgmt frame to this file to send it */
7930b39aaf2SVladimir Kondratiev static ssize_t wil_write_file_txmgmt(struct file *file, const char __user *buf,
7940b39aaf2SVladimir Kondratiev 				     size_t len, loff_t *ppos)
7950b39aaf2SVladimir Kondratiev {
7960b39aaf2SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
7970b39aaf2SVladimir Kondratiev 	struct wiphy *wiphy = wil_to_wiphy(wil);
7980b39aaf2SVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
7990b39aaf2SVladimir Kondratiev 	struct cfg80211_mgmt_tx_params params;
8000b39aaf2SVladimir Kondratiev 	int rc;
8010b39aaf2SVladimir Kondratiev 	void *frame = kmalloc(len, GFP_KERNEL);
8028fe59627SVladimir Kondratiev 
8030b39aaf2SVladimir Kondratiev 	if (!frame)
8040b39aaf2SVladimir Kondratiev 		return -ENOMEM;
8050b39aaf2SVladimir Kondratiev 
8068e09b7d2SLino Sanfilippo 	if (copy_from_user(frame, buf, len)) {
8078e09b7d2SLino Sanfilippo 		kfree(frame);
8080b39aaf2SVladimir Kondratiev 		return -EIO;
8098e09b7d2SLino Sanfilippo 	}
8100b39aaf2SVladimir Kondratiev 
8110b39aaf2SVladimir Kondratiev 	params.buf = frame;
8120b39aaf2SVladimir Kondratiev 	params.len = len;
8130b39aaf2SVladimir Kondratiev 	params.chan = wdev->preset_chandef.chan;
8140b39aaf2SVladimir Kondratiev 
8150b39aaf2SVladimir Kondratiev 	rc = wil_cfg80211_mgmt_tx(wiphy, wdev, &params, NULL);
8160b39aaf2SVladimir Kondratiev 
8170b39aaf2SVladimir Kondratiev 	kfree(frame);
8180b39aaf2SVladimir Kondratiev 	wil_info(wil, "%s() -> %d\n", __func__, rc);
8190b39aaf2SVladimir Kondratiev 
8200b39aaf2SVladimir Kondratiev 	return len;
8210b39aaf2SVladimir Kondratiev }
8220b39aaf2SVladimir Kondratiev 
8230b39aaf2SVladimir Kondratiev static const struct file_operations fops_txmgmt = {
8240b39aaf2SVladimir Kondratiev 	.write = wil_write_file_txmgmt,
8250b39aaf2SVladimir Kondratiev 	.open  = simple_open,
8260b39aaf2SVladimir Kondratiev };
8272be7d22fSVladimir Kondratiev 
828ff974e40SVladimir Kondratiev /* Write WMI command (w/o mbox header) to this file to send it
829ff974e40SVladimir Kondratiev  * WMI starts from wil6210_mbox_hdr_wmi header
830ff974e40SVladimir Kondratiev  */
831ff974e40SVladimir Kondratiev static ssize_t wil_write_file_wmi(struct file *file, const char __user *buf,
832ff974e40SVladimir Kondratiev 				  size_t len, loff_t *ppos)
833ff974e40SVladimir Kondratiev {
834ff974e40SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
835ff974e40SVladimir Kondratiev 	struct wil6210_mbox_hdr_wmi *wmi;
836ff974e40SVladimir Kondratiev 	void *cmd;
837ff974e40SVladimir Kondratiev 	int cmdlen = len - sizeof(struct wil6210_mbox_hdr_wmi);
838ff974e40SVladimir Kondratiev 	u16 cmdid;
839ff974e40SVladimir Kondratiev 	int rc, rc1;
840ff974e40SVladimir Kondratiev 
841ff974e40SVladimir Kondratiev 	if (cmdlen <= 0)
842ff974e40SVladimir Kondratiev 		return -EINVAL;
843ff974e40SVladimir Kondratiev 
844ff974e40SVladimir Kondratiev 	wmi = kmalloc(len, GFP_KERNEL);
845ff974e40SVladimir Kondratiev 	if (!wmi)
846ff974e40SVladimir Kondratiev 		return -ENOMEM;
847ff974e40SVladimir Kondratiev 
848ff974e40SVladimir Kondratiev 	rc = simple_write_to_buffer(wmi, len, ppos, buf, len);
8498e09b7d2SLino Sanfilippo 	if (rc < 0) {
8508e09b7d2SLino Sanfilippo 		kfree(wmi);
851ff974e40SVladimir Kondratiev 		return rc;
8528e09b7d2SLino Sanfilippo 	}
853ff974e40SVladimir Kondratiev 
854ff974e40SVladimir Kondratiev 	cmd = &wmi[1];
855ff974e40SVladimir Kondratiev 	cmdid = le16_to_cpu(wmi->id);
856ff974e40SVladimir Kondratiev 
857ff974e40SVladimir Kondratiev 	rc1 = wmi_send(wil, cmdid, cmd, cmdlen);
858ff974e40SVladimir Kondratiev 	kfree(wmi);
859ff974e40SVladimir Kondratiev 
860ff974e40SVladimir Kondratiev 	wil_info(wil, "%s(0x%04x[%d]) -> %d\n", __func__, cmdid, cmdlen, rc1);
861ff974e40SVladimir Kondratiev 
862ff974e40SVladimir Kondratiev 	return rc;
863ff974e40SVladimir Kondratiev }
864ff974e40SVladimir Kondratiev 
865ff974e40SVladimir Kondratiev static const struct file_operations fops_wmi = {
866ff974e40SVladimir Kondratiev 	.write = wil_write_file_wmi,
867ff974e40SVladimir Kondratiev 	.open  = simple_open,
868ff974e40SVladimir Kondratiev };
869ff974e40SVladimir Kondratiev 
870c236658fSVladimir Kondratiev static void wil_seq_hexdump(struct seq_file *s, void *p, int len,
871c236658fSVladimir Kondratiev 			    const char *prefix)
872c236658fSVladimir Kondratiev {
873c236658fSVladimir Kondratiev 	char printbuf[16 * 3 + 2];
874c236658fSVladimir Kondratiev 	int i = 0;
8758fe59627SVladimir Kondratiev 
876c236658fSVladimir Kondratiev 	while (i < len) {
877c236658fSVladimir Kondratiev 		int l = min(len - i, 16);
8788fe59627SVladimir Kondratiev 
879c236658fSVladimir Kondratiev 		hex_dump_to_buffer(p + i, l, 16, 1, printbuf,
880c236658fSVladimir Kondratiev 				   sizeof(printbuf), false);
881c236658fSVladimir Kondratiev 		seq_printf(s, "%s%s\n", prefix, printbuf);
882c236658fSVladimir Kondratiev 		i += l;
883c236658fSVladimir Kondratiev 	}
884c236658fSVladimir Kondratiev }
885c236658fSVladimir Kondratiev 
886c236658fSVladimir Kondratiev static void wil_seq_print_skb(struct seq_file *s, struct sk_buff *skb)
887c236658fSVladimir Kondratiev {
888c236658fSVladimir Kondratiev 	int i = 0;
889c236658fSVladimir Kondratiev 	int len = skb_headlen(skb);
890c236658fSVladimir Kondratiev 	void *p = skb->data;
891c236658fSVladimir Kondratiev 	int nr_frags = skb_shinfo(skb)->nr_frags;
892c236658fSVladimir Kondratiev 
893c236658fSVladimir Kondratiev 	seq_printf(s, "    len = %d\n", len);
894c236658fSVladimir Kondratiev 	wil_seq_hexdump(s, p, len, "      : ");
895c236658fSVladimir Kondratiev 
896c236658fSVladimir Kondratiev 	if (nr_frags) {
897c236658fSVladimir Kondratiev 		seq_printf(s, "    nr_frags = %d\n", nr_frags);
898c236658fSVladimir Kondratiev 		for (i = 0; i < nr_frags; i++) {
899c236658fSVladimir Kondratiev 			const struct skb_frag_struct *frag =
900c236658fSVladimir Kondratiev 					&skb_shinfo(skb)->frags[i];
901c236658fSVladimir Kondratiev 
902c236658fSVladimir Kondratiev 			len = skb_frag_size(frag);
903c236658fSVladimir Kondratiev 			p = skb_frag_address_safe(frag);
904c236658fSVladimir Kondratiev 			seq_printf(s, "    [%2d] : len = %d\n", i, len);
905c236658fSVladimir Kondratiev 			wil_seq_hexdump(s, p, len, "      : ");
906c236658fSVladimir Kondratiev 		}
907c236658fSVladimir Kondratiev 	}
908c236658fSVladimir Kondratiev }
909c236658fSVladimir Kondratiev 
9103a85543eSVladimir Kondratiev /*---------Tx/Rx descriptor------------*/
9112be7d22fSVladimir Kondratiev static int wil_txdesc_debugfs_show(struct seq_file *s, void *data)
9122be7d22fSVladimir Kondratiev {
9132be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
9143a85543eSVladimir Kondratiev 	struct vring *vring;
915af31cb5aSVladimir Kondratiev 	bool tx = (dbg_vring_index < WIL6210_MAX_TX_RINGS);
9168fe59627SVladimir Kondratiev 
9178fe59627SVladimir Kondratiev 	vring = tx ? &wil->vring_tx[dbg_vring_index] : &wil->vring_rx;
9182be7d22fSVladimir Kondratiev 
9192be7d22fSVladimir Kondratiev 	if (!vring->va) {
920af31cb5aSVladimir Kondratiev 		if (tx)
9213a85543eSVladimir Kondratiev 			seq_printf(s, "No Tx[%2d] VRING\n", dbg_vring_index);
9223a85543eSVladimir Kondratiev 		else
9233a85543eSVladimir Kondratiev 			seq_puts(s, "No Rx VRING\n");
9242be7d22fSVladimir Kondratiev 		return 0;
9252be7d22fSVladimir Kondratiev 	}
9262be7d22fSVladimir Kondratiev 
9272be7d22fSVladimir Kondratiev 	if (dbg_txdesc_index < vring->size) {
9283a85543eSVladimir Kondratiev 		/* use struct vring_tx_desc for Rx as well,
9293a85543eSVladimir Kondratiev 		 * only field used, .dma.length, is the same
9303a85543eSVladimir Kondratiev 		 */
9312be7d22fSVladimir Kondratiev 		volatile struct vring_tx_desc *d =
9328fe59627SVladimir Kondratiev 				&vring->va[dbg_txdesc_index].tx;
9332be7d22fSVladimir Kondratiev 		volatile u32 *u = (volatile u32 *)d;
934f88f113aSVladimir Kondratiev 		struct sk_buff *skb = vring->ctx[dbg_txdesc_index].skb;
9352be7d22fSVladimir Kondratiev 
936af31cb5aSVladimir Kondratiev 		if (tx)
9373a85543eSVladimir Kondratiev 			seq_printf(s, "Tx[%2d][%3d] = {\n", dbg_vring_index,
9383a85543eSVladimir Kondratiev 				   dbg_txdesc_index);
9393a85543eSVladimir Kondratiev 		else
9403a85543eSVladimir Kondratiev 			seq_printf(s, "Rx[%3d] = {\n", dbg_txdesc_index);
9412be7d22fSVladimir Kondratiev 		seq_printf(s, "  MAC = 0x%08x 0x%08x 0x%08x 0x%08x\n",
9422be7d22fSVladimir Kondratiev 			   u[0], u[1], u[2], u[3]);
9432be7d22fSVladimir Kondratiev 		seq_printf(s, "  DMA = 0x%08x 0x%08x 0x%08x 0x%08x\n",
9442be7d22fSVladimir Kondratiev 			   u[4], u[5], u[6], u[7]);
94539c52ee8SVladimir Kondratiev 		seq_printf(s, "  SKB = 0x%p\n", skb);
9462be7d22fSVladimir Kondratiev 
9472be7d22fSVladimir Kondratiev 		if (skb) {
948c236658fSVladimir Kondratiev 			skb_get(skb);
949c236658fSVladimir Kondratiev 			wil_seq_print_skb(s, skb);
950c236658fSVladimir Kondratiev 			kfree_skb(skb);
9512be7d22fSVladimir Kondratiev 		}
9528fe59627SVladimir Kondratiev 		seq_puts(s, "}\n");
9532be7d22fSVladimir Kondratiev 	} else {
954af31cb5aSVladimir Kondratiev 		if (tx)
9553a85543eSVladimir Kondratiev 			seq_printf(s, "[%2d] TxDesc index (%d) >= size (%d)\n",
9563a85543eSVladimir Kondratiev 				   dbg_vring_index, dbg_txdesc_index,
9573a85543eSVladimir Kondratiev 				   vring->size);
9583a85543eSVladimir Kondratiev 		else
9593a85543eSVladimir Kondratiev 			seq_printf(s, "RxDesc index (%d) >= size (%d)\n",
9602be7d22fSVladimir Kondratiev 				   dbg_txdesc_index, vring->size);
9612be7d22fSVladimir Kondratiev 	}
9622be7d22fSVladimir Kondratiev 
9632be7d22fSVladimir Kondratiev 	return 0;
9642be7d22fSVladimir Kondratiev }
9652be7d22fSVladimir Kondratiev 
9662be7d22fSVladimir Kondratiev static int wil_txdesc_seq_open(struct inode *inode, struct file *file)
9672be7d22fSVladimir Kondratiev {
9682be7d22fSVladimir Kondratiev 	return single_open(file, wil_txdesc_debugfs_show, inode->i_private);
9692be7d22fSVladimir Kondratiev }
9702be7d22fSVladimir Kondratiev 
9712be7d22fSVladimir Kondratiev static const struct file_operations fops_txdesc = {
9722be7d22fSVladimir Kondratiev 	.open		= wil_txdesc_seq_open,
9732be7d22fSVladimir Kondratiev 	.release	= single_release,
9742be7d22fSVladimir Kondratiev 	.read		= seq_read,
9752be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
9762be7d22fSVladimir Kondratiev };
9772be7d22fSVladimir Kondratiev 
9782be7d22fSVladimir Kondratiev /*---------beamforming------------*/
97936345ac3SVladimir Kondratiev static char *wil_bfstatus_str(u32 status)
98036345ac3SVladimir Kondratiev {
98136345ac3SVladimir Kondratiev 	switch (status) {
98236345ac3SVladimir Kondratiev 	case 0:
98336345ac3SVladimir Kondratiev 		return "Failed";
98436345ac3SVladimir Kondratiev 	case 1:
98536345ac3SVladimir Kondratiev 		return "OK";
98636345ac3SVladimir Kondratiev 	case 2:
98736345ac3SVladimir Kondratiev 		return "Retrying";
98836345ac3SVladimir Kondratiev 	default:
98936345ac3SVladimir Kondratiev 		return "??";
99036345ac3SVladimir Kondratiev 	}
99136345ac3SVladimir Kondratiev }
99236345ac3SVladimir Kondratiev 
99336345ac3SVladimir Kondratiev static bool is_all_zeros(void * const x_, size_t sz)
99436345ac3SVladimir Kondratiev {
99536345ac3SVladimir Kondratiev 	/* if reply is all-0, ignore this CID */
99636345ac3SVladimir Kondratiev 	u32 *x = x_;
99736345ac3SVladimir Kondratiev 	int n;
99836345ac3SVladimir Kondratiev 
99936345ac3SVladimir Kondratiev 	for (n = 0; n < sz / sizeof(*x); n++)
100036345ac3SVladimir Kondratiev 		if (x[n])
100136345ac3SVladimir Kondratiev 			return false;
100236345ac3SVladimir Kondratiev 
100336345ac3SVladimir Kondratiev 	return true;
100436345ac3SVladimir Kondratiev }
100536345ac3SVladimir Kondratiev 
10062be7d22fSVladimir Kondratiev static int wil_bf_debugfs_show(struct seq_file *s, void *data)
10072be7d22fSVladimir Kondratiev {
100836345ac3SVladimir Kondratiev 	int rc;
100936345ac3SVladimir Kondratiev 	int i;
10102be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
101136345ac3SVladimir Kondratiev 	struct wmi_notify_req_cmd cmd = {
101236345ac3SVladimir Kondratiev 		.interval_usec = 0,
101336345ac3SVladimir Kondratiev 	};
101436345ac3SVladimir Kondratiev 	struct {
101536345ac3SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
101636345ac3SVladimir Kondratiev 		struct wmi_notify_req_done_event evt;
101736345ac3SVladimir Kondratiev 	} __packed reply;
101836345ac3SVladimir Kondratiev 
101936345ac3SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
102036345ac3SVladimir Kondratiev 		u32 status;
102136345ac3SVladimir Kondratiev 
102236345ac3SVladimir Kondratiev 		cmd.cid = i;
102336345ac3SVladimir Kondratiev 		rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
102436345ac3SVladimir Kondratiev 			      WMI_NOTIFY_REQ_DONE_EVENTID, &reply,
102536345ac3SVladimir Kondratiev 			      sizeof(reply), 20);
102636345ac3SVladimir Kondratiev 		/* if reply is all-0, ignore this CID */
102736345ac3SVladimir Kondratiev 		if (rc || is_all_zeros(&reply.evt, sizeof(reply.evt)))
102836345ac3SVladimir Kondratiev 			continue;
102936345ac3SVladimir Kondratiev 
103036345ac3SVladimir Kondratiev 		status = le32_to_cpu(reply.evt.status);
103136345ac3SVladimir Kondratiev 		seq_printf(s, "CID %d {\n"
103236345ac3SVladimir Kondratiev 			   "  TSF = 0x%016llx\n"
103336345ac3SVladimir Kondratiev 			   "  TxMCS = %2d TxTpt = %4d\n"
103436345ac3SVladimir Kondratiev 			   "  SQI = %4d\n"
103536345ac3SVladimir Kondratiev 			   "  Status = 0x%08x %s\n"
103636345ac3SVladimir Kondratiev 			   "  Sectors(rx:tx) my %2d:%2d peer %2d:%2d\n"
103736345ac3SVladimir Kondratiev 			   "  Goodput(rx:tx) %4d:%4d\n"
103836345ac3SVladimir Kondratiev 			   "}\n",
103936345ac3SVladimir Kondratiev 			   i,
104036345ac3SVladimir Kondratiev 			   le64_to_cpu(reply.evt.tsf),
104136345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.bf_mcs),
104236345ac3SVladimir Kondratiev 			   le32_to_cpu(reply.evt.tx_tpt),
104336345ac3SVladimir Kondratiev 			   reply.evt.sqi,
104436345ac3SVladimir Kondratiev 			   status, wil_bfstatus_str(status),
104536345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.my_rx_sector),
104636345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.my_tx_sector),
104736345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.other_rx_sector),
104836345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.other_tx_sector),
104936345ac3SVladimir Kondratiev 			   le32_to_cpu(reply.evt.rx_goodput),
105036345ac3SVladimir Kondratiev 			   le32_to_cpu(reply.evt.tx_goodput));
105136345ac3SVladimir Kondratiev 	}
10522be7d22fSVladimir Kondratiev 	return 0;
10532be7d22fSVladimir Kondratiev }
10542be7d22fSVladimir Kondratiev 
10552be7d22fSVladimir Kondratiev static int wil_bf_seq_open(struct inode *inode, struct file *file)
10562be7d22fSVladimir Kondratiev {
10572be7d22fSVladimir Kondratiev 	return single_open(file, wil_bf_debugfs_show, inode->i_private);
10582be7d22fSVladimir Kondratiev }
10592be7d22fSVladimir Kondratiev 
10602be7d22fSVladimir Kondratiev static const struct file_operations fops_bf = {
10612be7d22fSVladimir Kondratiev 	.open		= wil_bf_seq_open,
10622be7d22fSVladimir Kondratiev 	.release	= single_release,
10632be7d22fSVladimir Kondratiev 	.read		= seq_read,
10642be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
10652be7d22fSVladimir Kondratiev };
10668fe59627SVladimir Kondratiev 
10672be7d22fSVladimir Kondratiev /*---------SSID------------*/
10682be7d22fSVladimir Kondratiev static ssize_t wil_read_file_ssid(struct file *file, char __user *user_buf,
10692be7d22fSVladimir Kondratiev 				  size_t count, loff_t *ppos)
10702be7d22fSVladimir Kondratiev {
10712be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
10722be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
10732be7d22fSVladimir Kondratiev 
10742be7d22fSVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos,
10752be7d22fSVladimir Kondratiev 				       wdev->ssid, wdev->ssid_len);
10762be7d22fSVladimir Kondratiev }
10772be7d22fSVladimir Kondratiev 
10782be7d22fSVladimir Kondratiev static ssize_t wil_write_file_ssid(struct file *file, const char __user *buf,
10792be7d22fSVladimir Kondratiev 				   size_t count, loff_t *ppos)
10802be7d22fSVladimir Kondratiev {
10812be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
10822be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
10832be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
10842be7d22fSVladimir Kondratiev 
10852be7d22fSVladimir Kondratiev 	if (*ppos != 0) {
10862be7d22fSVladimir Kondratiev 		wil_err(wil, "Unable to set SSID substring from [%d]\n",
10872be7d22fSVladimir Kondratiev 			(int)*ppos);
10882be7d22fSVladimir Kondratiev 		return -EINVAL;
10892be7d22fSVladimir Kondratiev 	}
10902be7d22fSVladimir Kondratiev 
10912be7d22fSVladimir Kondratiev 	if (count > sizeof(wdev->ssid)) {
10922be7d22fSVladimir Kondratiev 		wil_err(wil, "SSID too long, len = %d\n", (int)count);
10932be7d22fSVladimir Kondratiev 		return -EINVAL;
10942be7d22fSVladimir Kondratiev 	}
10952be7d22fSVladimir Kondratiev 	if (netif_running(ndev)) {
10962be7d22fSVladimir Kondratiev 		wil_err(wil, "Unable to change SSID on running interface\n");
10972be7d22fSVladimir Kondratiev 		return -EINVAL;
10982be7d22fSVladimir Kondratiev 	}
10992be7d22fSVladimir Kondratiev 
11002be7d22fSVladimir Kondratiev 	wdev->ssid_len = count;
11012be7d22fSVladimir Kondratiev 	return simple_write_to_buffer(wdev->ssid, wdev->ssid_len, ppos,
11022be7d22fSVladimir Kondratiev 				      buf, count);
11032be7d22fSVladimir Kondratiev }
11042be7d22fSVladimir Kondratiev 
11052be7d22fSVladimir Kondratiev static const struct file_operations fops_ssid = {
11062be7d22fSVladimir Kondratiev 	.read = wil_read_file_ssid,
11072be7d22fSVladimir Kondratiev 	.write = wil_write_file_ssid,
110893ecbd64SWei Yongjun 	.open  = simple_open,
11092be7d22fSVladimir Kondratiev };
11102be7d22fSVladimir Kondratiev 
11111a2780e0SVladimir Kondratiev /*---------temp------------*/
11121a2780e0SVladimir Kondratiev static void print_temp(struct seq_file *s, const char *prefix, u32 t)
11131a2780e0SVladimir Kondratiev {
11141a2780e0SVladimir Kondratiev 	switch (t) {
11151a2780e0SVladimir Kondratiev 	case 0:
11161a2780e0SVladimir Kondratiev 	case ~(u32)0:
11171a2780e0SVladimir Kondratiev 		seq_printf(s, "%s N/A\n", prefix);
11181a2780e0SVladimir Kondratiev 	break;
11191a2780e0SVladimir Kondratiev 	default:
11201a2780e0SVladimir Kondratiev 		seq_printf(s, "%s %d.%03d\n", prefix, t / 1000, t % 1000);
11211a2780e0SVladimir Kondratiev 		break;
11221a2780e0SVladimir Kondratiev 	}
11231a2780e0SVladimir Kondratiev }
11241a2780e0SVladimir Kondratiev 
11251a2780e0SVladimir Kondratiev static int wil_temp_debugfs_show(struct seq_file *s, void *data)
11261a2780e0SVladimir Kondratiev {
11271a2780e0SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
11281a2780e0SVladimir Kondratiev 	u32 t_m, t_r;
11291a2780e0SVladimir Kondratiev 	int rc = wmi_get_temperature(wil, &t_m, &t_r);
11308fe59627SVladimir Kondratiev 
11311a2780e0SVladimir Kondratiev 	if (rc) {
11328fe59627SVladimir Kondratiev 		seq_puts(s, "Failed\n");
11331a2780e0SVladimir Kondratiev 		return 0;
11341a2780e0SVladimir Kondratiev 	}
11351a2780e0SVladimir Kondratiev 
1136d45cff9fSVladimir Kondratiev 	print_temp(s, "T_mac   =", t_m);
1137d45cff9fSVladimir Kondratiev 	print_temp(s, "T_radio =", t_r);
11381a2780e0SVladimir Kondratiev 
11391a2780e0SVladimir Kondratiev 	return 0;
11401a2780e0SVladimir Kondratiev }
11411a2780e0SVladimir Kondratiev 
11421a2780e0SVladimir Kondratiev static int wil_temp_seq_open(struct inode *inode, struct file *file)
11431a2780e0SVladimir Kondratiev {
11441a2780e0SVladimir Kondratiev 	return single_open(file, wil_temp_debugfs_show, inode->i_private);
11451a2780e0SVladimir Kondratiev }
11461a2780e0SVladimir Kondratiev 
11471a2780e0SVladimir Kondratiev static const struct file_operations fops_temp = {
11481a2780e0SVladimir Kondratiev 	.open		= wil_temp_seq_open,
11491a2780e0SVladimir Kondratiev 	.release	= single_release,
11501a2780e0SVladimir Kondratiev 	.read		= seq_read,
11511a2780e0SVladimir Kondratiev 	.llseek		= seq_lseek,
11521a2780e0SVladimir Kondratiev };
11531a2780e0SVladimir Kondratiev 
11549eb82d43SVladimir Kondratiev /*---------freq------------*/
11559eb82d43SVladimir Kondratiev static int wil_freq_debugfs_show(struct seq_file *s, void *data)
11569eb82d43SVladimir Kondratiev {
11579eb82d43SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
11589eb82d43SVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
11599eb82d43SVladimir Kondratiev 	u16 freq = wdev->chandef.chan ? wdev->chandef.chan->center_freq : 0;
11609eb82d43SVladimir Kondratiev 
11619eb82d43SVladimir Kondratiev 	seq_printf(s, "Freq = %d\n", freq);
11629eb82d43SVladimir Kondratiev 
11639eb82d43SVladimir Kondratiev 	return 0;
11649eb82d43SVladimir Kondratiev }
11659eb82d43SVladimir Kondratiev 
11669eb82d43SVladimir Kondratiev static int wil_freq_seq_open(struct inode *inode, struct file *file)
11679eb82d43SVladimir Kondratiev {
11689eb82d43SVladimir Kondratiev 	return single_open(file, wil_freq_debugfs_show, inode->i_private);
11699eb82d43SVladimir Kondratiev }
11709eb82d43SVladimir Kondratiev 
11719eb82d43SVladimir Kondratiev static const struct file_operations fops_freq = {
11729eb82d43SVladimir Kondratiev 	.open		= wil_freq_seq_open,
11739eb82d43SVladimir Kondratiev 	.release	= single_release,
11749eb82d43SVladimir Kondratiev 	.read		= seq_read,
11759eb82d43SVladimir Kondratiev 	.llseek		= seq_lseek,
11769eb82d43SVladimir Kondratiev };
11779eb82d43SVladimir Kondratiev 
11789eb82d43SVladimir Kondratiev /*---------link------------*/
11799eb82d43SVladimir Kondratiev static int wil_link_debugfs_show(struct seq_file *s, void *data)
11809eb82d43SVladimir Kondratiev {
11819eb82d43SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
11829eb82d43SVladimir Kondratiev 	struct station_info sinfo;
11839eb82d43SVladimir Kondratiev 	int i, rc;
11849eb82d43SVladimir Kondratiev 
11859eb82d43SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
11869eb82d43SVladimir Kondratiev 		struct wil_sta_info *p = &wil->sta[i];
11879eb82d43SVladimir Kondratiev 		char *status = "unknown";
11888fe59627SVladimir Kondratiev 
11899eb82d43SVladimir Kondratiev 		switch (p->status) {
11909eb82d43SVladimir Kondratiev 		case wil_sta_unused:
11919eb82d43SVladimir Kondratiev 			status = "unused   ";
11929eb82d43SVladimir Kondratiev 			break;
11939eb82d43SVladimir Kondratiev 		case wil_sta_conn_pending:
11949eb82d43SVladimir Kondratiev 			status = "pending  ";
11959eb82d43SVladimir Kondratiev 			break;
11969eb82d43SVladimir Kondratiev 		case wil_sta_connected:
11979eb82d43SVladimir Kondratiev 			status = "connected";
11989eb82d43SVladimir Kondratiev 			break;
11999eb82d43SVladimir Kondratiev 		}
1200230d8442SVladimir Kondratiev 		seq_printf(s, "[%d] %pM %s\n", i, p->addr, status);
12019eb82d43SVladimir Kondratiev 
12029eb82d43SVladimir Kondratiev 		if (p->status == wil_sta_connected) {
12039eb82d43SVladimir Kondratiev 			rc = wil_cid_fill_sinfo(wil, i, &sinfo);
12049eb82d43SVladimir Kondratiev 			if (rc)
12059eb82d43SVladimir Kondratiev 				return rc;
12069eb82d43SVladimir Kondratiev 
12079eb82d43SVladimir Kondratiev 			seq_printf(s, "  Tx_mcs = %d\n", sinfo.txrate.mcs);
12089eb82d43SVladimir Kondratiev 			seq_printf(s, "  Rx_mcs = %d\n", sinfo.rxrate.mcs);
12099eb82d43SVladimir Kondratiev 			seq_printf(s, "  SQ     = %d\n", sinfo.signal);
12109eb82d43SVladimir Kondratiev 		}
12119eb82d43SVladimir Kondratiev 	}
12129eb82d43SVladimir Kondratiev 
12139eb82d43SVladimir Kondratiev 	return 0;
12149eb82d43SVladimir Kondratiev }
12159eb82d43SVladimir Kondratiev 
12169eb82d43SVladimir Kondratiev static int wil_link_seq_open(struct inode *inode, struct file *file)
12179eb82d43SVladimir Kondratiev {
12189eb82d43SVladimir Kondratiev 	return single_open(file, wil_link_debugfs_show, inode->i_private);
12199eb82d43SVladimir Kondratiev }
12209eb82d43SVladimir Kondratiev 
12219eb82d43SVladimir Kondratiev static const struct file_operations fops_link = {
12229eb82d43SVladimir Kondratiev 	.open		= wil_link_seq_open,
12239eb82d43SVladimir Kondratiev 	.release	= single_release,
12249eb82d43SVladimir Kondratiev 	.read		= seq_read,
12259eb82d43SVladimir Kondratiev 	.llseek		= seq_lseek,
12269eb82d43SVladimir Kondratiev };
12279eb82d43SVladimir Kondratiev 
122884bb29b7SVladimir Kondratiev /*---------info------------*/
122984bb29b7SVladimir Kondratiev static int wil_info_debugfs_show(struct seq_file *s, void *data)
123084bb29b7SVladimir Kondratiev {
1231be299858SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
1232be299858SVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
123384bb29b7SVladimir Kondratiev 	int is_ac = power_supply_is_system_supplied();
1234be299858SVladimir Kondratiev 	int rx = atomic_xchg(&wil->isr_count_rx, 0);
1235be299858SVladimir Kondratiev 	int tx = atomic_xchg(&wil->isr_count_tx, 0);
1236be299858SVladimir Kondratiev 	static ulong rxf_old, txf_old;
1237be299858SVladimir Kondratiev 	ulong rxf = ndev->stats.rx_packets;
1238be299858SVladimir Kondratiev 	ulong txf = ndev->stats.tx_packets;
123955f8f680SVladimir Kondratiev 	unsigned int i;
124084bb29b7SVladimir Kondratiev 
124184bb29b7SVladimir Kondratiev 	/* >0 : AC; 0 : battery; <0 : error */
124284bb29b7SVladimir Kondratiev 	seq_printf(s, "AC powered : %d\n", is_ac);
1243be299858SVladimir Kondratiev 	seq_printf(s, "Rx irqs:packets : %8d : %8ld\n", rx, rxf - rxf_old);
1244be299858SVladimir Kondratiev 	seq_printf(s, "Tx irqs:packets : %8d : %8ld\n", tx, txf - txf_old);
1245be299858SVladimir Kondratiev 	rxf_old = rxf;
1246be299858SVladimir Kondratiev 	txf_old = txf;
124784bb29b7SVladimir Kondratiev 
124855f8f680SVladimir Kondratiev #define CHECK_QSTATE(x) (state & BIT(__QUEUE_STATE_ ## x)) ? \
124955f8f680SVladimir Kondratiev 	" " __stringify(x) : ""
125055f8f680SVladimir Kondratiev 
125155f8f680SVladimir Kondratiev 	for (i = 0; i < ndev->num_tx_queues; i++) {
125255f8f680SVladimir Kondratiev 		struct netdev_queue *txq = netdev_get_tx_queue(ndev, i);
125355f8f680SVladimir Kondratiev 		unsigned long state = txq->state;
125455f8f680SVladimir Kondratiev 
125555f8f680SVladimir Kondratiev 		seq_printf(s, "Tx queue[%i] state : 0x%lx%s%s%s\n", i, state,
125655f8f680SVladimir Kondratiev 			   CHECK_QSTATE(DRV_XOFF),
125755f8f680SVladimir Kondratiev 			   CHECK_QSTATE(STACK_XOFF),
125855f8f680SVladimir Kondratiev 			   CHECK_QSTATE(FROZEN)
125955f8f680SVladimir Kondratiev 			  );
126055f8f680SVladimir Kondratiev 	}
126155f8f680SVladimir Kondratiev #undef CHECK_QSTATE
126284bb29b7SVladimir Kondratiev 	return 0;
126384bb29b7SVladimir Kondratiev }
126484bb29b7SVladimir Kondratiev 
126584bb29b7SVladimir Kondratiev static int wil_info_seq_open(struct inode *inode, struct file *file)
126684bb29b7SVladimir Kondratiev {
126784bb29b7SVladimir Kondratiev 	return single_open(file, wil_info_debugfs_show, inode->i_private);
126884bb29b7SVladimir Kondratiev }
126984bb29b7SVladimir Kondratiev 
127084bb29b7SVladimir Kondratiev static const struct file_operations fops_info = {
127184bb29b7SVladimir Kondratiev 	.open		= wil_info_seq_open,
127284bb29b7SVladimir Kondratiev 	.release	= single_release,
127384bb29b7SVladimir Kondratiev 	.read		= seq_read,
127484bb29b7SVladimir Kondratiev 	.llseek		= seq_lseek,
127584bb29b7SVladimir Kondratiev };
127684bb29b7SVladimir Kondratiev 
1277c33407a8SVladimir Kondratiev /*---------recovery------------*/
1278c33407a8SVladimir Kondratiev /* mode = [manual|auto]
1279c33407a8SVladimir Kondratiev  * state = [idle|pending|running]
1280c33407a8SVladimir Kondratiev  */
1281c33407a8SVladimir Kondratiev static ssize_t wil_read_file_recovery(struct file *file, char __user *user_buf,
1282c33407a8SVladimir Kondratiev 				      size_t count, loff_t *ppos)
1283c33407a8SVladimir Kondratiev {
1284c33407a8SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
1285c33407a8SVladimir Kondratiev 	char buf[80];
1286c33407a8SVladimir Kondratiev 	int n;
1287c33407a8SVladimir Kondratiev 	static const char * const sstate[] = {"idle", "pending", "running"};
1288c33407a8SVladimir Kondratiev 
1289c33407a8SVladimir Kondratiev 	n = snprintf(buf, sizeof(buf), "mode = %s\nstate = %s\n",
1290c33407a8SVladimir Kondratiev 		     no_fw_recovery ? "manual" : "auto",
1291c33407a8SVladimir Kondratiev 		     sstate[wil->recovery_state]);
1292c33407a8SVladimir Kondratiev 
1293c33407a8SVladimir Kondratiev 	n = min_t(int, n, sizeof(buf));
1294c33407a8SVladimir Kondratiev 
1295c33407a8SVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos,
1296c33407a8SVladimir Kondratiev 				       buf, n);
1297c33407a8SVladimir Kondratiev }
1298c33407a8SVladimir Kondratiev 
1299c33407a8SVladimir Kondratiev static ssize_t wil_write_file_recovery(struct file *file,
1300c33407a8SVladimir Kondratiev 				       const char __user *buf_,
1301c33407a8SVladimir Kondratiev 				       size_t count, loff_t *ppos)
1302c33407a8SVladimir Kondratiev {
1303c33407a8SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
1304c33407a8SVladimir Kondratiev 	static const char run_command[] = "run";
1305c33407a8SVladimir Kondratiev 	char buf[sizeof(run_command) + 1]; /* to detect "runx" */
1306c33407a8SVladimir Kondratiev 	ssize_t rc;
1307c33407a8SVladimir Kondratiev 
1308c33407a8SVladimir Kondratiev 	if (wil->recovery_state != fw_recovery_pending) {
1309c33407a8SVladimir Kondratiev 		wil_err(wil, "No recovery pending\n");
1310c33407a8SVladimir Kondratiev 		return -EINVAL;
1311c33407a8SVladimir Kondratiev 	}
1312c33407a8SVladimir Kondratiev 
1313c33407a8SVladimir Kondratiev 	if (*ppos != 0) {
1314c33407a8SVladimir Kondratiev 		wil_err(wil, "Offset [%d]\n", (int)*ppos);
1315c33407a8SVladimir Kondratiev 		return -EINVAL;
1316c33407a8SVladimir Kondratiev 	}
1317c33407a8SVladimir Kondratiev 
1318c33407a8SVladimir Kondratiev 	if (count > sizeof(buf)) {
1319c33407a8SVladimir Kondratiev 		wil_err(wil, "Input too long, len = %d\n", (int)count);
1320c33407a8SVladimir Kondratiev 		return -EINVAL;
1321c33407a8SVladimir Kondratiev 	}
1322c33407a8SVladimir Kondratiev 
1323c33407a8SVladimir Kondratiev 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, buf_, count);
1324c33407a8SVladimir Kondratiev 	if (rc < 0)
1325c33407a8SVladimir Kondratiev 		return rc;
1326c33407a8SVladimir Kondratiev 
1327c33407a8SVladimir Kondratiev 	buf[rc] = '\0';
1328c33407a8SVladimir Kondratiev 	if (0 == strcmp(buf, run_command))
1329c33407a8SVladimir Kondratiev 		wil_set_recovery_state(wil, fw_recovery_running);
1330c33407a8SVladimir Kondratiev 	else
1331c33407a8SVladimir Kondratiev 		wil_err(wil, "Bad recovery command \"%s\"\n", buf);
1332c33407a8SVladimir Kondratiev 
1333c33407a8SVladimir Kondratiev 	return rc;
1334c33407a8SVladimir Kondratiev }
1335c33407a8SVladimir Kondratiev 
1336c33407a8SVladimir Kondratiev static const struct file_operations fops_recovery = {
1337c33407a8SVladimir Kondratiev 	.read = wil_read_file_recovery,
1338c33407a8SVladimir Kondratiev 	.write = wil_write_file_recovery,
1339c33407a8SVladimir Kondratiev 	.open  = simple_open,
1340c33407a8SVladimir Kondratiev };
1341c33407a8SVladimir Kondratiev 
13423df2cd36SVladimir Kondratiev /*---------Station matrix------------*/
1343b4490f42SVladimir Kondratiev static void wil_print_rxtid(struct seq_file *s, struct wil_tid_ampdu_rx *r)
1344b4490f42SVladimir Kondratiev {
1345b4490f42SVladimir Kondratiev 	int i;
1346b4490f42SVladimir Kondratiev 	u16 index = ((r->head_seq_num - r->ssn) & 0xfff) % r->buf_size;
13478fe59627SVladimir Kondratiev 
134856637e7bSVladimir Kondratiev 	seq_printf(s, "([%2d] %3d TU) 0x%03x [", r->buf_size, r->timeout,
134956637e7bSVladimir Kondratiev 		   r->head_seq_num);
1350b4490f42SVladimir Kondratiev 	for (i = 0; i < r->buf_size; i++) {
1351b4490f42SVladimir Kondratiev 		if (i == index)
1352b4490f42SVladimir Kondratiev 			seq_printf(s, "%c", r->reorder_buf[i] ? 'O' : '|');
1353b4490f42SVladimir Kondratiev 		else
1354b4490f42SVladimir Kondratiev 			seq_printf(s, "%c", r->reorder_buf[i] ? '*' : '_');
1355b4490f42SVladimir Kondratiev 	}
13566093e66bSVladimir Kondratiev 	seq_printf(s, "] drop %llu last 0x%03x\n", r->drop, r->ssn_last_drop);
1357b4490f42SVladimir Kondratiev }
13583df2cd36SVladimir Kondratiev 
13593df2cd36SVladimir Kondratiev static int wil_sta_debugfs_show(struct seq_file *s, void *data)
1360bd33273bSVladimir Kondratiev __acquires(&p->tid_rx_lock) __releases(&p->tid_rx_lock)
13613df2cd36SVladimir Kondratiev {
13623df2cd36SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
1363c4a110d8SVladimir Kondratiev 	int i, tid, mcs;
13643df2cd36SVladimir Kondratiev 
13653df2cd36SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
13663df2cd36SVladimir Kondratiev 		struct wil_sta_info *p = &wil->sta[i];
13673df2cd36SVladimir Kondratiev 		char *status = "unknown";
13688fe59627SVladimir Kondratiev 
13693df2cd36SVladimir Kondratiev 		switch (p->status) {
13703df2cd36SVladimir Kondratiev 		case wil_sta_unused:
13713df2cd36SVladimir Kondratiev 			status = "unused   ";
13723df2cd36SVladimir Kondratiev 			break;
13733df2cd36SVladimir Kondratiev 		case wil_sta_conn_pending:
13743df2cd36SVladimir Kondratiev 			status = "pending  ";
13753df2cd36SVladimir Kondratiev 			break;
13763df2cd36SVladimir Kondratiev 		case wil_sta_connected:
13773df2cd36SVladimir Kondratiev 			status = "connected";
13783df2cd36SVladimir Kondratiev 			break;
13793df2cd36SVladimir Kondratiev 		}
1380230d8442SVladimir Kondratiev 		seq_printf(s, "[%d] %pM %s\n", i, p->addr, status);
1381b4490f42SVladimir Kondratiev 
1382b4490f42SVladimir Kondratiev 		if (p->status == wil_sta_connected) {
1383bd33273bSVladimir Kondratiev 			spin_lock_bh(&p->tid_rx_lock);
1384b4490f42SVladimir Kondratiev 			for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1385b4490f42SVladimir Kondratiev 				struct wil_tid_ampdu_rx *r = p->tid_rx[tid];
13868fe59627SVladimir Kondratiev 
1387b4490f42SVladimir Kondratiev 				if (r) {
1388b4490f42SVladimir Kondratiev 					seq_printf(s, "[%2d] ", tid);
1389b4490f42SVladimir Kondratiev 					wil_print_rxtid(s, r);
1390b4490f42SVladimir Kondratiev 				}
1391b4490f42SVladimir Kondratiev 			}
1392bd33273bSVladimir Kondratiev 			spin_unlock_bh(&p->tid_rx_lock);
1393c4a110d8SVladimir Kondratiev 			seq_puts(s, "Rx/MCS:");
1394c4a110d8SVladimir Kondratiev 			for (mcs = 0; mcs < ARRAY_SIZE(p->stats.rx_per_mcs);
1395c4a110d8SVladimir Kondratiev 			     mcs++)
1396c4a110d8SVladimir Kondratiev 				seq_printf(s, " %lld",
1397c4a110d8SVladimir Kondratiev 					   p->stats.rx_per_mcs[mcs]);
1398c4a110d8SVladimir Kondratiev 			seq_puts(s, "\n");
1399b4490f42SVladimir Kondratiev 		}
14003df2cd36SVladimir Kondratiev 	}
14013df2cd36SVladimir Kondratiev 
14023df2cd36SVladimir Kondratiev 	return 0;
14033df2cd36SVladimir Kondratiev }
14043df2cd36SVladimir Kondratiev 
14053df2cd36SVladimir Kondratiev static int wil_sta_seq_open(struct inode *inode, struct file *file)
14063df2cd36SVladimir Kondratiev {
14073df2cd36SVladimir Kondratiev 	return single_open(file, wil_sta_debugfs_show, inode->i_private);
14083df2cd36SVladimir Kondratiev }
14093df2cd36SVladimir Kondratiev 
14103df2cd36SVladimir Kondratiev static const struct file_operations fops_sta = {
14113df2cd36SVladimir Kondratiev 	.open		= wil_sta_seq_open,
14123df2cd36SVladimir Kondratiev 	.release	= single_release,
14133df2cd36SVladimir Kondratiev 	.read		= seq_read,
14143df2cd36SVladimir Kondratiev 	.llseek		= seq_lseek,
14153df2cd36SVladimir Kondratiev };
14163df2cd36SVladimir Kondratiev 
14172be7d22fSVladimir Kondratiev /*----------------*/
1418b541d0a0SVladimir Kondratiev static void wil6210_debugfs_init_blobs(struct wil6210_priv *wil,
1419b541d0a0SVladimir Kondratiev 				       struct dentry *dbg)
1420b541d0a0SVladimir Kondratiev {
1421b541d0a0SVladimir Kondratiev 	int i;
1422b541d0a0SVladimir Kondratiev 	char name[32];
1423b541d0a0SVladimir Kondratiev 
1424b541d0a0SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
1425b541d0a0SVladimir Kondratiev 		struct debugfs_blob_wrapper *blob = &wil->blobs[i];
1426b541d0a0SVladimir Kondratiev 		const struct fw_map *map = &fw_mapping[i];
1427b541d0a0SVladimir Kondratiev 
1428b541d0a0SVladimir Kondratiev 		if (!map->name)
1429b541d0a0SVladimir Kondratiev 			continue;
1430b541d0a0SVladimir Kondratiev 
1431b541d0a0SVladimir Kondratiev 		blob->data = (void * __force)wil->csr + HOSTADDR(map->host);
1432b541d0a0SVladimir Kondratiev 		blob->size = map->to - map->from;
1433b541d0a0SVladimir Kondratiev 		snprintf(name, sizeof(name), "blob_%s", map->name);
1434b541d0a0SVladimir Kondratiev 		wil_debugfs_create_ioblob(name, S_IRUGO, dbg, blob);
1435b541d0a0SVladimir Kondratiev 	}
1436b541d0a0SVladimir Kondratiev }
1437b541d0a0SVladimir Kondratiev 
1438b7cde470SVladimir Kondratiev /* misc files */
1439b7cde470SVladimir Kondratiev static const struct {
1440b7cde470SVladimir Kondratiev 	const char *name;
1441b7cde470SVladimir Kondratiev 	umode_t mode;
1442b7cde470SVladimir Kondratiev 	const struct file_operations *fops;
1443b7cde470SVladimir Kondratiev } dbg_files[] = {
1444b7cde470SVladimir Kondratiev 	{"mbox",	S_IRUGO,		&fops_mbox},
1445b7cde470SVladimir Kondratiev 	{"vrings",	S_IRUGO,		&fops_vring},
1446b7cde470SVladimir Kondratiev 	{"stations",	S_IRUGO,		&fops_sta},
1447b7cde470SVladimir Kondratiev 	{"desc",	S_IRUGO,		&fops_txdesc},
1448b7cde470SVladimir Kondratiev 	{"bf",		S_IRUGO,		&fops_bf},
1449b7cde470SVladimir Kondratiev 	{"ssid",	S_IRUGO | S_IWUSR,	&fops_ssid},
1450b7cde470SVladimir Kondratiev 	{"mem_val",	S_IRUGO,		&fops_memread},
1451b7cde470SVladimir Kondratiev 	{"reset",		  S_IWUSR,	&fops_reset},
1452b7cde470SVladimir Kondratiev 	{"rxon",		  S_IWUSR,	&fops_rxon},
1453b7cde470SVladimir Kondratiev 	{"tx_mgmt",		  S_IWUSR,	&fops_txmgmt},
1454b7cde470SVladimir Kondratiev 	{"wmi_send",		  S_IWUSR,	&fops_wmi},
145549cb5dfbSVladimir Kondratiev 	{"back",	S_IRUGO | S_IWUSR,	&fops_back},
1456dc16427bSVladimir Kondratiev 	{"pmccfg",	S_IRUGO | S_IWUSR,	&fops_pmccfg},
1457dc16427bSVladimir Kondratiev 	{"pmcdata",	S_IRUGO,		&fops_pmcdata},
1458b7cde470SVladimir Kondratiev 	{"temp",	S_IRUGO,		&fops_temp},
1459b7cde470SVladimir Kondratiev 	{"freq",	S_IRUGO,		&fops_freq},
1460b7cde470SVladimir Kondratiev 	{"link",	S_IRUGO,		&fops_link},
1461b7cde470SVladimir Kondratiev 	{"info",	S_IRUGO,		&fops_info},
1462c33407a8SVladimir Kondratiev 	{"recovery",	S_IRUGO | S_IWUSR,	&fops_recovery},
1463b7cde470SVladimir Kondratiev };
1464b7cde470SVladimir Kondratiev 
1465b7cde470SVladimir Kondratiev static void wil6210_debugfs_init_files(struct wil6210_priv *wil,
1466b7cde470SVladimir Kondratiev 				       struct dentry *dbg)
1467b7cde470SVladimir Kondratiev {
1468b7cde470SVladimir Kondratiev 	int i;
1469b7cde470SVladimir Kondratiev 
1470b7cde470SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(dbg_files); i++)
1471b7cde470SVladimir Kondratiev 		debugfs_create_file(dbg_files[i].name, dbg_files[i].mode, dbg,
1472b7cde470SVladimir Kondratiev 				    wil, dbg_files[i].fops);
1473b7cde470SVladimir Kondratiev }
1474b7cde470SVladimir Kondratiev 
1475b7cde470SVladimir Kondratiev /* interrupt control blocks */
1476b7cde470SVladimir Kondratiev static const struct {
1477b7cde470SVladimir Kondratiev 	const char *name;
1478b7cde470SVladimir Kondratiev 	u32 icr_off;
1479b7cde470SVladimir Kondratiev } dbg_icr[] = {
1480b7cde470SVladimir Kondratiev 	{"USER_ICR",		HOSTADDR(RGF_USER_USER_ICR)},
1481b7cde470SVladimir Kondratiev 	{"DMA_EP_TX_ICR",	HOSTADDR(RGF_DMA_EP_TX_ICR)},
1482b7cde470SVladimir Kondratiev 	{"DMA_EP_RX_ICR",	HOSTADDR(RGF_DMA_EP_RX_ICR)},
1483b7cde470SVladimir Kondratiev 	{"DMA_EP_MISC_ICR",	HOSTADDR(RGF_DMA_EP_MISC_ICR)},
1484b7cde470SVladimir Kondratiev };
1485b7cde470SVladimir Kondratiev 
1486b7cde470SVladimir Kondratiev static void wil6210_debugfs_init_isr(struct wil6210_priv *wil,
1487b7cde470SVladimir Kondratiev 				     struct dentry *dbg)
1488b7cde470SVladimir Kondratiev {
1489b7cde470SVladimir Kondratiev 	int i;
1490b7cde470SVladimir Kondratiev 
1491b7cde470SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(dbg_icr); i++)
1492b7cde470SVladimir Kondratiev 		wil6210_debugfs_create_ISR(wil, dbg_icr[i].name, dbg,
1493b7cde470SVladimir Kondratiev 					   dbg_icr[i].icr_off);
1494b7cde470SVladimir Kondratiev }
1495b7cde470SVladimir Kondratiev 
1496b7cde470SVladimir Kondratiev #define WIL_FIELD(name, mode, type) { __stringify(name), mode, \
1497b7cde470SVladimir Kondratiev 	offsetof(struct wil6210_priv, name), type}
1498b7cde470SVladimir Kondratiev 
1499b7cde470SVladimir Kondratiev /* fields in struct wil6210_priv */
1500b7cde470SVladimir Kondratiev static const struct dbg_off dbg_wil_off[] = {
1501774974e5SVladimir Kondratiev 	WIL_FIELD(privacy,	S_IRUGO,		doff_u32),
15029419b6a2SVladimir Kondratiev 	WIL_FIELD(status[0],	S_IRUGO | S_IWUSR,	doff_ulong),
1503b7cde470SVladimir Kondratiev 	WIL_FIELD(fw_version,	S_IRUGO,		doff_u32),
1504b7cde470SVladimir Kondratiev 	WIL_FIELD(hw_version,	S_IRUGO,		doff_x32),
1505c33407a8SVladimir Kondratiev 	WIL_FIELD(recovery_count, S_IRUGO,		doff_u32),
150602beaf1aSVladimir Kondratiev 	WIL_FIELD(ap_isolate,	S_IRUGO,		doff_u32),
1507b7cde470SVladimir Kondratiev 	{},
1508b7cde470SVladimir Kondratiev };
1509b7cde470SVladimir Kondratiev 
1510b7cde470SVladimir Kondratiev static const struct dbg_off dbg_wil_regs[] = {
1511b7cde470SVladimir Kondratiev 	{"RGF_MAC_MTRL_COUNTER_0", S_IRUGO, HOSTADDR(RGF_MAC_MTRL_COUNTER_0),
1512b7cde470SVladimir Kondratiev 		doff_io32},
1513b7cde470SVladimir Kondratiev 	{"RGF_USER_USAGE_1", S_IRUGO, HOSTADDR(RGF_USER_USAGE_1), doff_io32},
1514b7cde470SVladimir Kondratiev 	{},
1515b7cde470SVladimir Kondratiev };
1516b7cde470SVladimir Kondratiev 
1517b7cde470SVladimir Kondratiev /* static parameters */
1518b7cde470SVladimir Kondratiev static const struct dbg_off dbg_statics[] = {
1519b7cde470SVladimir Kondratiev 	{"desc_index",	S_IRUGO | S_IWUSR, (ulong)&dbg_txdesc_index, doff_u32},
1520b7cde470SVladimir Kondratiev 	{"vring_index",	S_IRUGO | S_IWUSR, (ulong)&dbg_vring_index, doff_u32},
1521b7cde470SVladimir Kondratiev 	{"mem_addr",	S_IRUGO | S_IWUSR, (ulong)&mem_addr, doff_u32},
15220436fd9aSVladimir Shulman 	{"vring_idle_trsh", S_IRUGO | S_IWUSR, (ulong)&vring_idle_trsh,
15230436fd9aSVladimir Shulman 	 doff_u32},
1524b7cde470SVladimir Kondratiev 	{},
1525b7cde470SVladimir Kondratiev };
1526b7cde470SVladimir Kondratiev 
15272be7d22fSVladimir Kondratiev int wil6210_debugfs_init(struct wil6210_priv *wil)
15282be7d22fSVladimir Kondratiev {
15292be7d22fSVladimir Kondratiev 	struct dentry *dbg = wil->debug = debugfs_create_dir(WIL_NAME,
15302be7d22fSVladimir Kondratiev 			wil_to_wiphy(wil)->debugfsdir);
15312be7d22fSVladimir Kondratiev 
15322be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(dbg))
15332be7d22fSVladimir Kondratiev 		return -ENODEV;
15342be7d22fSVladimir Kondratiev 
1535dc16427bSVladimir Kondratiev 	wil_pmc_init(wil);
1536dc16427bSVladimir Kondratiev 
1537b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_files(wil, dbg);
1538b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_isr(wil, dbg);
1539b541d0a0SVladimir Kondratiev 	wil6210_debugfs_init_blobs(wil, dbg);
1540b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dbg, wil, dbg_wil_off);
1541b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dbg, (void * __force)wil->csr,
1542b7cde470SVladimir Kondratiev 				    dbg_wil_regs);
1543b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dbg, NULL, dbg_statics);
1544b7cde470SVladimir Kondratiev 
1545b7cde470SVladimir Kondratiev 	wil6210_debugfs_create_pseudo_ISR(wil, dbg);
1546b7cde470SVladimir Kondratiev 
1547b7cde470SVladimir Kondratiev 	wil6210_debugfs_create_ITR_CNT(wil, dbg);
15482be7d22fSVladimir Kondratiev 
15492be7d22fSVladimir Kondratiev 	return 0;
15502be7d22fSVladimir Kondratiev }
15512be7d22fSVladimir Kondratiev 
15522be7d22fSVladimir Kondratiev void wil6210_debugfs_remove(struct wil6210_priv *wil)
15532be7d22fSVladimir Kondratiev {
15542be7d22fSVladimir Kondratiev 	debugfs_remove_recursive(wil->debug);
15552be7d22fSVladimir Kondratiev 	wil->debug = NULL;
1556dc16427bSVladimir Kondratiev 
1557dc16427bSVladimir Kondratiev 	/* free pmc memory without sending command to fw, as it will
1558dc16427bSVladimir Kondratiev 	 * be reset on the way down anyway
1559dc16427bSVladimir Kondratiev 	 */
1560dc16427bSVladimir Kondratiev 	wil_pmc_free(wil, false);
15612be7d22fSVladimir Kondratiev }
1562