xref: /linux/drivers/net/wireless/ath/wil6210/debugfs.c (revision 16e5c1fc36040e592128a164499bc25eb138a80f)
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) {
65b9eeb512SVladimir Kondratiev 		v = readl(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 
159a202fbbfSAndy Shevchenko static void wil_seq_hexdump(struct seq_file *s, void *p, int len,
160a202fbbfSAndy Shevchenko 			    const char *prefix)
161a202fbbfSAndy Shevchenko {
162a202fbbfSAndy Shevchenko 	seq_hex_dump(s, prefix, DUMP_PREFIX_NONE, 16, 1, p, len, false);
163a202fbbfSAndy Shevchenko }
164a202fbbfSAndy Shevchenko 
1652be7d22fSVladimir Kondratiev static void wil_print_ring(struct seq_file *s, const char *prefix,
1662be7d22fSVladimir Kondratiev 			   void __iomem *off)
1672be7d22fSVladimir Kondratiev {
1682be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
1692be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring r;
1702be7d22fSVladimir Kondratiev 	int rsize;
1712be7d22fSVladimir Kondratiev 	uint i;
1722be7d22fSVladimir Kondratiev 
1732be7d22fSVladimir Kondratiev 	wil_memcpy_fromio_32(&r, off, sizeof(r));
1742be7d22fSVladimir Kondratiev 	wil_mbox_ring_le2cpus(&r);
1752be7d22fSVladimir Kondratiev 	/*
1762be7d22fSVladimir Kondratiev 	 * we just read memory block from NIC. This memory may be
1772be7d22fSVladimir Kondratiev 	 * garbage. Check validity before using it.
1782be7d22fSVladimir Kondratiev 	 */
1792be7d22fSVladimir Kondratiev 	rsize = r.size / sizeof(struct wil6210_mbox_ring_desc);
1802be7d22fSVladimir Kondratiev 
1812be7d22fSVladimir Kondratiev 	seq_printf(s, "ring %s = {\n", prefix);
1822be7d22fSVladimir Kondratiev 	seq_printf(s, "  base = 0x%08x\n", r.base);
1832be7d22fSVladimir Kondratiev 	seq_printf(s, "  size = 0x%04x bytes -> %d entries\n", r.size, rsize);
1842be7d22fSVladimir Kondratiev 	seq_printf(s, "  tail = 0x%08x\n", r.tail);
1852be7d22fSVladimir Kondratiev 	seq_printf(s, "  head = 0x%08x\n", r.head);
1862be7d22fSVladimir Kondratiev 	seq_printf(s, "  entry size = %d\n", r.entry_size);
1872be7d22fSVladimir Kondratiev 
1882be7d22fSVladimir Kondratiev 	if (r.size % sizeof(struct wil6210_mbox_ring_desc)) {
1892be7d22fSVladimir Kondratiev 		seq_printf(s, "  ??? size is not multiple of %zd, garbage?\n",
1902be7d22fSVladimir Kondratiev 			   sizeof(struct wil6210_mbox_ring_desc));
1912be7d22fSVladimir Kondratiev 		goto out;
1922be7d22fSVladimir Kondratiev 	}
1932be7d22fSVladimir Kondratiev 
1942be7d22fSVladimir Kondratiev 	if (!wmi_addr(wil, r.base) ||
1952be7d22fSVladimir Kondratiev 	    !wmi_addr(wil, r.tail) ||
1962be7d22fSVladimir Kondratiev 	    !wmi_addr(wil, r.head)) {
1978fe59627SVladimir Kondratiev 		seq_puts(s, "  ??? pointers are garbage?\n");
1982be7d22fSVladimir Kondratiev 		goto out;
1992be7d22fSVladimir Kondratiev 	}
2002be7d22fSVladimir Kondratiev 
2012be7d22fSVladimir Kondratiev 	for (i = 0; i < rsize; i++) {
2022be7d22fSVladimir Kondratiev 		struct wil6210_mbox_ring_desc d;
2032be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr hdr;
2042be7d22fSVladimir Kondratiev 		size_t delta = i * sizeof(d);
2052be7d22fSVladimir Kondratiev 		void __iomem *x = wil->csr + HOSTADDR(r.base) + delta;
2062be7d22fSVladimir Kondratiev 
2072be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(&d, x, sizeof(d));
2082be7d22fSVladimir Kondratiev 
2092be7d22fSVladimir Kondratiev 		seq_printf(s, "  [%2x] %s %s%s 0x%08x", i,
2102be7d22fSVladimir Kondratiev 			   d.sync ? "F" : "E",
2112be7d22fSVladimir Kondratiev 			   (r.tail - r.base == delta) ? "t" : " ",
2122be7d22fSVladimir Kondratiev 			   (r.head - r.base == delta) ? "h" : " ",
2132be7d22fSVladimir Kondratiev 			   le32_to_cpu(d.addr));
2142be7d22fSVladimir Kondratiev 		if (0 == wmi_read_hdr(wil, d.addr, &hdr)) {
2152be7d22fSVladimir Kondratiev 			u16 len = le16_to_cpu(hdr.len);
2168fe59627SVladimir Kondratiev 
2172be7d22fSVladimir Kondratiev 			seq_printf(s, " -> %04x %04x %04x %02x\n",
2182be7d22fSVladimir Kondratiev 				   le16_to_cpu(hdr.seq), len,
2192be7d22fSVladimir Kondratiev 				   le16_to_cpu(hdr.type), hdr.flags);
2202be7d22fSVladimir Kondratiev 			if (len <= MAX_MBOXITEM_SIZE) {
2212be7d22fSVladimir Kondratiev 				unsigned char databuf[MAX_MBOXITEM_SIZE];
2222be7d22fSVladimir Kondratiev 				void __iomem *src = wmi_buffer(wil, d.addr) +
2232be7d22fSVladimir Kondratiev 					sizeof(struct wil6210_mbox_hdr);
2242be7d22fSVladimir Kondratiev 				/*
2252be7d22fSVladimir Kondratiev 				 * No need to check @src for validity -
2262be7d22fSVladimir Kondratiev 				 * we already validated @d.addr while
2272be7d22fSVladimir Kondratiev 				 * reading header
2282be7d22fSVladimir Kondratiev 				 */
2292be7d22fSVladimir Kondratiev 				wil_memcpy_fromio_32(databuf, src, len);
230a202fbbfSAndy Shevchenko 				wil_seq_hexdump(s, databuf, len, "      : ");
2312be7d22fSVladimir Kondratiev 			}
2322be7d22fSVladimir Kondratiev 		} else {
2338fe59627SVladimir Kondratiev 			seq_puts(s, "\n");
2342be7d22fSVladimir Kondratiev 		}
2352be7d22fSVladimir Kondratiev 	}
2362be7d22fSVladimir Kondratiev  out:
2378fe59627SVladimir Kondratiev 	seq_puts(s, "}\n");
2382be7d22fSVladimir Kondratiev }
2392be7d22fSVladimir Kondratiev 
2402be7d22fSVladimir Kondratiev static int wil_mbox_debugfs_show(struct seq_file *s, void *data)
2412be7d22fSVladimir Kondratiev {
2422be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
2432be7d22fSVladimir Kondratiev 
2442be7d22fSVladimir Kondratiev 	wil_print_ring(s, "tx", wil->csr + HOST_MBOX +
2452be7d22fSVladimir Kondratiev 		       offsetof(struct wil6210_mbox_ctl, tx));
2462be7d22fSVladimir Kondratiev 	wil_print_ring(s, "rx", wil->csr + HOST_MBOX +
2472be7d22fSVladimir Kondratiev 		       offsetof(struct wil6210_mbox_ctl, rx));
2482be7d22fSVladimir Kondratiev 
2492be7d22fSVladimir Kondratiev 	return 0;
2502be7d22fSVladimir Kondratiev }
2512be7d22fSVladimir Kondratiev 
2522be7d22fSVladimir Kondratiev static int wil_mbox_seq_open(struct inode *inode, struct file *file)
2532be7d22fSVladimir Kondratiev {
2542be7d22fSVladimir Kondratiev 	return single_open(file, wil_mbox_debugfs_show, inode->i_private);
2552be7d22fSVladimir Kondratiev }
2562be7d22fSVladimir Kondratiev 
2572be7d22fSVladimir Kondratiev static const struct file_operations fops_mbox = {
2582be7d22fSVladimir Kondratiev 	.open		= wil_mbox_seq_open,
2592be7d22fSVladimir Kondratiev 	.release	= single_release,
2602be7d22fSVladimir Kondratiev 	.read		= seq_read,
2612be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
2622be7d22fSVladimir Kondratiev };
2632be7d22fSVladimir Kondratiev 
2642be7d22fSVladimir Kondratiev static int wil_debugfs_iomem_x32_set(void *data, u64 val)
2652be7d22fSVladimir Kondratiev {
266b9eeb512SVladimir Kondratiev 	writel(val, (void __iomem *)data);
2672be7d22fSVladimir Kondratiev 	wmb(); /* make sure write propagated to HW */
2682be7d22fSVladimir Kondratiev 
2692be7d22fSVladimir Kondratiev 	return 0;
2702be7d22fSVladimir Kondratiev }
2712be7d22fSVladimir Kondratiev 
2722be7d22fSVladimir Kondratiev static int wil_debugfs_iomem_x32_get(void *data, u64 *val)
2732be7d22fSVladimir Kondratiev {
274b9eeb512SVladimir Kondratiev 	*val = readl((void __iomem *)data);
2752be7d22fSVladimir Kondratiev 
2762be7d22fSVladimir Kondratiev 	return 0;
2772be7d22fSVladimir Kondratiev }
2782be7d22fSVladimir Kondratiev 
2792be7d22fSVladimir Kondratiev DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, wil_debugfs_iomem_x32_get,
2802be7d22fSVladimir Kondratiev 			wil_debugfs_iomem_x32_set, "0x%08llx\n");
2812be7d22fSVladimir Kondratiev 
2822be7d22fSVladimir Kondratiev static struct dentry *wil_debugfs_create_iomem_x32(const char *name,
2830ecc833bSAl Viro 						   umode_t mode,
2842be7d22fSVladimir Kondratiev 						   struct dentry *parent,
285b7cde470SVladimir Kondratiev 						   void *value)
2862be7d22fSVladimir Kondratiev {
287b7cde470SVladimir Kondratiev 	return debugfs_create_file(name, mode, parent, value,
2882be7d22fSVladimir Kondratiev 				   &fops_iomem_x32);
2892be7d22fSVladimir Kondratiev }
2902be7d22fSVladimir Kondratiev 
2913de6cf20SVladimir Kondratiev static int wil_debugfs_ulong_set(void *data, u64 val)
2923de6cf20SVladimir Kondratiev {
2933de6cf20SVladimir Kondratiev 	*(ulong *)data = val;
2943de6cf20SVladimir Kondratiev 	return 0;
2953de6cf20SVladimir Kondratiev }
2968fe59627SVladimir Kondratiev 
2973de6cf20SVladimir Kondratiev static int wil_debugfs_ulong_get(void *data, u64 *val)
2983de6cf20SVladimir Kondratiev {
2993de6cf20SVladimir Kondratiev 	*val = *(ulong *)data;
3003de6cf20SVladimir Kondratiev 	return 0;
3013de6cf20SVladimir Kondratiev }
3028fe59627SVladimir Kondratiev 
3033de6cf20SVladimir Kondratiev DEFINE_SIMPLE_ATTRIBUTE(wil_fops_ulong, wil_debugfs_ulong_get,
3048ea06188SVladimir Kondratiev 			wil_debugfs_ulong_set, "0x%llx\n");
3053de6cf20SVladimir Kondratiev 
3063de6cf20SVladimir Kondratiev static struct dentry *wil_debugfs_create_ulong(const char *name, umode_t mode,
3073de6cf20SVladimir Kondratiev 					       struct dentry *parent,
3083de6cf20SVladimir Kondratiev 					       ulong *value)
3093de6cf20SVladimir Kondratiev {
3103de6cf20SVladimir Kondratiev 	return debugfs_create_file(name, mode, parent, value, &wil_fops_ulong);
3113de6cf20SVladimir Kondratiev }
3123de6cf20SVladimir Kondratiev 
313b7cde470SVladimir Kondratiev /**
314b7cde470SVladimir Kondratiev  * wil6210_debugfs_init_offset - create set of debugfs files
315b7cde470SVladimir Kondratiev  * @wil - driver's context, used for printing
316b7cde470SVladimir Kondratiev  * @dbg - directory on the debugfs, where files will be created
317b7cde470SVladimir Kondratiev  * @base - base address used in address calculation
318b7cde470SVladimir Kondratiev  * @tbl - table with file descriptions. Should be terminated with empty element.
319b7cde470SVladimir Kondratiev  *
320b7cde470SVladimir Kondratiev  * Creates files accordingly to the @tbl.
321b7cde470SVladimir Kondratiev  */
322b7cde470SVladimir Kondratiev static void wil6210_debugfs_init_offset(struct wil6210_priv *wil,
323b7cde470SVladimir Kondratiev 					struct dentry *dbg, void *base,
324b7cde470SVladimir Kondratiev 					const struct dbg_off * const tbl)
325b7cde470SVladimir Kondratiev {
326b7cde470SVladimir Kondratiev 	int i;
327b7cde470SVladimir Kondratiev 
328b7cde470SVladimir Kondratiev 	for (i = 0; tbl[i].name; i++) {
329867fa0d4SVladimir Kondratiev 		struct dentry *f;
330b7cde470SVladimir Kondratiev 
331b7cde470SVladimir Kondratiev 		switch (tbl[i].type) {
332b7cde470SVladimir Kondratiev 		case doff_u32:
333b7cde470SVladimir Kondratiev 			f = debugfs_create_u32(tbl[i].name, tbl[i].mode, dbg,
334b7cde470SVladimir Kondratiev 					       base + tbl[i].off);
335b7cde470SVladimir Kondratiev 			break;
336b7cde470SVladimir Kondratiev 		case doff_x32:
337b7cde470SVladimir Kondratiev 			f = debugfs_create_x32(tbl[i].name, tbl[i].mode, dbg,
338b7cde470SVladimir Kondratiev 					       base + tbl[i].off);
339b7cde470SVladimir Kondratiev 			break;
340b7cde470SVladimir Kondratiev 		case doff_ulong:
341b7cde470SVladimir Kondratiev 			f = wil_debugfs_create_ulong(tbl[i].name, tbl[i].mode,
342b7cde470SVladimir Kondratiev 						     dbg, base + tbl[i].off);
343b7cde470SVladimir Kondratiev 			break;
344b7cde470SVladimir Kondratiev 		case doff_io32:
345b7cde470SVladimir Kondratiev 			f = wil_debugfs_create_iomem_x32(tbl[i].name,
346b7cde470SVladimir Kondratiev 							 tbl[i].mode, dbg,
347b7cde470SVladimir Kondratiev 							 base + tbl[i].off);
348b7cde470SVladimir Kondratiev 			break;
349867fa0d4SVladimir Kondratiev 		default:
350867fa0d4SVladimir Kondratiev 			f = ERR_PTR(-EINVAL);
351b7cde470SVladimir Kondratiev 		}
352b7cde470SVladimir Kondratiev 		if (IS_ERR_OR_NULL(f))
353b7cde470SVladimir Kondratiev 			wil_err(wil, "Create file \"%s\": err %ld\n",
354b7cde470SVladimir Kondratiev 				tbl[i].name, PTR_ERR(f));
355b7cde470SVladimir Kondratiev 	}
356b7cde470SVladimir Kondratiev }
357b7cde470SVladimir Kondratiev 
358b7cde470SVladimir Kondratiev static const struct dbg_off isr_off[] = {
359b7cde470SVladimir Kondratiev 	{"ICC", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICC), doff_io32},
360b7cde470SVladimir Kondratiev 	{"ICR", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICR), doff_io32},
361b7cde470SVladimir Kondratiev 	{"ICM", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, ICM), doff_io32},
362b7cde470SVladimir Kondratiev 	{"ICS",		  S_IWUSR, offsetof(struct RGF_ICR, ICS), doff_io32},
363b7cde470SVladimir Kondratiev 	{"IMV", S_IRUGO | S_IWUSR, offsetof(struct RGF_ICR, IMV), doff_io32},
364b7cde470SVladimir Kondratiev 	{"IMS",		  S_IWUSR, offsetof(struct RGF_ICR, IMS), doff_io32},
365b7cde470SVladimir Kondratiev 	{"IMC",		  S_IWUSR, offsetof(struct RGF_ICR, IMC), doff_io32},
366b7cde470SVladimir Kondratiev 	{},
367b7cde470SVladimir Kondratiev };
3688fe59627SVladimir Kondratiev 
3692be7d22fSVladimir Kondratiev static int wil6210_debugfs_create_ISR(struct wil6210_priv *wil,
3702be7d22fSVladimir Kondratiev 				      const char *name,
3712be7d22fSVladimir Kondratiev 				      struct dentry *parent, u32 off)
3722be7d22fSVladimir Kondratiev {
3732be7d22fSVladimir Kondratiev 	struct dentry *d = debugfs_create_dir(name, parent);
3742be7d22fSVladimir Kondratiev 
3752be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(d))
3762be7d22fSVladimir Kondratiev 		return -ENODEV;
3772be7d22fSVladimir Kondratiev 
378b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr + off,
379b7cde470SVladimir Kondratiev 				    isr_off);
3802be7d22fSVladimir Kondratiev 
3812be7d22fSVladimir Kondratiev 	return 0;
3822be7d22fSVladimir Kondratiev }
3832be7d22fSVladimir Kondratiev 
384b7cde470SVladimir Kondratiev static const struct dbg_off pseudo_isr_off[] = {
385b7cde470SVladimir Kondratiev 	{"CAUSE",   S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE), doff_io32},
386b7cde470SVladimir Kondratiev 	{"MASK_SW", S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW), doff_io32},
387b7cde470SVladimir Kondratiev 	{"MASK_FW", S_IRUGO, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_FW), doff_io32},
388b7cde470SVladimir Kondratiev 	{},
389b7cde470SVladimir Kondratiev };
390b7cde470SVladimir Kondratiev 
3912be7d22fSVladimir Kondratiev static int wil6210_debugfs_create_pseudo_ISR(struct wil6210_priv *wil,
3922be7d22fSVladimir Kondratiev 					     struct dentry *parent)
3932be7d22fSVladimir Kondratiev {
3942be7d22fSVladimir Kondratiev 	struct dentry *d = debugfs_create_dir("PSEUDO_ISR", parent);
3952be7d22fSVladimir Kondratiev 
3962be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(d))
3972be7d22fSVladimir Kondratiev 		return -ENODEV;
3982be7d22fSVladimir Kondratiev 
399b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
400b7cde470SVladimir Kondratiev 				    pseudo_isr_off);
4012be7d22fSVladimir Kondratiev 
4022be7d22fSVladimir Kondratiev 	return 0;
4032be7d22fSVladimir Kondratiev }
4042be7d22fSVladimir Kondratiev 
40578366f69SVladimir Kondratiev static const struct dbg_off lgc_itr_cnt_off[] = {
406b7cde470SVladimir Kondratiev 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_TRSH), doff_io32},
407b7cde470SVladimir Kondratiev 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_DATA), doff_io32},
408b7cde470SVladimir Kondratiev 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_CNT_CRL), doff_io32},
409b7cde470SVladimir Kondratiev 	{},
410b7cde470SVladimir Kondratiev };
411b7cde470SVladimir Kondratiev 
41278366f69SVladimir Kondratiev static const struct dbg_off tx_itr_cnt_off[] = {
41378366f69SVladimir Kondratiev 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_TRSH),
41478366f69SVladimir Kondratiev 	 doff_io32},
41578366f69SVladimir Kondratiev 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_DATA),
41678366f69SVladimir Kondratiev 	 doff_io32},
41778366f69SVladimir Kondratiev 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_CNT_CTL),
41878366f69SVladimir Kondratiev 	 doff_io32},
41978366f69SVladimir Kondratiev 	{"IDL_TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_TRSH),
42078366f69SVladimir Kondratiev 	 doff_io32},
42178366f69SVladimir Kondratiev 	{"IDL_DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_DATA),
42278366f69SVladimir Kondratiev 	 doff_io32},
42378366f69SVladimir Kondratiev 	{"IDL_CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_CTL),
42478366f69SVladimir Kondratiev 	 doff_io32},
42578366f69SVladimir Kondratiev 	{},
42678366f69SVladimir Kondratiev };
42778366f69SVladimir Kondratiev 
42878366f69SVladimir Kondratiev static const struct dbg_off rx_itr_cnt_off[] = {
42978366f69SVladimir Kondratiev 	{"TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_TRSH),
43078366f69SVladimir Kondratiev 	 doff_io32},
43178366f69SVladimir Kondratiev 	{"DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_DATA),
43278366f69SVladimir Kondratiev 	 doff_io32},
43378366f69SVladimir Kondratiev 	{"CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_CNT_CTL),
43478366f69SVladimir Kondratiev 	 doff_io32},
43578366f69SVladimir Kondratiev 	{"IDL_TRSH", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_TRSH),
43678366f69SVladimir Kondratiev 	 doff_io32},
43778366f69SVladimir Kondratiev 	{"IDL_DATA", S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_DATA),
43878366f69SVladimir Kondratiev 	 doff_io32},
43978366f69SVladimir Kondratiev 	{"IDL_CTL",  S_IRUGO | S_IWUSR, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_CTL),
44078366f69SVladimir Kondratiev 	 doff_io32},
44178366f69SVladimir Kondratiev 	{},
44278366f69SVladimir Kondratiev };
44378366f69SVladimir Kondratiev 
4442be7d22fSVladimir Kondratiev static int wil6210_debugfs_create_ITR_CNT(struct wil6210_priv *wil,
4452be7d22fSVladimir Kondratiev 					  struct dentry *parent)
4462be7d22fSVladimir Kondratiev {
44778366f69SVladimir Kondratiev 	struct dentry *d, *dtx, *drx;
4482be7d22fSVladimir Kondratiev 
44978366f69SVladimir Kondratiev 	d = debugfs_create_dir("ITR_CNT", parent);
4502be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(d))
4512be7d22fSVladimir Kondratiev 		return -ENODEV;
4522be7d22fSVladimir Kondratiev 
45378366f69SVladimir Kondratiev 	dtx = debugfs_create_dir("TX", d);
45478366f69SVladimir Kondratiev 	drx = debugfs_create_dir("RX", d);
45578366f69SVladimir Kondratiev 	if (IS_ERR_OR_NULL(dtx) || IS_ERR_OR_NULL(drx))
45678366f69SVladimir Kondratiev 		return -ENODEV;
4572be7d22fSVladimir Kondratiev 
45878366f69SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
45978366f69SVladimir Kondratiev 				    lgc_itr_cnt_off);
46078366f69SVladimir Kondratiev 
46178366f69SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dtx, (void * __force)wil->csr,
46278366f69SVladimir Kondratiev 				    tx_itr_cnt_off);
46378366f69SVladimir Kondratiev 
46478366f69SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, drx, (void * __force)wil->csr,
46578366f69SVladimir Kondratiev 				    rx_itr_cnt_off);
4662be7d22fSVladimir Kondratiev 	return 0;
4672be7d22fSVladimir Kondratiev }
4682be7d22fSVladimir Kondratiev 
4692be7d22fSVladimir Kondratiev static int wil_memread_debugfs_show(struct seq_file *s, void *data)
4702be7d22fSVladimir Kondratiev {
4712be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
4722be7d22fSVladimir Kondratiev 	void __iomem *a = wmi_buffer(wil, cpu_to_le32(mem_addr));
4732be7d22fSVladimir Kondratiev 
4742be7d22fSVladimir Kondratiev 	if (a)
475b9eeb512SVladimir Kondratiev 		seq_printf(s, "[0x%08x] = 0x%08x\n", mem_addr, readl(a));
4762be7d22fSVladimir Kondratiev 	else
4772be7d22fSVladimir Kondratiev 		seq_printf(s, "[0x%08x] = INVALID\n", mem_addr);
4782be7d22fSVladimir Kondratiev 
4792be7d22fSVladimir Kondratiev 	return 0;
4802be7d22fSVladimir Kondratiev }
4812be7d22fSVladimir Kondratiev 
4822be7d22fSVladimir Kondratiev static int wil_memread_seq_open(struct inode *inode, struct file *file)
4832be7d22fSVladimir Kondratiev {
4842be7d22fSVladimir Kondratiev 	return single_open(file, wil_memread_debugfs_show, inode->i_private);
4852be7d22fSVladimir Kondratiev }
4862be7d22fSVladimir Kondratiev 
4872be7d22fSVladimir Kondratiev static const struct file_operations fops_memread = {
4882be7d22fSVladimir Kondratiev 	.open		= wil_memread_seq_open,
4892be7d22fSVladimir Kondratiev 	.release	= single_release,
4902be7d22fSVladimir Kondratiev 	.read		= seq_read,
4912be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
4922be7d22fSVladimir Kondratiev };
4932be7d22fSVladimir Kondratiev 
4942be7d22fSVladimir Kondratiev static ssize_t wil_read_file_ioblob(struct file *file, char __user *user_buf,
4952be7d22fSVladimir Kondratiev 				    size_t count, loff_t *ppos)
4962be7d22fSVladimir Kondratiev {
4972be7d22fSVladimir Kondratiev 	enum { max_count = 4096 };
4982be7d22fSVladimir Kondratiev 	struct debugfs_blob_wrapper *blob = file->private_data;
4992be7d22fSVladimir Kondratiev 	loff_t pos = *ppos;
5002be7d22fSVladimir Kondratiev 	size_t available = blob->size;
5012be7d22fSVladimir Kondratiev 	void *buf;
5022be7d22fSVladimir Kondratiev 	size_t ret;
5032be7d22fSVladimir Kondratiev 
5042be7d22fSVladimir Kondratiev 	if (pos < 0)
5052be7d22fSVladimir Kondratiev 		return -EINVAL;
5062be7d22fSVladimir Kondratiev 
5072be7d22fSVladimir Kondratiev 	if (pos >= available || !count)
5082be7d22fSVladimir Kondratiev 		return 0;
5092be7d22fSVladimir Kondratiev 
5102be7d22fSVladimir Kondratiev 	if (count > available - pos)
5112be7d22fSVladimir Kondratiev 		count = available - pos;
5122be7d22fSVladimir Kondratiev 	if (count > max_count)
5132be7d22fSVladimir Kondratiev 		count = max_count;
5142be7d22fSVladimir Kondratiev 
5152be7d22fSVladimir Kondratiev 	buf = kmalloc(count, GFP_KERNEL);
5162be7d22fSVladimir Kondratiev 	if (!buf)
5172be7d22fSVladimir Kondratiev 		return -ENOMEM;
5182be7d22fSVladimir Kondratiev 
5192be7d22fSVladimir Kondratiev 	wil_memcpy_fromio_32(buf, (const volatile void __iomem *)blob->data +
5202be7d22fSVladimir Kondratiev 			     pos, count);
5212be7d22fSVladimir Kondratiev 
5222be7d22fSVladimir Kondratiev 	ret = copy_to_user(user_buf, buf, count);
5232be7d22fSVladimir Kondratiev 	kfree(buf);
5242be7d22fSVladimir Kondratiev 	if (ret == count)
5252be7d22fSVladimir Kondratiev 		return -EFAULT;
5262be7d22fSVladimir Kondratiev 
5272be7d22fSVladimir Kondratiev 	count -= ret;
5282be7d22fSVladimir Kondratiev 	*ppos = pos + count;
5292be7d22fSVladimir Kondratiev 
5302be7d22fSVladimir Kondratiev 	return count;
5312be7d22fSVladimir Kondratiev }
5322be7d22fSVladimir Kondratiev 
5332be7d22fSVladimir Kondratiev static const struct file_operations fops_ioblob = {
5342be7d22fSVladimir Kondratiev 	.read =		wil_read_file_ioblob,
53593ecbd64SWei Yongjun 	.open =		simple_open,
5362be7d22fSVladimir Kondratiev 	.llseek =	default_llseek,
5372be7d22fSVladimir Kondratiev };
5382be7d22fSVladimir Kondratiev 
5392be7d22fSVladimir Kondratiev static
5402be7d22fSVladimir Kondratiev struct dentry *wil_debugfs_create_ioblob(const char *name,
5410ecc833bSAl Viro 					 umode_t mode,
5422be7d22fSVladimir Kondratiev 					 struct dentry *parent,
5432be7d22fSVladimir Kondratiev 					 struct debugfs_blob_wrapper *blob)
5442be7d22fSVladimir Kondratiev {
5452be7d22fSVladimir Kondratiev 	return debugfs_create_file(name, mode, parent, blob, &fops_ioblob);
5462be7d22fSVladimir Kondratiev }
5478fe59627SVladimir Kondratiev 
5482be7d22fSVladimir Kondratiev /*---reset---*/
5492be7d22fSVladimir Kondratiev static ssize_t wil_write_file_reset(struct file *file, const char __user *buf,
5502be7d22fSVladimir Kondratiev 				    size_t len, loff_t *ppos)
5512be7d22fSVladimir Kondratiev {
5522be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
5532be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
5542be7d22fSVladimir Kondratiev 
5552be7d22fSVladimir Kondratiev 	/**
5562be7d22fSVladimir Kondratiev 	 * BUG:
5572be7d22fSVladimir Kondratiev 	 * this code does NOT sync device state with the rest of system
5582be7d22fSVladimir Kondratiev 	 * use with care, debug only!!!
5592be7d22fSVladimir Kondratiev 	 */
5602be7d22fSVladimir Kondratiev 	rtnl_lock();
5612be7d22fSVladimir Kondratiev 	dev_close(ndev);
5622be7d22fSVladimir Kondratiev 	ndev->flags &= ~IFF_UP;
5632be7d22fSVladimir Kondratiev 	rtnl_unlock();
5642cd0f021SVladimir Kondratiev 	wil_reset(wil, true);
5652be7d22fSVladimir Kondratiev 
5662be7d22fSVladimir Kondratiev 	return len;
5672be7d22fSVladimir Kondratiev }
5682be7d22fSVladimir Kondratiev 
5692be7d22fSVladimir Kondratiev static const struct file_operations fops_reset = {
5702be7d22fSVladimir Kondratiev 	.write = wil_write_file_reset,
57193ecbd64SWei Yongjun 	.open  = simple_open,
5722be7d22fSVladimir Kondratiev };
5738fe59627SVladimir Kondratiev 
5740b39aaf2SVladimir Kondratiev /*---write channel 1..4 to rxon for it, 0 to rxoff---*/
5750b39aaf2SVladimir Kondratiev static ssize_t wil_write_file_rxon(struct file *file, const char __user *buf,
5760b39aaf2SVladimir Kondratiev 				   size_t len, loff_t *ppos)
5770b39aaf2SVladimir Kondratiev {
5780b39aaf2SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
5790b39aaf2SVladimir Kondratiev 	int rc;
5800b39aaf2SVladimir Kondratiev 	long channel;
5810b39aaf2SVladimir Kondratiev 	bool on;
5820b39aaf2SVladimir Kondratiev 
583*16e5c1fcSAl Viro 	char *kbuf = memdup_user_nul(buf, len);
5848fe59627SVladimir Kondratiev 
585*16e5c1fcSAl Viro 	if (IS_ERR(kbuf))
586*16e5c1fcSAl Viro 		return PTR_ERR(kbuf);
5870b39aaf2SVladimir Kondratiev 	rc = kstrtol(kbuf, 0, &channel);
5880b39aaf2SVladimir Kondratiev 	kfree(kbuf);
5890b39aaf2SVladimir Kondratiev 	if (rc)
5900b39aaf2SVladimir Kondratiev 		return rc;
5910b39aaf2SVladimir Kondratiev 
5920b39aaf2SVladimir Kondratiev 	if ((channel < 0) || (channel > 4)) {
5930b39aaf2SVladimir Kondratiev 		wil_err(wil, "Invalid channel %ld\n", channel);
5940b39aaf2SVladimir Kondratiev 		return -EINVAL;
5950b39aaf2SVladimir Kondratiev 	}
5960b39aaf2SVladimir Kondratiev 	on = !!channel;
5970b39aaf2SVladimir Kondratiev 
5980b39aaf2SVladimir Kondratiev 	if (on) {
5990b39aaf2SVladimir Kondratiev 		rc = wmi_set_channel(wil, (int)channel);
6000b39aaf2SVladimir Kondratiev 		if (rc)
6010b39aaf2SVladimir Kondratiev 			return rc;
6020b39aaf2SVladimir Kondratiev 	}
6030b39aaf2SVladimir Kondratiev 
6040b39aaf2SVladimir Kondratiev 	rc = wmi_rxon(wil, on);
6050b39aaf2SVladimir Kondratiev 	if (rc)
6060b39aaf2SVladimir Kondratiev 		return rc;
6070b39aaf2SVladimir Kondratiev 
6080b39aaf2SVladimir Kondratiev 	return len;
6090b39aaf2SVladimir Kondratiev }
6100b39aaf2SVladimir Kondratiev 
6110b39aaf2SVladimir Kondratiev static const struct file_operations fops_rxon = {
6120b39aaf2SVladimir Kondratiev 	.write = wil_write_file_rxon,
6130b39aaf2SVladimir Kondratiev 	.open  = simple_open,
6140b39aaf2SVladimir Kondratiev };
6158fe59627SVladimir Kondratiev 
61649cb5dfbSVladimir Kondratiev /* block ack control, write:
61749cb5dfbSVladimir Kondratiev  * - "add <ringid> <agg_size> <timeout>" to trigger ADDBA
61849cb5dfbSVladimir Kondratiev  * - "del_tx <ringid> <reason>" to trigger DELBA for Tx side
61949cb5dfbSVladimir Kondratiev  * - "del_rx <CID> <TID> <reason>" to trigger DELBA for Rx side
6203277213fSVladimir Kondratiev  */
62149cb5dfbSVladimir Kondratiev static ssize_t wil_write_back(struct file *file, const char __user *buf,
6223277213fSVladimir Kondratiev 			      size_t len, loff_t *ppos)
6233277213fSVladimir Kondratiev {
6243277213fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
6253277213fSVladimir Kondratiev 	int rc;
6263277213fSVladimir Kondratiev 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
6272a19f776SColin Ian King 	char cmd[9];
62849cb5dfbSVladimir Kondratiev 	int p1, p2, p3;
6293277213fSVladimir Kondratiev 
6303277213fSVladimir Kondratiev 	if (!kbuf)
6313277213fSVladimir Kondratiev 		return -ENOMEM;
6323277213fSVladimir Kondratiev 
6333277213fSVladimir Kondratiev 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
6343277213fSVladimir Kondratiev 	if (rc != len) {
6353277213fSVladimir Kondratiev 		kfree(kbuf);
6363277213fSVladimir Kondratiev 		return rc >= 0 ? -EIO : rc;
6373277213fSVladimir Kondratiev 	}
6383277213fSVladimir Kondratiev 
6393277213fSVladimir Kondratiev 	kbuf[len] = '\0';
64049cb5dfbSVladimir Kondratiev 	rc = sscanf(kbuf, "%8s %d %d %d", cmd, &p1, &p2, &p3);
6413277213fSVladimir Kondratiev 	kfree(kbuf);
6423277213fSVladimir Kondratiev 
64349cb5dfbSVladimir Kondratiev 	if (rc < 0)
6443277213fSVladimir Kondratiev 		return rc;
64549cb5dfbSVladimir Kondratiev 	if (rc < 2)
6463277213fSVladimir Kondratiev 		return -EINVAL;
6473277213fSVladimir Kondratiev 
64849cb5dfbSVladimir Kondratiev 	if (0 == strcmp(cmd, "add")) {
64949cb5dfbSVladimir Kondratiev 		if (rc < 3) {
65049cb5dfbSVladimir Kondratiev 			wil_err(wil, "BACK: add require at least 2 params\n");
65149cb5dfbSVladimir Kondratiev 			return -EINVAL;
65249cb5dfbSVladimir Kondratiev 		}
65349cb5dfbSVladimir Kondratiev 		if (rc < 4)
65449cb5dfbSVladimir Kondratiev 			p3 = 0;
65549cb5dfbSVladimir Kondratiev 		wmi_addba(wil, p1, p2, p3);
65649cb5dfbSVladimir Kondratiev 	} else if (0 == strcmp(cmd, "del_tx")) {
65749cb5dfbSVladimir Kondratiev 		if (rc < 3)
65849cb5dfbSVladimir Kondratiev 			p2 = WLAN_REASON_QSTA_LEAVE_QBSS;
65949cb5dfbSVladimir Kondratiev 		wmi_delba_tx(wil, p1, p2);
66049cb5dfbSVladimir Kondratiev 	} else if (0 == strcmp(cmd, "del_rx")) {
66149cb5dfbSVladimir Kondratiev 		if (rc < 3) {
66249cb5dfbSVladimir Kondratiev 			wil_err(wil,
66349cb5dfbSVladimir Kondratiev 				"BACK: del_rx require at least 2 params\n");
66449cb5dfbSVladimir Kondratiev 			return -EINVAL;
66549cb5dfbSVladimir Kondratiev 		}
66649cb5dfbSVladimir Kondratiev 		if (rc < 4)
66749cb5dfbSVladimir Kondratiev 			p3 = WLAN_REASON_QSTA_LEAVE_QBSS;
66849cb5dfbSVladimir Kondratiev 		wmi_delba_rx(wil, mk_cidxtid(p1, p2), p3);
66949cb5dfbSVladimir Kondratiev 	} else {
67049cb5dfbSVladimir Kondratiev 		wil_err(wil, "BACK: Unrecognized command \"%s\"\n", cmd);
67149cb5dfbSVladimir Kondratiev 		return -EINVAL;
67249cb5dfbSVladimir Kondratiev 	}
6733277213fSVladimir Kondratiev 
6743277213fSVladimir Kondratiev 	return len;
6753277213fSVladimir Kondratiev }
6763277213fSVladimir Kondratiev 
67749cb5dfbSVladimir Kondratiev static ssize_t wil_read_back(struct file *file, char __user *user_buf,
67849cb5dfbSVladimir Kondratiev 			     size_t count, loff_t *ppos)
67949cb5dfbSVladimir Kondratiev {
68049cb5dfbSVladimir Kondratiev 	static const char text[] = "block ack control, write:\n"
68149cb5dfbSVladimir Kondratiev 	" - \"add <ringid> <agg_size> <timeout>\" to trigger ADDBA\n"
68249cb5dfbSVladimir Kondratiev 	"If missing, <timeout> defaults to 0\n"
68349cb5dfbSVladimir Kondratiev 	" - \"del_tx <ringid> <reason>\" to trigger DELBA for Tx side\n"
68449cb5dfbSVladimir Kondratiev 	" - \"del_rx <CID> <TID> <reason>\" to trigger DELBA for Rx side\n"
68549cb5dfbSVladimir Kondratiev 	"If missing, <reason> set to \"STA_LEAVING\" (36)\n";
68649cb5dfbSVladimir Kondratiev 
68749cb5dfbSVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos, text,
68849cb5dfbSVladimir Kondratiev 				       sizeof(text));
68949cb5dfbSVladimir Kondratiev }
69049cb5dfbSVladimir Kondratiev 
69149cb5dfbSVladimir Kondratiev static const struct file_operations fops_back = {
69249cb5dfbSVladimir Kondratiev 	.read = wil_read_back,
69349cb5dfbSVladimir Kondratiev 	.write = wil_write_back,
6943277213fSVladimir Kondratiev 	.open  = simple_open,
6953277213fSVladimir Kondratiev };
6963277213fSVladimir Kondratiev 
697dc16427bSVladimir Kondratiev /* pmc control, write:
698dc16427bSVladimir Kondratiev  * - "alloc <num descriptors> <descriptor_size>" to allocate PMC
699dc16427bSVladimir Kondratiev  * - "free" to release memory allocated for PMC
700dc16427bSVladimir Kondratiev  */
701dc16427bSVladimir Kondratiev static ssize_t wil_write_pmccfg(struct file *file, const char __user *buf,
702dc16427bSVladimir Kondratiev 				size_t len, loff_t *ppos)
703dc16427bSVladimir Kondratiev {
704dc16427bSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
705dc16427bSVladimir Kondratiev 	int rc;
706dc16427bSVladimir Kondratiev 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
707dc16427bSVladimir Kondratiev 	char cmd[9];
708dc16427bSVladimir Kondratiev 	int num_descs, desc_size;
709dc16427bSVladimir Kondratiev 
710dc16427bSVladimir Kondratiev 	if (!kbuf)
711dc16427bSVladimir Kondratiev 		return -ENOMEM;
712dc16427bSVladimir Kondratiev 
713dc16427bSVladimir Kondratiev 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
714dc16427bSVladimir Kondratiev 	if (rc != len) {
715dc16427bSVladimir Kondratiev 		kfree(kbuf);
716dc16427bSVladimir Kondratiev 		return rc >= 0 ? -EIO : rc;
717dc16427bSVladimir Kondratiev 	}
718dc16427bSVladimir Kondratiev 
719dc16427bSVladimir Kondratiev 	kbuf[len] = '\0';
720dc16427bSVladimir Kondratiev 	rc = sscanf(kbuf, "%8s %d %d", cmd, &num_descs, &desc_size);
721dc16427bSVladimir Kondratiev 	kfree(kbuf);
722dc16427bSVladimir Kondratiev 
723dc16427bSVladimir Kondratiev 	if (rc < 0)
724dc16427bSVladimir Kondratiev 		return rc;
725dc16427bSVladimir Kondratiev 
726dc16427bSVladimir Kondratiev 	if (rc < 1) {
727dc16427bSVladimir Kondratiev 		wil_err(wil, "pmccfg: no params given\n");
728dc16427bSVladimir Kondratiev 		return -EINVAL;
729dc16427bSVladimir Kondratiev 	}
730dc16427bSVladimir Kondratiev 
731dc16427bSVladimir Kondratiev 	if (0 == strcmp(cmd, "alloc")) {
732dc16427bSVladimir Kondratiev 		if (rc != 3) {
733dc16427bSVladimir Kondratiev 			wil_err(wil, "pmccfg: alloc requires 2 params\n");
734dc16427bSVladimir Kondratiev 			return -EINVAL;
735dc16427bSVladimir Kondratiev 		}
736dc16427bSVladimir Kondratiev 		wil_pmc_alloc(wil, num_descs, desc_size);
737dc16427bSVladimir Kondratiev 	} else if (0 == strcmp(cmd, "free")) {
738dc16427bSVladimir Kondratiev 		if (rc != 1) {
739dc16427bSVladimir Kondratiev 			wil_err(wil, "pmccfg: free does not have any params\n");
740dc16427bSVladimir Kondratiev 			return -EINVAL;
741dc16427bSVladimir Kondratiev 		}
742dc16427bSVladimir Kondratiev 		wil_pmc_free(wil, true);
743dc16427bSVladimir Kondratiev 	} else {
744dc16427bSVladimir Kondratiev 		wil_err(wil, "pmccfg: Unrecognized command \"%s\"\n", cmd);
745dc16427bSVladimir Kondratiev 		return -EINVAL;
746dc16427bSVladimir Kondratiev 	}
747dc16427bSVladimir Kondratiev 
748dc16427bSVladimir Kondratiev 	return len;
749dc16427bSVladimir Kondratiev }
750dc16427bSVladimir Kondratiev 
751dc16427bSVladimir Kondratiev static ssize_t wil_read_pmccfg(struct file *file, char __user *user_buf,
752dc16427bSVladimir Kondratiev 			       size_t count, loff_t *ppos)
753dc16427bSVladimir Kondratiev {
754dc16427bSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
755dc16427bSVladimir Kondratiev 	char text[256];
756dc16427bSVladimir Kondratiev 	char help[] = "pmc control, write:\n"
757dc16427bSVladimir Kondratiev 	" - \"alloc <num descriptors> <descriptor_size>\" to allocate pmc\n"
758dc16427bSVladimir Kondratiev 	" - \"free\" to free memory allocated for pmc\n";
759dc16427bSVladimir Kondratiev 
760dc16427bSVladimir Kondratiev 	sprintf(text, "Last command status: %d\n\n%s",
761dc16427bSVladimir Kondratiev 		wil_pmc_last_cmd_status(wil),
762dc16427bSVladimir Kondratiev 		help);
763dc16427bSVladimir Kondratiev 
764dc16427bSVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos, text,
765dc16427bSVladimir Kondratiev 				       strlen(text) + 1);
766dc16427bSVladimir Kondratiev }
767dc16427bSVladimir Kondratiev 
768dc16427bSVladimir Kondratiev static const struct file_operations fops_pmccfg = {
769dc16427bSVladimir Kondratiev 	.read = wil_read_pmccfg,
770dc16427bSVladimir Kondratiev 	.write = wil_write_pmccfg,
771dc16427bSVladimir Kondratiev 	.open  = simple_open,
772dc16427bSVladimir Kondratiev };
773dc16427bSVladimir Kondratiev 
774dc16427bSVladimir Kondratiev static const struct file_operations fops_pmcdata = {
775dc16427bSVladimir Kondratiev 	.open		= simple_open,
776dc16427bSVladimir Kondratiev 	.read		= wil_pmc_read,
777dc16427bSVladimir Kondratiev 	.llseek		= wil_pmc_llseek,
778dc16427bSVladimir Kondratiev };
779dc16427bSVladimir Kondratiev 
7800b39aaf2SVladimir Kondratiev /*---tx_mgmt---*/
7810b39aaf2SVladimir Kondratiev /* Write mgmt frame to this file to send it */
7820b39aaf2SVladimir Kondratiev static ssize_t wil_write_file_txmgmt(struct file *file, const char __user *buf,
7830b39aaf2SVladimir Kondratiev 				     size_t len, loff_t *ppos)
7840b39aaf2SVladimir Kondratiev {
7850b39aaf2SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
7860b39aaf2SVladimir Kondratiev 	struct wiphy *wiphy = wil_to_wiphy(wil);
7870b39aaf2SVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
7880b39aaf2SVladimir Kondratiev 	struct cfg80211_mgmt_tx_params params;
7890b39aaf2SVladimir Kondratiev 	int rc;
7900b39aaf2SVladimir Kondratiev 	void *frame = kmalloc(len, GFP_KERNEL);
7918fe59627SVladimir Kondratiev 
7920b39aaf2SVladimir Kondratiev 	if (!frame)
7930b39aaf2SVladimir Kondratiev 		return -ENOMEM;
7940b39aaf2SVladimir Kondratiev 
7958e09b7d2SLino Sanfilippo 	if (copy_from_user(frame, buf, len)) {
7968e09b7d2SLino Sanfilippo 		kfree(frame);
7970b39aaf2SVladimir Kondratiev 		return -EIO;
7988e09b7d2SLino Sanfilippo 	}
7990b39aaf2SVladimir Kondratiev 
8000b39aaf2SVladimir Kondratiev 	params.buf = frame;
8010b39aaf2SVladimir Kondratiev 	params.len = len;
8020b39aaf2SVladimir Kondratiev 	params.chan = wdev->preset_chandef.chan;
8030b39aaf2SVladimir Kondratiev 
8040b39aaf2SVladimir Kondratiev 	rc = wil_cfg80211_mgmt_tx(wiphy, wdev, &params, NULL);
8050b39aaf2SVladimir Kondratiev 
8060b39aaf2SVladimir Kondratiev 	kfree(frame);
8070b39aaf2SVladimir Kondratiev 	wil_info(wil, "%s() -> %d\n", __func__, rc);
8080b39aaf2SVladimir Kondratiev 
8090b39aaf2SVladimir Kondratiev 	return len;
8100b39aaf2SVladimir Kondratiev }
8110b39aaf2SVladimir Kondratiev 
8120b39aaf2SVladimir Kondratiev static const struct file_operations fops_txmgmt = {
8130b39aaf2SVladimir Kondratiev 	.write = wil_write_file_txmgmt,
8140b39aaf2SVladimir Kondratiev 	.open  = simple_open,
8150b39aaf2SVladimir Kondratiev };
8162be7d22fSVladimir Kondratiev 
817ff974e40SVladimir Kondratiev /* Write WMI command (w/o mbox header) to this file to send it
818ff974e40SVladimir Kondratiev  * WMI starts from wil6210_mbox_hdr_wmi header
819ff974e40SVladimir Kondratiev  */
820ff974e40SVladimir Kondratiev static ssize_t wil_write_file_wmi(struct file *file, const char __user *buf,
821ff974e40SVladimir Kondratiev 				  size_t len, loff_t *ppos)
822ff974e40SVladimir Kondratiev {
823ff974e40SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
824ff974e40SVladimir Kondratiev 	struct wil6210_mbox_hdr_wmi *wmi;
825ff974e40SVladimir Kondratiev 	void *cmd;
826ff974e40SVladimir Kondratiev 	int cmdlen = len - sizeof(struct wil6210_mbox_hdr_wmi);
827ff974e40SVladimir Kondratiev 	u16 cmdid;
828ff974e40SVladimir Kondratiev 	int rc, rc1;
829ff974e40SVladimir Kondratiev 
830ff974e40SVladimir Kondratiev 	if (cmdlen <= 0)
831ff974e40SVladimir Kondratiev 		return -EINVAL;
832ff974e40SVladimir Kondratiev 
833ff974e40SVladimir Kondratiev 	wmi = kmalloc(len, GFP_KERNEL);
834ff974e40SVladimir Kondratiev 	if (!wmi)
835ff974e40SVladimir Kondratiev 		return -ENOMEM;
836ff974e40SVladimir Kondratiev 
837ff974e40SVladimir Kondratiev 	rc = simple_write_to_buffer(wmi, len, ppos, buf, len);
8388e09b7d2SLino Sanfilippo 	if (rc < 0) {
8398e09b7d2SLino Sanfilippo 		kfree(wmi);
840ff974e40SVladimir Kondratiev 		return rc;
8418e09b7d2SLino Sanfilippo 	}
842ff974e40SVladimir Kondratiev 
843ff974e40SVladimir Kondratiev 	cmd = &wmi[1];
844ff974e40SVladimir Kondratiev 	cmdid = le16_to_cpu(wmi->id);
845ff974e40SVladimir Kondratiev 
846ff974e40SVladimir Kondratiev 	rc1 = wmi_send(wil, cmdid, cmd, cmdlen);
847ff974e40SVladimir Kondratiev 	kfree(wmi);
848ff974e40SVladimir Kondratiev 
849ff974e40SVladimir Kondratiev 	wil_info(wil, "%s(0x%04x[%d]) -> %d\n", __func__, cmdid, cmdlen, rc1);
850ff974e40SVladimir Kondratiev 
851ff974e40SVladimir Kondratiev 	return rc;
852ff974e40SVladimir Kondratiev }
853ff974e40SVladimir Kondratiev 
854ff974e40SVladimir Kondratiev static const struct file_operations fops_wmi = {
855ff974e40SVladimir Kondratiev 	.write = wil_write_file_wmi,
856ff974e40SVladimir Kondratiev 	.open  = simple_open,
857ff974e40SVladimir Kondratiev };
858ff974e40SVladimir Kondratiev 
859c236658fSVladimir Kondratiev static void wil_seq_print_skb(struct seq_file *s, struct sk_buff *skb)
860c236658fSVladimir Kondratiev {
861c236658fSVladimir Kondratiev 	int i = 0;
862c236658fSVladimir Kondratiev 	int len = skb_headlen(skb);
863c236658fSVladimir Kondratiev 	void *p = skb->data;
864c236658fSVladimir Kondratiev 	int nr_frags = skb_shinfo(skb)->nr_frags;
865c236658fSVladimir Kondratiev 
866c236658fSVladimir Kondratiev 	seq_printf(s, "    len = %d\n", len);
867c236658fSVladimir Kondratiev 	wil_seq_hexdump(s, p, len, "      : ");
868c236658fSVladimir Kondratiev 
869c236658fSVladimir Kondratiev 	if (nr_frags) {
870c236658fSVladimir Kondratiev 		seq_printf(s, "    nr_frags = %d\n", nr_frags);
871c236658fSVladimir Kondratiev 		for (i = 0; i < nr_frags; i++) {
872c236658fSVladimir Kondratiev 			const struct skb_frag_struct *frag =
873c236658fSVladimir Kondratiev 					&skb_shinfo(skb)->frags[i];
874c236658fSVladimir Kondratiev 
875c236658fSVladimir Kondratiev 			len = skb_frag_size(frag);
876c236658fSVladimir Kondratiev 			p = skb_frag_address_safe(frag);
877c236658fSVladimir Kondratiev 			seq_printf(s, "    [%2d] : len = %d\n", i, len);
878c236658fSVladimir Kondratiev 			wil_seq_hexdump(s, p, len, "      : ");
879c236658fSVladimir Kondratiev 		}
880c236658fSVladimir Kondratiev 	}
881c236658fSVladimir Kondratiev }
882c236658fSVladimir Kondratiev 
8833a85543eSVladimir Kondratiev /*---------Tx/Rx descriptor------------*/
8842be7d22fSVladimir Kondratiev static int wil_txdesc_debugfs_show(struct seq_file *s, void *data)
8852be7d22fSVladimir Kondratiev {
8862be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
8873a85543eSVladimir Kondratiev 	struct vring *vring;
888af31cb5aSVladimir Kondratiev 	bool tx = (dbg_vring_index < WIL6210_MAX_TX_RINGS);
8898fe59627SVladimir Kondratiev 
8908fe59627SVladimir Kondratiev 	vring = tx ? &wil->vring_tx[dbg_vring_index] : &wil->vring_rx;
8912be7d22fSVladimir Kondratiev 
8922be7d22fSVladimir Kondratiev 	if (!vring->va) {
893af31cb5aSVladimir Kondratiev 		if (tx)
8943a85543eSVladimir Kondratiev 			seq_printf(s, "No Tx[%2d] VRING\n", dbg_vring_index);
8953a85543eSVladimir Kondratiev 		else
8963a85543eSVladimir Kondratiev 			seq_puts(s, "No Rx VRING\n");
8972be7d22fSVladimir Kondratiev 		return 0;
8982be7d22fSVladimir Kondratiev 	}
8992be7d22fSVladimir Kondratiev 
9002be7d22fSVladimir Kondratiev 	if (dbg_txdesc_index < vring->size) {
9013a85543eSVladimir Kondratiev 		/* use struct vring_tx_desc for Rx as well,
9023a85543eSVladimir Kondratiev 		 * only field used, .dma.length, is the same
9033a85543eSVladimir Kondratiev 		 */
9042be7d22fSVladimir Kondratiev 		volatile struct vring_tx_desc *d =
9058fe59627SVladimir Kondratiev 				&vring->va[dbg_txdesc_index].tx;
9062be7d22fSVladimir Kondratiev 		volatile u32 *u = (volatile u32 *)d;
907f88f113aSVladimir Kondratiev 		struct sk_buff *skb = vring->ctx[dbg_txdesc_index].skb;
9082be7d22fSVladimir Kondratiev 
909af31cb5aSVladimir Kondratiev 		if (tx)
9103a85543eSVladimir Kondratiev 			seq_printf(s, "Tx[%2d][%3d] = {\n", dbg_vring_index,
9113a85543eSVladimir Kondratiev 				   dbg_txdesc_index);
9123a85543eSVladimir Kondratiev 		else
9133a85543eSVladimir Kondratiev 			seq_printf(s, "Rx[%3d] = {\n", dbg_txdesc_index);
9142be7d22fSVladimir Kondratiev 		seq_printf(s, "  MAC = 0x%08x 0x%08x 0x%08x 0x%08x\n",
9152be7d22fSVladimir Kondratiev 			   u[0], u[1], u[2], u[3]);
9162be7d22fSVladimir Kondratiev 		seq_printf(s, "  DMA = 0x%08x 0x%08x 0x%08x 0x%08x\n",
9172be7d22fSVladimir Kondratiev 			   u[4], u[5], u[6], u[7]);
91839c52ee8SVladimir Kondratiev 		seq_printf(s, "  SKB = 0x%p\n", skb);
9192be7d22fSVladimir Kondratiev 
9202be7d22fSVladimir Kondratiev 		if (skb) {
921c236658fSVladimir Kondratiev 			skb_get(skb);
922c236658fSVladimir Kondratiev 			wil_seq_print_skb(s, skb);
923c236658fSVladimir Kondratiev 			kfree_skb(skb);
9242be7d22fSVladimir Kondratiev 		}
9258fe59627SVladimir Kondratiev 		seq_puts(s, "}\n");
9262be7d22fSVladimir Kondratiev 	} else {
927af31cb5aSVladimir Kondratiev 		if (tx)
9283a85543eSVladimir Kondratiev 			seq_printf(s, "[%2d] TxDesc index (%d) >= size (%d)\n",
9293a85543eSVladimir Kondratiev 				   dbg_vring_index, dbg_txdesc_index,
9303a85543eSVladimir Kondratiev 				   vring->size);
9313a85543eSVladimir Kondratiev 		else
9323a85543eSVladimir Kondratiev 			seq_printf(s, "RxDesc index (%d) >= size (%d)\n",
9332be7d22fSVladimir Kondratiev 				   dbg_txdesc_index, vring->size);
9342be7d22fSVladimir Kondratiev 	}
9352be7d22fSVladimir Kondratiev 
9362be7d22fSVladimir Kondratiev 	return 0;
9372be7d22fSVladimir Kondratiev }
9382be7d22fSVladimir Kondratiev 
9392be7d22fSVladimir Kondratiev static int wil_txdesc_seq_open(struct inode *inode, struct file *file)
9402be7d22fSVladimir Kondratiev {
9412be7d22fSVladimir Kondratiev 	return single_open(file, wil_txdesc_debugfs_show, inode->i_private);
9422be7d22fSVladimir Kondratiev }
9432be7d22fSVladimir Kondratiev 
9442be7d22fSVladimir Kondratiev static const struct file_operations fops_txdesc = {
9452be7d22fSVladimir Kondratiev 	.open		= wil_txdesc_seq_open,
9462be7d22fSVladimir Kondratiev 	.release	= single_release,
9472be7d22fSVladimir Kondratiev 	.read		= seq_read,
9482be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
9492be7d22fSVladimir Kondratiev };
9502be7d22fSVladimir Kondratiev 
9512be7d22fSVladimir Kondratiev /*---------beamforming------------*/
95236345ac3SVladimir Kondratiev static char *wil_bfstatus_str(u32 status)
95336345ac3SVladimir Kondratiev {
95436345ac3SVladimir Kondratiev 	switch (status) {
95536345ac3SVladimir Kondratiev 	case 0:
95636345ac3SVladimir Kondratiev 		return "Failed";
95736345ac3SVladimir Kondratiev 	case 1:
95836345ac3SVladimir Kondratiev 		return "OK";
95936345ac3SVladimir Kondratiev 	case 2:
96036345ac3SVladimir Kondratiev 		return "Retrying";
96136345ac3SVladimir Kondratiev 	default:
96236345ac3SVladimir Kondratiev 		return "??";
96336345ac3SVladimir Kondratiev 	}
96436345ac3SVladimir Kondratiev }
96536345ac3SVladimir Kondratiev 
96636345ac3SVladimir Kondratiev static bool is_all_zeros(void * const x_, size_t sz)
96736345ac3SVladimir Kondratiev {
96836345ac3SVladimir Kondratiev 	/* if reply is all-0, ignore this CID */
96936345ac3SVladimir Kondratiev 	u32 *x = x_;
97036345ac3SVladimir Kondratiev 	int n;
97136345ac3SVladimir Kondratiev 
97236345ac3SVladimir Kondratiev 	for (n = 0; n < sz / sizeof(*x); n++)
97336345ac3SVladimir Kondratiev 		if (x[n])
97436345ac3SVladimir Kondratiev 			return false;
97536345ac3SVladimir Kondratiev 
97636345ac3SVladimir Kondratiev 	return true;
97736345ac3SVladimir Kondratiev }
97836345ac3SVladimir Kondratiev 
9792be7d22fSVladimir Kondratiev static int wil_bf_debugfs_show(struct seq_file *s, void *data)
9802be7d22fSVladimir Kondratiev {
98136345ac3SVladimir Kondratiev 	int rc;
98236345ac3SVladimir Kondratiev 	int i;
9832be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
98436345ac3SVladimir Kondratiev 	struct wmi_notify_req_cmd cmd = {
98536345ac3SVladimir Kondratiev 		.interval_usec = 0,
98636345ac3SVladimir Kondratiev 	};
98736345ac3SVladimir Kondratiev 	struct {
98836345ac3SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
98936345ac3SVladimir Kondratiev 		struct wmi_notify_req_done_event evt;
99036345ac3SVladimir Kondratiev 	} __packed reply;
99136345ac3SVladimir Kondratiev 
99236345ac3SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
99336345ac3SVladimir Kondratiev 		u32 status;
99436345ac3SVladimir Kondratiev 
99536345ac3SVladimir Kondratiev 		cmd.cid = i;
99636345ac3SVladimir Kondratiev 		rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
99736345ac3SVladimir Kondratiev 			      WMI_NOTIFY_REQ_DONE_EVENTID, &reply,
99836345ac3SVladimir Kondratiev 			      sizeof(reply), 20);
99936345ac3SVladimir Kondratiev 		/* if reply is all-0, ignore this CID */
100036345ac3SVladimir Kondratiev 		if (rc || is_all_zeros(&reply.evt, sizeof(reply.evt)))
100136345ac3SVladimir Kondratiev 			continue;
100236345ac3SVladimir Kondratiev 
100336345ac3SVladimir Kondratiev 		status = le32_to_cpu(reply.evt.status);
100436345ac3SVladimir Kondratiev 		seq_printf(s, "CID %d {\n"
100536345ac3SVladimir Kondratiev 			   "  TSF = 0x%016llx\n"
100636345ac3SVladimir Kondratiev 			   "  TxMCS = %2d TxTpt = %4d\n"
100736345ac3SVladimir Kondratiev 			   "  SQI = %4d\n"
100836345ac3SVladimir Kondratiev 			   "  Status = 0x%08x %s\n"
100936345ac3SVladimir Kondratiev 			   "  Sectors(rx:tx) my %2d:%2d peer %2d:%2d\n"
101036345ac3SVladimir Kondratiev 			   "  Goodput(rx:tx) %4d:%4d\n"
101136345ac3SVladimir Kondratiev 			   "}\n",
101236345ac3SVladimir Kondratiev 			   i,
101336345ac3SVladimir Kondratiev 			   le64_to_cpu(reply.evt.tsf),
101436345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.bf_mcs),
101536345ac3SVladimir Kondratiev 			   le32_to_cpu(reply.evt.tx_tpt),
101636345ac3SVladimir Kondratiev 			   reply.evt.sqi,
101736345ac3SVladimir Kondratiev 			   status, wil_bfstatus_str(status),
101836345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.my_rx_sector),
101936345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.my_tx_sector),
102036345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.other_rx_sector),
102136345ac3SVladimir Kondratiev 			   le16_to_cpu(reply.evt.other_tx_sector),
102236345ac3SVladimir Kondratiev 			   le32_to_cpu(reply.evt.rx_goodput),
102336345ac3SVladimir Kondratiev 			   le32_to_cpu(reply.evt.tx_goodput));
102436345ac3SVladimir Kondratiev 	}
10252be7d22fSVladimir Kondratiev 	return 0;
10262be7d22fSVladimir Kondratiev }
10272be7d22fSVladimir Kondratiev 
10282be7d22fSVladimir Kondratiev static int wil_bf_seq_open(struct inode *inode, struct file *file)
10292be7d22fSVladimir Kondratiev {
10302be7d22fSVladimir Kondratiev 	return single_open(file, wil_bf_debugfs_show, inode->i_private);
10312be7d22fSVladimir Kondratiev }
10322be7d22fSVladimir Kondratiev 
10332be7d22fSVladimir Kondratiev static const struct file_operations fops_bf = {
10342be7d22fSVladimir Kondratiev 	.open		= wil_bf_seq_open,
10352be7d22fSVladimir Kondratiev 	.release	= single_release,
10362be7d22fSVladimir Kondratiev 	.read		= seq_read,
10372be7d22fSVladimir Kondratiev 	.llseek		= seq_lseek,
10382be7d22fSVladimir Kondratiev };
10398fe59627SVladimir Kondratiev 
10402be7d22fSVladimir Kondratiev /*---------SSID------------*/
10412be7d22fSVladimir Kondratiev static ssize_t wil_read_file_ssid(struct file *file, char __user *user_buf,
10422be7d22fSVladimir Kondratiev 				  size_t count, loff_t *ppos)
10432be7d22fSVladimir Kondratiev {
10442be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
10452be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
10462be7d22fSVladimir Kondratiev 
10472be7d22fSVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos,
10482be7d22fSVladimir Kondratiev 				       wdev->ssid, wdev->ssid_len);
10492be7d22fSVladimir Kondratiev }
10502be7d22fSVladimir Kondratiev 
10512be7d22fSVladimir Kondratiev static ssize_t wil_write_file_ssid(struct file *file, const char __user *buf,
10522be7d22fSVladimir Kondratiev 				   size_t count, loff_t *ppos)
10532be7d22fSVladimir Kondratiev {
10542be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
10552be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
10562be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
10572be7d22fSVladimir Kondratiev 
10582be7d22fSVladimir Kondratiev 	if (*ppos != 0) {
10592be7d22fSVladimir Kondratiev 		wil_err(wil, "Unable to set SSID substring from [%d]\n",
10602be7d22fSVladimir Kondratiev 			(int)*ppos);
10612be7d22fSVladimir Kondratiev 		return -EINVAL;
10622be7d22fSVladimir Kondratiev 	}
10632be7d22fSVladimir Kondratiev 
10642be7d22fSVladimir Kondratiev 	if (count > sizeof(wdev->ssid)) {
10652be7d22fSVladimir Kondratiev 		wil_err(wil, "SSID too long, len = %d\n", (int)count);
10662be7d22fSVladimir Kondratiev 		return -EINVAL;
10672be7d22fSVladimir Kondratiev 	}
10682be7d22fSVladimir Kondratiev 	if (netif_running(ndev)) {
10692be7d22fSVladimir Kondratiev 		wil_err(wil, "Unable to change SSID on running interface\n");
10702be7d22fSVladimir Kondratiev 		return -EINVAL;
10712be7d22fSVladimir Kondratiev 	}
10722be7d22fSVladimir Kondratiev 
10732be7d22fSVladimir Kondratiev 	wdev->ssid_len = count;
10742be7d22fSVladimir Kondratiev 	return simple_write_to_buffer(wdev->ssid, wdev->ssid_len, ppos,
10752be7d22fSVladimir Kondratiev 				      buf, count);
10762be7d22fSVladimir Kondratiev }
10772be7d22fSVladimir Kondratiev 
10782be7d22fSVladimir Kondratiev static const struct file_operations fops_ssid = {
10792be7d22fSVladimir Kondratiev 	.read = wil_read_file_ssid,
10802be7d22fSVladimir Kondratiev 	.write = wil_write_file_ssid,
108193ecbd64SWei Yongjun 	.open  = simple_open,
10822be7d22fSVladimir Kondratiev };
10832be7d22fSVladimir Kondratiev 
10841a2780e0SVladimir Kondratiev /*---------temp------------*/
10851a2780e0SVladimir Kondratiev static void print_temp(struct seq_file *s, const char *prefix, u32 t)
10861a2780e0SVladimir Kondratiev {
10871a2780e0SVladimir Kondratiev 	switch (t) {
10881a2780e0SVladimir Kondratiev 	case 0:
10891a2780e0SVladimir Kondratiev 	case ~(u32)0:
10901a2780e0SVladimir Kondratiev 		seq_printf(s, "%s N/A\n", prefix);
10911a2780e0SVladimir Kondratiev 	break;
10921a2780e0SVladimir Kondratiev 	default:
10931a2780e0SVladimir Kondratiev 		seq_printf(s, "%s %d.%03d\n", prefix, t / 1000, t % 1000);
10941a2780e0SVladimir Kondratiev 		break;
10951a2780e0SVladimir Kondratiev 	}
10961a2780e0SVladimir Kondratiev }
10971a2780e0SVladimir Kondratiev 
10981a2780e0SVladimir Kondratiev static int wil_temp_debugfs_show(struct seq_file *s, void *data)
10991a2780e0SVladimir Kondratiev {
11001a2780e0SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
11011a2780e0SVladimir Kondratiev 	u32 t_m, t_r;
11021a2780e0SVladimir Kondratiev 	int rc = wmi_get_temperature(wil, &t_m, &t_r);
11038fe59627SVladimir Kondratiev 
11041a2780e0SVladimir Kondratiev 	if (rc) {
11058fe59627SVladimir Kondratiev 		seq_puts(s, "Failed\n");
11061a2780e0SVladimir Kondratiev 		return 0;
11071a2780e0SVladimir Kondratiev 	}
11081a2780e0SVladimir Kondratiev 
1109d45cff9fSVladimir Kondratiev 	print_temp(s, "T_mac   =", t_m);
1110d45cff9fSVladimir Kondratiev 	print_temp(s, "T_radio =", t_r);
11111a2780e0SVladimir Kondratiev 
11121a2780e0SVladimir Kondratiev 	return 0;
11131a2780e0SVladimir Kondratiev }
11141a2780e0SVladimir Kondratiev 
11151a2780e0SVladimir Kondratiev static int wil_temp_seq_open(struct inode *inode, struct file *file)
11161a2780e0SVladimir Kondratiev {
11171a2780e0SVladimir Kondratiev 	return single_open(file, wil_temp_debugfs_show, inode->i_private);
11181a2780e0SVladimir Kondratiev }
11191a2780e0SVladimir Kondratiev 
11201a2780e0SVladimir Kondratiev static const struct file_operations fops_temp = {
11211a2780e0SVladimir Kondratiev 	.open		= wil_temp_seq_open,
11221a2780e0SVladimir Kondratiev 	.release	= single_release,
11231a2780e0SVladimir Kondratiev 	.read		= seq_read,
11241a2780e0SVladimir Kondratiev 	.llseek		= seq_lseek,
11251a2780e0SVladimir Kondratiev };
11261a2780e0SVladimir Kondratiev 
11279eb82d43SVladimir Kondratiev /*---------freq------------*/
11289eb82d43SVladimir Kondratiev static int wil_freq_debugfs_show(struct seq_file *s, void *data)
11299eb82d43SVladimir Kondratiev {
11309eb82d43SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
11319eb82d43SVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
11329eb82d43SVladimir Kondratiev 	u16 freq = wdev->chandef.chan ? wdev->chandef.chan->center_freq : 0;
11339eb82d43SVladimir Kondratiev 
11349eb82d43SVladimir Kondratiev 	seq_printf(s, "Freq = %d\n", freq);
11359eb82d43SVladimir Kondratiev 
11369eb82d43SVladimir Kondratiev 	return 0;
11379eb82d43SVladimir Kondratiev }
11389eb82d43SVladimir Kondratiev 
11399eb82d43SVladimir Kondratiev static int wil_freq_seq_open(struct inode *inode, struct file *file)
11409eb82d43SVladimir Kondratiev {
11419eb82d43SVladimir Kondratiev 	return single_open(file, wil_freq_debugfs_show, inode->i_private);
11429eb82d43SVladimir Kondratiev }
11439eb82d43SVladimir Kondratiev 
11449eb82d43SVladimir Kondratiev static const struct file_operations fops_freq = {
11459eb82d43SVladimir Kondratiev 	.open		= wil_freq_seq_open,
11469eb82d43SVladimir Kondratiev 	.release	= single_release,
11479eb82d43SVladimir Kondratiev 	.read		= seq_read,
11489eb82d43SVladimir Kondratiev 	.llseek		= seq_lseek,
11499eb82d43SVladimir Kondratiev };
11509eb82d43SVladimir Kondratiev 
11519eb82d43SVladimir Kondratiev /*---------link------------*/
11529eb82d43SVladimir Kondratiev static int wil_link_debugfs_show(struct seq_file *s, void *data)
11539eb82d43SVladimir Kondratiev {
11549eb82d43SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
11559eb82d43SVladimir Kondratiev 	struct station_info sinfo;
11569eb82d43SVladimir Kondratiev 	int i, rc;
11579eb82d43SVladimir Kondratiev 
11589eb82d43SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
11599eb82d43SVladimir Kondratiev 		struct wil_sta_info *p = &wil->sta[i];
11609eb82d43SVladimir Kondratiev 		char *status = "unknown";
11618fe59627SVladimir Kondratiev 
11629eb82d43SVladimir Kondratiev 		switch (p->status) {
11639eb82d43SVladimir Kondratiev 		case wil_sta_unused:
11649eb82d43SVladimir Kondratiev 			status = "unused   ";
11659eb82d43SVladimir Kondratiev 			break;
11669eb82d43SVladimir Kondratiev 		case wil_sta_conn_pending:
11679eb82d43SVladimir Kondratiev 			status = "pending  ";
11689eb82d43SVladimir Kondratiev 			break;
11699eb82d43SVladimir Kondratiev 		case wil_sta_connected:
11709eb82d43SVladimir Kondratiev 			status = "connected";
11719eb82d43SVladimir Kondratiev 			break;
11729eb82d43SVladimir Kondratiev 		}
1173230d8442SVladimir Kondratiev 		seq_printf(s, "[%d] %pM %s\n", i, p->addr, status);
11749eb82d43SVladimir Kondratiev 
11759eb82d43SVladimir Kondratiev 		if (p->status == wil_sta_connected) {
11769eb82d43SVladimir Kondratiev 			rc = wil_cid_fill_sinfo(wil, i, &sinfo);
11779eb82d43SVladimir Kondratiev 			if (rc)
11789eb82d43SVladimir Kondratiev 				return rc;
11799eb82d43SVladimir Kondratiev 
11809eb82d43SVladimir Kondratiev 			seq_printf(s, "  Tx_mcs = %d\n", sinfo.txrate.mcs);
11819eb82d43SVladimir Kondratiev 			seq_printf(s, "  Rx_mcs = %d\n", sinfo.rxrate.mcs);
11829eb82d43SVladimir Kondratiev 			seq_printf(s, "  SQ     = %d\n", sinfo.signal);
11839eb82d43SVladimir Kondratiev 		}
11849eb82d43SVladimir Kondratiev 	}
11859eb82d43SVladimir Kondratiev 
11869eb82d43SVladimir Kondratiev 	return 0;
11879eb82d43SVladimir Kondratiev }
11889eb82d43SVladimir Kondratiev 
11899eb82d43SVladimir Kondratiev static int wil_link_seq_open(struct inode *inode, struct file *file)
11909eb82d43SVladimir Kondratiev {
11919eb82d43SVladimir Kondratiev 	return single_open(file, wil_link_debugfs_show, inode->i_private);
11929eb82d43SVladimir Kondratiev }
11939eb82d43SVladimir Kondratiev 
11949eb82d43SVladimir Kondratiev static const struct file_operations fops_link = {
11959eb82d43SVladimir Kondratiev 	.open		= wil_link_seq_open,
11969eb82d43SVladimir Kondratiev 	.release	= single_release,
11979eb82d43SVladimir Kondratiev 	.read		= seq_read,
11989eb82d43SVladimir Kondratiev 	.llseek		= seq_lseek,
11999eb82d43SVladimir Kondratiev };
12009eb82d43SVladimir Kondratiev 
120184bb29b7SVladimir Kondratiev /*---------info------------*/
120284bb29b7SVladimir Kondratiev static int wil_info_debugfs_show(struct seq_file *s, void *data)
120384bb29b7SVladimir Kondratiev {
1204be299858SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
1205be299858SVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
120684bb29b7SVladimir Kondratiev 	int is_ac = power_supply_is_system_supplied();
1207be299858SVladimir Kondratiev 	int rx = atomic_xchg(&wil->isr_count_rx, 0);
1208be299858SVladimir Kondratiev 	int tx = atomic_xchg(&wil->isr_count_tx, 0);
1209be299858SVladimir Kondratiev 	static ulong rxf_old, txf_old;
1210be299858SVladimir Kondratiev 	ulong rxf = ndev->stats.rx_packets;
1211be299858SVladimir Kondratiev 	ulong txf = ndev->stats.tx_packets;
121255f8f680SVladimir Kondratiev 	unsigned int i;
121384bb29b7SVladimir Kondratiev 
121484bb29b7SVladimir Kondratiev 	/* >0 : AC; 0 : battery; <0 : error */
121584bb29b7SVladimir Kondratiev 	seq_printf(s, "AC powered : %d\n", is_ac);
1216be299858SVladimir Kondratiev 	seq_printf(s, "Rx irqs:packets : %8d : %8ld\n", rx, rxf - rxf_old);
1217be299858SVladimir Kondratiev 	seq_printf(s, "Tx irqs:packets : %8d : %8ld\n", tx, txf - txf_old);
1218be299858SVladimir Kondratiev 	rxf_old = rxf;
1219be299858SVladimir Kondratiev 	txf_old = txf;
122084bb29b7SVladimir Kondratiev 
122155f8f680SVladimir Kondratiev #define CHECK_QSTATE(x) (state & BIT(__QUEUE_STATE_ ## x)) ? \
122255f8f680SVladimir Kondratiev 	" " __stringify(x) : ""
122355f8f680SVladimir Kondratiev 
122455f8f680SVladimir Kondratiev 	for (i = 0; i < ndev->num_tx_queues; i++) {
122555f8f680SVladimir Kondratiev 		struct netdev_queue *txq = netdev_get_tx_queue(ndev, i);
122655f8f680SVladimir Kondratiev 		unsigned long state = txq->state;
122755f8f680SVladimir Kondratiev 
122855f8f680SVladimir Kondratiev 		seq_printf(s, "Tx queue[%i] state : 0x%lx%s%s%s\n", i, state,
122955f8f680SVladimir Kondratiev 			   CHECK_QSTATE(DRV_XOFF),
123055f8f680SVladimir Kondratiev 			   CHECK_QSTATE(STACK_XOFF),
123155f8f680SVladimir Kondratiev 			   CHECK_QSTATE(FROZEN)
123255f8f680SVladimir Kondratiev 			  );
123355f8f680SVladimir Kondratiev 	}
123455f8f680SVladimir Kondratiev #undef CHECK_QSTATE
123584bb29b7SVladimir Kondratiev 	return 0;
123684bb29b7SVladimir Kondratiev }
123784bb29b7SVladimir Kondratiev 
123884bb29b7SVladimir Kondratiev static int wil_info_seq_open(struct inode *inode, struct file *file)
123984bb29b7SVladimir Kondratiev {
124084bb29b7SVladimir Kondratiev 	return single_open(file, wil_info_debugfs_show, inode->i_private);
124184bb29b7SVladimir Kondratiev }
124284bb29b7SVladimir Kondratiev 
124384bb29b7SVladimir Kondratiev static const struct file_operations fops_info = {
124484bb29b7SVladimir Kondratiev 	.open		= wil_info_seq_open,
124584bb29b7SVladimir Kondratiev 	.release	= single_release,
124684bb29b7SVladimir Kondratiev 	.read		= seq_read,
124784bb29b7SVladimir Kondratiev 	.llseek		= seq_lseek,
124884bb29b7SVladimir Kondratiev };
124984bb29b7SVladimir Kondratiev 
1250c33407a8SVladimir Kondratiev /*---------recovery------------*/
1251c33407a8SVladimir Kondratiev /* mode = [manual|auto]
1252c33407a8SVladimir Kondratiev  * state = [idle|pending|running]
1253c33407a8SVladimir Kondratiev  */
1254c33407a8SVladimir Kondratiev static ssize_t wil_read_file_recovery(struct file *file, char __user *user_buf,
1255c33407a8SVladimir Kondratiev 				      size_t count, loff_t *ppos)
1256c33407a8SVladimir Kondratiev {
1257c33407a8SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
1258c33407a8SVladimir Kondratiev 	char buf[80];
1259c33407a8SVladimir Kondratiev 	int n;
1260c33407a8SVladimir Kondratiev 	static const char * const sstate[] = {"idle", "pending", "running"};
1261c33407a8SVladimir Kondratiev 
1262c33407a8SVladimir Kondratiev 	n = snprintf(buf, sizeof(buf), "mode = %s\nstate = %s\n",
1263c33407a8SVladimir Kondratiev 		     no_fw_recovery ? "manual" : "auto",
1264c33407a8SVladimir Kondratiev 		     sstate[wil->recovery_state]);
1265c33407a8SVladimir Kondratiev 
1266c33407a8SVladimir Kondratiev 	n = min_t(int, n, sizeof(buf));
1267c33407a8SVladimir Kondratiev 
1268c33407a8SVladimir Kondratiev 	return simple_read_from_buffer(user_buf, count, ppos,
1269c33407a8SVladimir Kondratiev 				       buf, n);
1270c33407a8SVladimir Kondratiev }
1271c33407a8SVladimir Kondratiev 
1272c33407a8SVladimir Kondratiev static ssize_t wil_write_file_recovery(struct file *file,
1273c33407a8SVladimir Kondratiev 				       const char __user *buf_,
1274c33407a8SVladimir Kondratiev 				       size_t count, loff_t *ppos)
1275c33407a8SVladimir Kondratiev {
1276c33407a8SVladimir Kondratiev 	struct wil6210_priv *wil = file->private_data;
1277c33407a8SVladimir Kondratiev 	static const char run_command[] = "run";
1278c33407a8SVladimir Kondratiev 	char buf[sizeof(run_command) + 1]; /* to detect "runx" */
1279c33407a8SVladimir Kondratiev 	ssize_t rc;
1280c33407a8SVladimir Kondratiev 
1281c33407a8SVladimir Kondratiev 	if (wil->recovery_state != fw_recovery_pending) {
1282c33407a8SVladimir Kondratiev 		wil_err(wil, "No recovery pending\n");
1283c33407a8SVladimir Kondratiev 		return -EINVAL;
1284c33407a8SVladimir Kondratiev 	}
1285c33407a8SVladimir Kondratiev 
1286c33407a8SVladimir Kondratiev 	if (*ppos != 0) {
1287c33407a8SVladimir Kondratiev 		wil_err(wil, "Offset [%d]\n", (int)*ppos);
1288c33407a8SVladimir Kondratiev 		return -EINVAL;
1289c33407a8SVladimir Kondratiev 	}
1290c33407a8SVladimir Kondratiev 
1291c33407a8SVladimir Kondratiev 	if (count > sizeof(buf)) {
1292c33407a8SVladimir Kondratiev 		wil_err(wil, "Input too long, len = %d\n", (int)count);
1293c33407a8SVladimir Kondratiev 		return -EINVAL;
1294c33407a8SVladimir Kondratiev 	}
1295c33407a8SVladimir Kondratiev 
1296c33407a8SVladimir Kondratiev 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, buf_, count);
1297c33407a8SVladimir Kondratiev 	if (rc < 0)
1298c33407a8SVladimir Kondratiev 		return rc;
1299c33407a8SVladimir Kondratiev 
1300c33407a8SVladimir Kondratiev 	buf[rc] = '\0';
1301c33407a8SVladimir Kondratiev 	if (0 == strcmp(buf, run_command))
1302c33407a8SVladimir Kondratiev 		wil_set_recovery_state(wil, fw_recovery_running);
1303c33407a8SVladimir Kondratiev 	else
1304c33407a8SVladimir Kondratiev 		wil_err(wil, "Bad recovery command \"%s\"\n", buf);
1305c33407a8SVladimir Kondratiev 
1306c33407a8SVladimir Kondratiev 	return rc;
1307c33407a8SVladimir Kondratiev }
1308c33407a8SVladimir Kondratiev 
1309c33407a8SVladimir Kondratiev static const struct file_operations fops_recovery = {
1310c33407a8SVladimir Kondratiev 	.read = wil_read_file_recovery,
1311c33407a8SVladimir Kondratiev 	.write = wil_write_file_recovery,
1312c33407a8SVladimir Kondratiev 	.open  = simple_open,
1313c33407a8SVladimir Kondratiev };
1314c33407a8SVladimir Kondratiev 
13153df2cd36SVladimir Kondratiev /*---------Station matrix------------*/
1316b4490f42SVladimir Kondratiev static void wil_print_rxtid(struct seq_file *s, struct wil_tid_ampdu_rx *r)
1317b4490f42SVladimir Kondratiev {
1318b4490f42SVladimir Kondratiev 	int i;
1319b4490f42SVladimir Kondratiev 	u16 index = ((r->head_seq_num - r->ssn) & 0xfff) % r->buf_size;
132091a8edccSVladimir Kondratiev 	unsigned long long drop_dup = r->drop_dup, drop_old = r->drop_old;
13218fe59627SVladimir Kondratiev 
132256637e7bSVladimir Kondratiev 	seq_printf(s, "([%2d] %3d TU) 0x%03x [", r->buf_size, r->timeout,
132356637e7bSVladimir Kondratiev 		   r->head_seq_num);
1324b4490f42SVladimir Kondratiev 	for (i = 0; i < r->buf_size; i++) {
1325b4490f42SVladimir Kondratiev 		if (i == index)
1326b4490f42SVladimir Kondratiev 			seq_printf(s, "%c", r->reorder_buf[i] ? 'O' : '|');
1327b4490f42SVladimir Kondratiev 		else
1328b4490f42SVladimir Kondratiev 			seq_printf(s, "%c", r->reorder_buf[i] ? '*' : '_');
1329b4490f42SVladimir Kondratiev 	}
133091a8edccSVladimir Kondratiev 	seq_printf(s,
133191a8edccSVladimir Kondratiev 		   "] total %llu drop %llu (dup %llu + old %llu) last 0x%03x\n",
133291a8edccSVladimir Kondratiev 		   r->total, drop_dup + drop_old, drop_dup, drop_old,
133391a8edccSVladimir Kondratiev 		   r->ssn_last_drop);
1334b4490f42SVladimir Kondratiev }
13353df2cd36SVladimir Kondratiev 
13363df2cd36SVladimir Kondratiev static int wil_sta_debugfs_show(struct seq_file *s, void *data)
1337bd33273bSVladimir Kondratiev __acquires(&p->tid_rx_lock) __releases(&p->tid_rx_lock)
13383df2cd36SVladimir Kondratiev {
13393df2cd36SVladimir Kondratiev 	struct wil6210_priv *wil = s->private;
1340c4a110d8SVladimir Kondratiev 	int i, tid, mcs;
13413df2cd36SVladimir Kondratiev 
13423df2cd36SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
13433df2cd36SVladimir Kondratiev 		struct wil_sta_info *p = &wil->sta[i];
13443df2cd36SVladimir Kondratiev 		char *status = "unknown";
13458fe59627SVladimir Kondratiev 
13463df2cd36SVladimir Kondratiev 		switch (p->status) {
13473df2cd36SVladimir Kondratiev 		case wil_sta_unused:
13483df2cd36SVladimir Kondratiev 			status = "unused   ";
13493df2cd36SVladimir Kondratiev 			break;
13503df2cd36SVladimir Kondratiev 		case wil_sta_conn_pending:
13513df2cd36SVladimir Kondratiev 			status = "pending  ";
13523df2cd36SVladimir Kondratiev 			break;
13533df2cd36SVladimir Kondratiev 		case wil_sta_connected:
13543df2cd36SVladimir Kondratiev 			status = "connected";
13553df2cd36SVladimir Kondratiev 			break;
13563df2cd36SVladimir Kondratiev 		}
1357230d8442SVladimir Kondratiev 		seq_printf(s, "[%d] %pM %s\n", i, p->addr, status);
1358b4490f42SVladimir Kondratiev 
1359b4490f42SVladimir Kondratiev 		if (p->status == wil_sta_connected) {
1360bd33273bSVladimir Kondratiev 			spin_lock_bh(&p->tid_rx_lock);
1361b4490f42SVladimir Kondratiev 			for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1362b4490f42SVladimir Kondratiev 				struct wil_tid_ampdu_rx *r = p->tid_rx[tid];
13638fe59627SVladimir Kondratiev 
1364b4490f42SVladimir Kondratiev 				if (r) {
1365b4490f42SVladimir Kondratiev 					seq_printf(s, "[%2d] ", tid);
1366b4490f42SVladimir Kondratiev 					wil_print_rxtid(s, r);
1367b4490f42SVladimir Kondratiev 				}
1368b4490f42SVladimir Kondratiev 			}
1369bd33273bSVladimir Kondratiev 			spin_unlock_bh(&p->tid_rx_lock);
13703b282bc6SVladimir Kondratiev 			seq_printf(s,
13713b282bc6SVladimir Kondratiev 				   "Rx invalid frame: non-data %lu, short %lu, large %lu\n",
13723b282bc6SVladimir Kondratiev 				   p->stats.rx_non_data_frame,
13733b282bc6SVladimir Kondratiev 				   p->stats.rx_short_frame,
13743b282bc6SVladimir Kondratiev 				   p->stats.rx_large_frame);
13753b282bc6SVladimir Kondratiev 
1376c4a110d8SVladimir Kondratiev 			seq_puts(s, "Rx/MCS:");
1377c4a110d8SVladimir Kondratiev 			for (mcs = 0; mcs < ARRAY_SIZE(p->stats.rx_per_mcs);
1378c4a110d8SVladimir Kondratiev 			     mcs++)
1379c4a110d8SVladimir Kondratiev 				seq_printf(s, " %lld",
1380c4a110d8SVladimir Kondratiev 					   p->stats.rx_per_mcs[mcs]);
1381c4a110d8SVladimir Kondratiev 			seq_puts(s, "\n");
1382b4490f42SVladimir Kondratiev 		}
13833df2cd36SVladimir Kondratiev 	}
13843df2cd36SVladimir Kondratiev 
13853df2cd36SVladimir Kondratiev 	return 0;
13863df2cd36SVladimir Kondratiev }
13873df2cd36SVladimir Kondratiev 
13883df2cd36SVladimir Kondratiev static int wil_sta_seq_open(struct inode *inode, struct file *file)
13893df2cd36SVladimir Kondratiev {
13903df2cd36SVladimir Kondratiev 	return single_open(file, wil_sta_debugfs_show, inode->i_private);
13913df2cd36SVladimir Kondratiev }
13923df2cd36SVladimir Kondratiev 
13933df2cd36SVladimir Kondratiev static const struct file_operations fops_sta = {
13943df2cd36SVladimir Kondratiev 	.open		= wil_sta_seq_open,
13953df2cd36SVladimir Kondratiev 	.release	= single_release,
13963df2cd36SVladimir Kondratiev 	.read		= seq_read,
13973df2cd36SVladimir Kondratiev 	.llseek		= seq_lseek,
13983df2cd36SVladimir Kondratiev };
13993df2cd36SVladimir Kondratiev 
14002be7d22fSVladimir Kondratiev /*----------------*/
1401b541d0a0SVladimir Kondratiev static void wil6210_debugfs_init_blobs(struct wil6210_priv *wil,
1402b541d0a0SVladimir Kondratiev 				       struct dentry *dbg)
1403b541d0a0SVladimir Kondratiev {
1404b541d0a0SVladimir Kondratiev 	int i;
1405b541d0a0SVladimir Kondratiev 	char name[32];
1406b541d0a0SVladimir Kondratiev 
1407b541d0a0SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
1408b541d0a0SVladimir Kondratiev 		struct debugfs_blob_wrapper *blob = &wil->blobs[i];
1409b541d0a0SVladimir Kondratiev 		const struct fw_map *map = &fw_mapping[i];
1410b541d0a0SVladimir Kondratiev 
1411b541d0a0SVladimir Kondratiev 		if (!map->name)
1412b541d0a0SVladimir Kondratiev 			continue;
1413b541d0a0SVladimir Kondratiev 
1414b541d0a0SVladimir Kondratiev 		blob->data = (void * __force)wil->csr + HOSTADDR(map->host);
1415b541d0a0SVladimir Kondratiev 		blob->size = map->to - map->from;
1416b541d0a0SVladimir Kondratiev 		snprintf(name, sizeof(name), "blob_%s", map->name);
1417b541d0a0SVladimir Kondratiev 		wil_debugfs_create_ioblob(name, S_IRUGO, dbg, blob);
1418b541d0a0SVladimir Kondratiev 	}
1419b541d0a0SVladimir Kondratiev }
1420b541d0a0SVladimir Kondratiev 
1421b7cde470SVladimir Kondratiev /* misc files */
1422b7cde470SVladimir Kondratiev static const struct {
1423b7cde470SVladimir Kondratiev 	const char *name;
1424b7cde470SVladimir Kondratiev 	umode_t mode;
1425b7cde470SVladimir Kondratiev 	const struct file_operations *fops;
1426b7cde470SVladimir Kondratiev } dbg_files[] = {
1427b7cde470SVladimir Kondratiev 	{"mbox",	S_IRUGO,		&fops_mbox},
1428b7cde470SVladimir Kondratiev 	{"vrings",	S_IRUGO,		&fops_vring},
1429b7cde470SVladimir Kondratiev 	{"stations",	S_IRUGO,		&fops_sta},
1430b7cde470SVladimir Kondratiev 	{"desc",	S_IRUGO,		&fops_txdesc},
1431b7cde470SVladimir Kondratiev 	{"bf",		S_IRUGO,		&fops_bf},
1432b7cde470SVladimir Kondratiev 	{"ssid",	S_IRUGO | S_IWUSR,	&fops_ssid},
1433b7cde470SVladimir Kondratiev 	{"mem_val",	S_IRUGO,		&fops_memread},
1434b7cde470SVladimir Kondratiev 	{"reset",		  S_IWUSR,	&fops_reset},
1435b7cde470SVladimir Kondratiev 	{"rxon",		  S_IWUSR,	&fops_rxon},
1436b7cde470SVladimir Kondratiev 	{"tx_mgmt",		  S_IWUSR,	&fops_txmgmt},
1437b7cde470SVladimir Kondratiev 	{"wmi_send",		  S_IWUSR,	&fops_wmi},
143849cb5dfbSVladimir Kondratiev 	{"back",	S_IRUGO | S_IWUSR,	&fops_back},
1439dc16427bSVladimir Kondratiev 	{"pmccfg",	S_IRUGO | S_IWUSR,	&fops_pmccfg},
1440dc16427bSVladimir Kondratiev 	{"pmcdata",	S_IRUGO,		&fops_pmcdata},
1441b7cde470SVladimir Kondratiev 	{"temp",	S_IRUGO,		&fops_temp},
1442b7cde470SVladimir Kondratiev 	{"freq",	S_IRUGO,		&fops_freq},
1443b7cde470SVladimir Kondratiev 	{"link",	S_IRUGO,		&fops_link},
1444b7cde470SVladimir Kondratiev 	{"info",	S_IRUGO,		&fops_info},
1445c33407a8SVladimir Kondratiev 	{"recovery",	S_IRUGO | S_IWUSR,	&fops_recovery},
1446b7cde470SVladimir Kondratiev };
1447b7cde470SVladimir Kondratiev 
1448b7cde470SVladimir Kondratiev static void wil6210_debugfs_init_files(struct wil6210_priv *wil,
1449b7cde470SVladimir Kondratiev 				       struct dentry *dbg)
1450b7cde470SVladimir Kondratiev {
1451b7cde470SVladimir Kondratiev 	int i;
1452b7cde470SVladimir Kondratiev 
1453b7cde470SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(dbg_files); i++)
1454b7cde470SVladimir Kondratiev 		debugfs_create_file(dbg_files[i].name, dbg_files[i].mode, dbg,
1455b7cde470SVladimir Kondratiev 				    wil, dbg_files[i].fops);
1456b7cde470SVladimir Kondratiev }
1457b7cde470SVladimir Kondratiev 
1458b7cde470SVladimir Kondratiev /* interrupt control blocks */
1459b7cde470SVladimir Kondratiev static const struct {
1460b7cde470SVladimir Kondratiev 	const char *name;
1461b7cde470SVladimir Kondratiev 	u32 icr_off;
1462b7cde470SVladimir Kondratiev } dbg_icr[] = {
1463b7cde470SVladimir Kondratiev 	{"USER_ICR",		HOSTADDR(RGF_USER_USER_ICR)},
1464b7cde470SVladimir Kondratiev 	{"DMA_EP_TX_ICR",	HOSTADDR(RGF_DMA_EP_TX_ICR)},
1465b7cde470SVladimir Kondratiev 	{"DMA_EP_RX_ICR",	HOSTADDR(RGF_DMA_EP_RX_ICR)},
1466b7cde470SVladimir Kondratiev 	{"DMA_EP_MISC_ICR",	HOSTADDR(RGF_DMA_EP_MISC_ICR)},
1467b7cde470SVladimir Kondratiev };
1468b7cde470SVladimir Kondratiev 
1469b7cde470SVladimir Kondratiev static void wil6210_debugfs_init_isr(struct wil6210_priv *wil,
1470b7cde470SVladimir Kondratiev 				     struct dentry *dbg)
1471b7cde470SVladimir Kondratiev {
1472b7cde470SVladimir Kondratiev 	int i;
1473b7cde470SVladimir Kondratiev 
1474b7cde470SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(dbg_icr); i++)
1475b7cde470SVladimir Kondratiev 		wil6210_debugfs_create_ISR(wil, dbg_icr[i].name, dbg,
1476b7cde470SVladimir Kondratiev 					   dbg_icr[i].icr_off);
1477b7cde470SVladimir Kondratiev }
1478b7cde470SVladimir Kondratiev 
1479b7cde470SVladimir Kondratiev #define WIL_FIELD(name, mode, type) { __stringify(name), mode, \
1480b7cde470SVladimir Kondratiev 	offsetof(struct wil6210_priv, name), type}
1481b7cde470SVladimir Kondratiev 
1482b7cde470SVladimir Kondratiev /* fields in struct wil6210_priv */
1483b7cde470SVladimir Kondratiev static const struct dbg_off dbg_wil_off[] = {
1484774974e5SVladimir Kondratiev 	WIL_FIELD(privacy,	S_IRUGO,		doff_u32),
14859419b6a2SVladimir Kondratiev 	WIL_FIELD(status[0],	S_IRUGO | S_IWUSR,	doff_ulong),
1486b7cde470SVladimir Kondratiev 	WIL_FIELD(fw_version,	S_IRUGO,		doff_u32),
1487b7cde470SVladimir Kondratiev 	WIL_FIELD(hw_version,	S_IRUGO,		doff_x32),
1488c33407a8SVladimir Kondratiev 	WIL_FIELD(recovery_count, S_IRUGO,		doff_u32),
148902beaf1aSVladimir Kondratiev 	WIL_FIELD(ap_isolate,	S_IRUGO,		doff_u32),
1490b7cde470SVladimir Kondratiev 	{},
1491b7cde470SVladimir Kondratiev };
1492b7cde470SVladimir Kondratiev 
1493b7cde470SVladimir Kondratiev static const struct dbg_off dbg_wil_regs[] = {
1494b7cde470SVladimir Kondratiev 	{"RGF_MAC_MTRL_COUNTER_0", S_IRUGO, HOSTADDR(RGF_MAC_MTRL_COUNTER_0),
1495b7cde470SVladimir Kondratiev 		doff_io32},
1496b7cde470SVladimir Kondratiev 	{"RGF_USER_USAGE_1", S_IRUGO, HOSTADDR(RGF_USER_USAGE_1), doff_io32},
1497b7cde470SVladimir Kondratiev 	{},
1498b7cde470SVladimir Kondratiev };
1499b7cde470SVladimir Kondratiev 
1500b7cde470SVladimir Kondratiev /* static parameters */
1501b7cde470SVladimir Kondratiev static const struct dbg_off dbg_statics[] = {
1502b7cde470SVladimir Kondratiev 	{"desc_index",	S_IRUGO | S_IWUSR, (ulong)&dbg_txdesc_index, doff_u32},
1503b7cde470SVladimir Kondratiev 	{"vring_index",	S_IRUGO | S_IWUSR, (ulong)&dbg_vring_index, doff_u32},
1504b7cde470SVladimir Kondratiev 	{"mem_addr",	S_IRUGO | S_IWUSR, (ulong)&mem_addr, doff_u32},
15050436fd9aSVladimir Shulman 	{"vring_idle_trsh", S_IRUGO | S_IWUSR, (ulong)&vring_idle_trsh,
15060436fd9aSVladimir Shulman 	 doff_u32},
1507b7cde470SVladimir Kondratiev 	{},
1508b7cde470SVladimir Kondratiev };
1509b7cde470SVladimir Kondratiev 
15102be7d22fSVladimir Kondratiev int wil6210_debugfs_init(struct wil6210_priv *wil)
15112be7d22fSVladimir Kondratiev {
15122be7d22fSVladimir Kondratiev 	struct dentry *dbg = wil->debug = debugfs_create_dir(WIL_NAME,
15132be7d22fSVladimir Kondratiev 			wil_to_wiphy(wil)->debugfsdir);
15142be7d22fSVladimir Kondratiev 
15152be7d22fSVladimir Kondratiev 	if (IS_ERR_OR_NULL(dbg))
15162be7d22fSVladimir Kondratiev 		return -ENODEV;
15172be7d22fSVladimir Kondratiev 
1518dc16427bSVladimir Kondratiev 	wil_pmc_init(wil);
1519dc16427bSVladimir Kondratiev 
1520b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_files(wil, dbg);
1521b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_isr(wil, dbg);
1522b541d0a0SVladimir Kondratiev 	wil6210_debugfs_init_blobs(wil, dbg);
1523b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dbg, wil, dbg_wil_off);
1524b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dbg, (void * __force)wil->csr,
1525b7cde470SVladimir Kondratiev 				    dbg_wil_regs);
1526b7cde470SVladimir Kondratiev 	wil6210_debugfs_init_offset(wil, dbg, NULL, dbg_statics);
1527b7cde470SVladimir Kondratiev 
1528b7cde470SVladimir Kondratiev 	wil6210_debugfs_create_pseudo_ISR(wil, dbg);
1529b7cde470SVladimir Kondratiev 
1530b7cde470SVladimir Kondratiev 	wil6210_debugfs_create_ITR_CNT(wil, dbg);
15312be7d22fSVladimir Kondratiev 
15322be7d22fSVladimir Kondratiev 	return 0;
15332be7d22fSVladimir Kondratiev }
15342be7d22fSVladimir Kondratiev 
15352be7d22fSVladimir Kondratiev void wil6210_debugfs_remove(struct wil6210_priv *wil)
15362be7d22fSVladimir Kondratiev {
15372be7d22fSVladimir Kondratiev 	debugfs_remove_recursive(wil->debug);
15382be7d22fSVladimir Kondratiev 	wil->debug = NULL;
1539dc16427bSVladimir Kondratiev 
1540dc16427bSVladimir Kondratiev 	/* free pmc memory without sending command to fw, as it will
1541dc16427bSVladimir Kondratiev 	 * be reset on the way down anyway
1542dc16427bSVladimir Kondratiev 	 */
1543dc16427bSVladimir Kondratiev 	wil_pmc_free(wil, false);
15442be7d22fSVladimir Kondratiev }
1545