1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/pci.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/power_supply.h>
24 #include "wil6210.h"
25 #include "wmi.h"
26 #include "txrx.h"
27 #include "pmc.h"
28 
29 /* Nasty hack. Better have per device instances */
30 static u32 mem_addr;
31 static u32 dbg_txdesc_index;
32 static u32 dbg_ring_index; /* 24+ for Rx, 0..23 for Tx */
33 static u32 dbg_status_msg_index;
34 /* 0..wil->num_rx_status_rings-1 for Rx, wil->tx_sring_idx for Tx */
35 static u32 dbg_sring_index;
36 
37 enum dbg_off_type {
38 	doff_u32 = 0,
39 	doff_x32 = 1,
40 	doff_ulong = 2,
41 	doff_io32 = 3,
42 	doff_u8 = 4
43 };
44 
45 /* offset to "wil" */
46 struct dbg_off {
47 	const char *name;
48 	umode_t mode;
49 	ulong off;
50 	enum dbg_off_type type;
51 };
52 
53 static void wil_print_desc_edma(struct seq_file *s, struct wil6210_priv *wil,
54 				struct wil_ring *ring,
55 				char _s, char _h, int idx)
56 {
57 	u8 num_of_descs;
58 	bool has_skb = false;
59 
60 	if (ring->is_rx) {
61 		struct wil_rx_enhanced_desc *rx_d =
62 			(struct wil_rx_enhanced_desc *)
63 			&ring->va[idx].rx.enhanced;
64 		u16 buff_id = le16_to_cpu(rx_d->mac.buff_id);
65 
66 		has_skb = wil->rx_buff_mgmt.buff_arr[buff_id].skb;
67 		seq_printf(s, "%c", (has_skb) ? _h : _s);
68 	} else {
69 		struct wil_tx_enhanced_desc *d =
70 			(struct wil_tx_enhanced_desc *)
71 			&ring->va[idx].tx.enhanced;
72 
73 		num_of_descs = (u8)d->mac.d[2];
74 		has_skb = ring->ctx[idx].skb;
75 		if (num_of_descs >= 1)
76 			seq_printf(s, "%c", ring->ctx[idx].skb ? _h : _s);
77 		else
78 			/* num_of_descs == 0, it's a frag in a list of descs */
79 			seq_printf(s, "%c", has_skb ? 'h' : _s);
80 	}
81 }
82 
83 static void wil_print_ring(struct seq_file *s, struct wil6210_priv *wil,
84 			   const char *name, struct wil_ring *ring,
85 			   char _s, char _h)
86 {
87 	void __iomem *x = wmi_addr(wil, ring->hwtail);
88 	u32 v;
89 
90 	seq_printf(s, "RING %s = {\n", name);
91 	seq_printf(s, "  pa     = %pad\n", &ring->pa);
92 	seq_printf(s, "  va     = 0x%p\n", ring->va);
93 	seq_printf(s, "  size   = %d\n", ring->size);
94 	if (wil->use_enhanced_dma_hw && ring->is_rx)
95 		seq_printf(s, "  swtail = %u\n", *ring->edma_rx_swtail.va);
96 	else
97 		seq_printf(s, "  swtail = %d\n", ring->swtail);
98 	seq_printf(s, "  swhead = %d\n", ring->swhead);
99 	seq_printf(s, "  hwtail = [0x%08x] -> ", ring->hwtail);
100 	if (x) {
101 		v = readl(x);
102 		seq_printf(s, "0x%08x = %d\n", v, v);
103 	} else {
104 		seq_puts(s, "???\n");
105 	}
106 
107 	if (ring->va && (ring->size <= (1 << WIL_RING_SIZE_ORDER_MAX))) {
108 		uint i;
109 
110 		for (i = 0; i < ring->size; i++) {
111 			if ((i % 128) == 0 && i != 0)
112 				seq_puts(s, "\n");
113 			if (wil->use_enhanced_dma_hw) {
114 				wil_print_desc_edma(s, wil, ring, _s, _h, i);
115 			} else {
116 				volatile struct vring_tx_desc *d =
117 					&ring->va[i].tx.legacy;
118 				seq_printf(s, "%c", (d->dma.status & BIT(0)) ?
119 					   _s : (ring->ctx[i].skb ? _h : 'h'));
120 			}
121 		}
122 		seq_puts(s, "\n");
123 	}
124 	seq_puts(s, "}\n");
125 }
126 
127 static int wil_ring_debugfs_show(struct seq_file *s, void *data)
128 {
129 	uint i;
130 	struct wil6210_priv *wil = s->private;
131 
132 	wil_print_ring(s, wil, "rx", &wil->ring_rx, 'S', '_');
133 
134 	for (i = 0; i < ARRAY_SIZE(wil->ring_tx); i++) {
135 		struct wil_ring *ring = &wil->ring_tx[i];
136 		struct wil_ring_tx_data *txdata = &wil->ring_tx_data[i];
137 
138 		if (ring->va) {
139 			int cid = wil->ring2cid_tid[i][0];
140 			int tid = wil->ring2cid_tid[i][1];
141 			u32 swhead = ring->swhead;
142 			u32 swtail = ring->swtail;
143 			int used = (ring->size + swhead - swtail)
144 				   % ring->size;
145 			int avail = ring->size - used - 1;
146 			char name[10];
147 			char sidle[10];
148 			/* performance monitoring */
149 			cycles_t now = get_cycles();
150 			uint64_t idle = txdata->idle * 100;
151 			uint64_t total = now - txdata->begin;
152 
153 			if (total != 0) {
154 				do_div(idle, total);
155 				snprintf(sidle, sizeof(sidle), "%3d%%",
156 					 (int)idle);
157 			} else {
158 				snprintf(sidle, sizeof(sidle), "N/A");
159 			}
160 			txdata->begin = now;
161 			txdata->idle = 0ULL;
162 
163 			snprintf(name, sizeof(name), "tx_%2d", i);
164 
165 			if (cid < WIL6210_MAX_CID)
166 				seq_printf(s,
167 					   "\n%pM CID %d TID %d 1x%s BACK([%u] %u TU A%s) [%3d|%3d] idle %s\n",
168 					   wil->sta[cid].addr, cid, tid,
169 					   txdata->dot1x_open ? "+" : "-",
170 					   txdata->agg_wsize,
171 					   txdata->agg_timeout,
172 					   txdata->agg_amsdu ? "+" : "-",
173 					   used, avail, sidle);
174 			else
175 				seq_printf(s,
176 					   "\nBroadcast 1x%s [%3d|%3d] idle %s\n",
177 					   txdata->dot1x_open ? "+" : "-",
178 					   used, avail, sidle);
179 
180 			wil_print_ring(s, wil, name, ring, '_', 'H');
181 		}
182 	}
183 
184 	return 0;
185 }
186 
187 static int wil_ring_seq_open(struct inode *inode, struct file *file)
188 {
189 	return single_open(file, wil_ring_debugfs_show, inode->i_private);
190 }
191 
192 static const struct file_operations fops_ring = {
193 	.open		= wil_ring_seq_open,
194 	.release	= single_release,
195 	.read		= seq_read,
196 	.llseek		= seq_lseek,
197 };
198 
199 static void wil_print_sring(struct seq_file *s, struct wil6210_priv *wil,
200 			    struct wil_status_ring *sring)
201 {
202 	void __iomem *x = wmi_addr(wil, sring->hwtail);
203 	int sring_idx = sring - wil->srings;
204 	u32 v;
205 
206 	seq_printf(s, "Status Ring %s [ %d ] = {\n",
207 		   sring->is_rx ? "RX" : "TX", sring_idx);
208 	seq_printf(s, "  pa     = %pad\n", &sring->pa);
209 	seq_printf(s, "  va     = 0x%pK\n", sring->va);
210 	seq_printf(s, "  size   = %d\n", sring->size);
211 	seq_printf(s, "  elem_size   = %zu\n", sring->elem_size);
212 	seq_printf(s, "  swhead = %d\n", sring->swhead);
213 	seq_printf(s, "  hwtail = [0x%08x] -> ", sring->hwtail);
214 	if (x) {
215 		v = readl_relaxed(x);
216 		seq_printf(s, "0x%08x = %d\n", v, v);
217 	} else {
218 		seq_puts(s, "???\n");
219 	}
220 	seq_printf(s, "  desc_rdy_pol   = %d\n", sring->desc_rdy_pol);
221 
222 	if (sring->va && (sring->size <= (1 << WIL_RING_SIZE_ORDER_MAX))) {
223 		uint i;
224 
225 		for (i = 0; i < sring->size; i++) {
226 			u32 *sdword_0 =
227 				(u32 *)(sring->va + (sring->elem_size * i));
228 
229 			if ((i % 128) == 0 && i != 0)
230 				seq_puts(s, "\n");
231 			if (i == sring->swhead)
232 				seq_printf(s, "%c", (*sdword_0 & BIT(31)) ?
233 					   'X' : 'x');
234 			else
235 				seq_printf(s, "%c", (*sdword_0 & BIT(31)) ?
236 					   '1' : '0');
237 		}
238 		seq_puts(s, "\n");
239 	}
240 	seq_puts(s, "}\n");
241 }
242 
243 static int wil_srings_debugfs_show(struct seq_file *s, void *data)
244 {
245 	struct wil6210_priv *wil = s->private;
246 	int i = 0;
247 
248 	for (i = 0; i < WIL6210_MAX_STATUS_RINGS; i++)
249 		if (wil->srings[i].va)
250 			wil_print_sring(s, wil, &wil->srings[i]);
251 
252 	return 0;
253 }
254 
255 static int wil_srings_seq_open(struct inode *inode, struct file *file)
256 {
257 	return single_open(file, wil_srings_debugfs_show, inode->i_private);
258 }
259 
260 static const struct file_operations fops_srings = {
261 	.open		= wil_srings_seq_open,
262 	.release	= single_release,
263 	.read		= seq_read,
264 	.llseek		= seq_lseek,
265 };
266 
267 static void wil_seq_hexdump(struct seq_file *s, void *p, int len,
268 			    const char *prefix)
269 {
270 	seq_hex_dump(s, prefix, DUMP_PREFIX_NONE, 16, 1, p, len, false);
271 }
272 
273 static void wil_print_mbox_ring(struct seq_file *s, const char *prefix,
274 				void __iomem *off)
275 {
276 	struct wil6210_priv *wil = s->private;
277 	struct wil6210_mbox_ring r;
278 	int rsize;
279 	uint i;
280 
281 	wil_halp_vote(wil);
282 
283 	wil_memcpy_fromio_32(&r, off, sizeof(r));
284 	wil_mbox_ring_le2cpus(&r);
285 	/*
286 	 * we just read memory block from NIC. This memory may be
287 	 * garbage. Check validity before using it.
288 	 */
289 	rsize = r.size / sizeof(struct wil6210_mbox_ring_desc);
290 
291 	seq_printf(s, "ring %s = {\n", prefix);
292 	seq_printf(s, "  base = 0x%08x\n", r.base);
293 	seq_printf(s, "  size = 0x%04x bytes -> %d entries\n", r.size, rsize);
294 	seq_printf(s, "  tail = 0x%08x\n", r.tail);
295 	seq_printf(s, "  head = 0x%08x\n", r.head);
296 	seq_printf(s, "  entry size = %d\n", r.entry_size);
297 
298 	if (r.size % sizeof(struct wil6210_mbox_ring_desc)) {
299 		seq_printf(s, "  ??? size is not multiple of %zd, garbage?\n",
300 			   sizeof(struct wil6210_mbox_ring_desc));
301 		goto out;
302 	}
303 
304 	if (!wmi_addr(wil, r.base) ||
305 	    !wmi_addr(wil, r.tail) ||
306 	    !wmi_addr(wil, r.head)) {
307 		seq_puts(s, "  ??? pointers are garbage?\n");
308 		goto out;
309 	}
310 
311 	for (i = 0; i < rsize; i++) {
312 		struct wil6210_mbox_ring_desc d;
313 		struct wil6210_mbox_hdr hdr;
314 		size_t delta = i * sizeof(d);
315 		void __iomem *x = wil->csr + HOSTADDR(r.base) + delta;
316 
317 		wil_memcpy_fromio_32(&d, x, sizeof(d));
318 
319 		seq_printf(s, "  [%2x] %s %s%s 0x%08x", i,
320 			   d.sync ? "F" : "E",
321 			   (r.tail - r.base == delta) ? "t" : " ",
322 			   (r.head - r.base == delta) ? "h" : " ",
323 			   le32_to_cpu(d.addr));
324 		if (0 == wmi_read_hdr(wil, d.addr, &hdr)) {
325 			u16 len = le16_to_cpu(hdr.len);
326 
327 			seq_printf(s, " -> %04x %04x %04x %02x\n",
328 				   le16_to_cpu(hdr.seq), len,
329 				   le16_to_cpu(hdr.type), hdr.flags);
330 			if (len <= MAX_MBOXITEM_SIZE) {
331 				unsigned char databuf[MAX_MBOXITEM_SIZE];
332 				void __iomem *src = wmi_buffer(wil, d.addr) +
333 					sizeof(struct wil6210_mbox_hdr);
334 				/*
335 				 * No need to check @src for validity -
336 				 * we already validated @d.addr while
337 				 * reading header
338 				 */
339 				wil_memcpy_fromio_32(databuf, src, len);
340 				wil_seq_hexdump(s, databuf, len, "      : ");
341 			}
342 		} else {
343 			seq_puts(s, "\n");
344 		}
345 	}
346  out:
347 	seq_puts(s, "}\n");
348 	wil_halp_unvote(wil);
349 }
350 
351 static int wil_mbox_debugfs_show(struct seq_file *s, void *data)
352 {
353 	struct wil6210_priv *wil = s->private;
354 	int ret;
355 
356 	ret = wil_pm_runtime_get(wil);
357 	if (ret < 0)
358 		return ret;
359 
360 	wil_print_mbox_ring(s, "tx", wil->csr + HOST_MBOX +
361 		       offsetof(struct wil6210_mbox_ctl, tx));
362 	wil_print_mbox_ring(s, "rx", wil->csr + HOST_MBOX +
363 		       offsetof(struct wil6210_mbox_ctl, rx));
364 
365 	wil_pm_runtime_put(wil);
366 
367 	return 0;
368 }
369 
370 static int wil_mbox_seq_open(struct inode *inode, struct file *file)
371 {
372 	return single_open(file, wil_mbox_debugfs_show, inode->i_private);
373 }
374 
375 static const struct file_operations fops_mbox = {
376 	.open		= wil_mbox_seq_open,
377 	.release	= single_release,
378 	.read		= seq_read,
379 	.llseek		= seq_lseek,
380 };
381 
382 static int wil_debugfs_iomem_x32_set(void *data, u64 val)
383 {
384 	struct wil_debugfs_iomem_data *d = (struct
385 					    wil_debugfs_iomem_data *)data;
386 	struct wil6210_priv *wil = d->wil;
387 	int ret;
388 
389 	ret = wil_pm_runtime_get(wil);
390 	if (ret < 0)
391 		return ret;
392 
393 	writel(val, (void __iomem *)d->offset);
394 	wmb(); /* make sure write propagated to HW */
395 
396 	wil_pm_runtime_put(wil);
397 
398 	return 0;
399 }
400 
401 static int wil_debugfs_iomem_x32_get(void *data, u64 *val)
402 {
403 	struct wil_debugfs_iomem_data *d = (struct
404 					    wil_debugfs_iomem_data *)data;
405 	struct wil6210_priv *wil = d->wil;
406 	int ret;
407 
408 	ret = wil_pm_runtime_get(wil);
409 	if (ret < 0)
410 		return ret;
411 
412 	*val = readl((void __iomem *)d->offset);
413 
414 	wil_pm_runtime_put(wil);
415 
416 	return 0;
417 }
418 
419 DEFINE_DEBUGFS_ATTRIBUTE(fops_iomem_x32, wil_debugfs_iomem_x32_get,
420 			 wil_debugfs_iomem_x32_set, "0x%08llx\n");
421 
422 static struct dentry *wil_debugfs_create_iomem_x32(const char *name,
423 						   umode_t mode,
424 						   struct dentry *parent,
425 						   void *value,
426 						   struct wil6210_priv *wil)
427 {
428 	struct dentry *file;
429 	struct wil_debugfs_iomem_data *data = &wil->dbg_data.data_arr[
430 					      wil->dbg_data.iomem_data_count];
431 
432 	data->wil = wil;
433 	data->offset = value;
434 
435 	file = debugfs_create_file_unsafe(name, mode, parent, data,
436 					  &fops_iomem_x32);
437 	if (!IS_ERR_OR_NULL(file))
438 		wil->dbg_data.iomem_data_count++;
439 
440 	return file;
441 }
442 
443 static int wil_debugfs_ulong_set(void *data, u64 val)
444 {
445 	*(ulong *)data = val;
446 	return 0;
447 }
448 
449 static int wil_debugfs_ulong_get(void *data, u64 *val)
450 {
451 	*val = *(ulong *)data;
452 	return 0;
453 }
454 
455 DEFINE_DEBUGFS_ATTRIBUTE(wil_fops_ulong, wil_debugfs_ulong_get,
456 			 wil_debugfs_ulong_set, "0x%llx\n");
457 
458 static struct dentry *wil_debugfs_create_ulong(const char *name, umode_t mode,
459 					       struct dentry *parent,
460 					       ulong *value)
461 {
462 	return debugfs_create_file_unsafe(name, mode, parent, value,
463 					  &wil_fops_ulong);
464 }
465 
466 /**
467  * wil6210_debugfs_init_offset - create set of debugfs files
468  * @wil - driver's context, used for printing
469  * @dbg - directory on the debugfs, where files will be created
470  * @base - base address used in address calculation
471  * @tbl - table with file descriptions. Should be terminated with empty element.
472  *
473  * Creates files accordingly to the @tbl.
474  */
475 static void wil6210_debugfs_init_offset(struct wil6210_priv *wil,
476 					struct dentry *dbg, void *base,
477 					const struct dbg_off * const tbl)
478 {
479 	int i;
480 
481 	for (i = 0; tbl[i].name; i++) {
482 		struct dentry *f;
483 
484 		switch (tbl[i].type) {
485 		case doff_u32:
486 			f = debugfs_create_u32(tbl[i].name, tbl[i].mode, dbg,
487 					       base + tbl[i].off);
488 			break;
489 		case doff_x32:
490 			f = debugfs_create_x32(tbl[i].name, tbl[i].mode, dbg,
491 					       base + tbl[i].off);
492 			break;
493 		case doff_ulong:
494 			f = wil_debugfs_create_ulong(tbl[i].name, tbl[i].mode,
495 						     dbg, base + tbl[i].off);
496 			break;
497 		case doff_io32:
498 			f = wil_debugfs_create_iomem_x32(tbl[i].name,
499 							 tbl[i].mode, dbg,
500 							 base + tbl[i].off,
501 							 wil);
502 			break;
503 		case doff_u8:
504 			f = debugfs_create_u8(tbl[i].name, tbl[i].mode, dbg,
505 					      base + tbl[i].off);
506 			break;
507 		default:
508 			f = ERR_PTR(-EINVAL);
509 		}
510 		if (IS_ERR_OR_NULL(f))
511 			wil_err(wil, "Create file \"%s\": err %ld\n",
512 				tbl[i].name, PTR_ERR(f));
513 	}
514 }
515 
516 static const struct dbg_off isr_off[] = {
517 	{"ICC", 0644, offsetof(struct RGF_ICR, ICC), doff_io32},
518 	{"ICR", 0644, offsetof(struct RGF_ICR, ICR), doff_io32},
519 	{"ICM", 0644, offsetof(struct RGF_ICR, ICM), doff_io32},
520 	{"ICS",	0244, offsetof(struct RGF_ICR, ICS), doff_io32},
521 	{"IMV", 0644, offsetof(struct RGF_ICR, IMV), doff_io32},
522 	{"IMS",	0244, offsetof(struct RGF_ICR, IMS), doff_io32},
523 	{"IMC",	0244, offsetof(struct RGF_ICR, IMC), doff_io32},
524 	{},
525 };
526 
527 static int wil6210_debugfs_create_ISR(struct wil6210_priv *wil,
528 				      const char *name,
529 				      struct dentry *parent, u32 off)
530 {
531 	struct dentry *d = debugfs_create_dir(name, parent);
532 
533 	if (IS_ERR_OR_NULL(d))
534 		return -ENODEV;
535 
536 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr + off,
537 				    isr_off);
538 
539 	return 0;
540 }
541 
542 static const struct dbg_off pseudo_isr_off[] = {
543 	{"CAUSE",   0444, HOSTADDR(RGF_DMA_PSEUDO_CAUSE), doff_io32},
544 	{"MASK_SW", 0444, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW), doff_io32},
545 	{"MASK_FW", 0444, HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_FW), doff_io32},
546 	{},
547 };
548 
549 static int wil6210_debugfs_create_pseudo_ISR(struct wil6210_priv *wil,
550 					     struct dentry *parent)
551 {
552 	struct dentry *d = debugfs_create_dir("PSEUDO_ISR", parent);
553 
554 	if (IS_ERR_OR_NULL(d))
555 		return -ENODEV;
556 
557 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
558 				    pseudo_isr_off);
559 
560 	return 0;
561 }
562 
563 static const struct dbg_off lgc_itr_cnt_off[] = {
564 	{"TRSH", 0644, HOSTADDR(RGF_DMA_ITR_CNT_TRSH), doff_io32},
565 	{"DATA", 0644, HOSTADDR(RGF_DMA_ITR_CNT_DATA), doff_io32},
566 	{"CTL",  0644, HOSTADDR(RGF_DMA_ITR_CNT_CRL), doff_io32},
567 	{},
568 };
569 
570 static const struct dbg_off tx_itr_cnt_off[] = {
571 	{"TRSH", 0644, HOSTADDR(RGF_DMA_ITR_TX_CNT_TRSH),
572 	 doff_io32},
573 	{"DATA", 0644, HOSTADDR(RGF_DMA_ITR_TX_CNT_DATA),
574 	 doff_io32},
575 	{"CTL",  0644, HOSTADDR(RGF_DMA_ITR_TX_CNT_CTL),
576 	 doff_io32},
577 	{"IDL_TRSH", 0644, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_TRSH),
578 	 doff_io32},
579 	{"IDL_DATA", 0644, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_DATA),
580 	 doff_io32},
581 	{"IDL_CTL",  0644, HOSTADDR(RGF_DMA_ITR_TX_IDL_CNT_CTL),
582 	 doff_io32},
583 	{},
584 };
585 
586 static const struct dbg_off rx_itr_cnt_off[] = {
587 	{"TRSH", 0644, HOSTADDR(RGF_DMA_ITR_RX_CNT_TRSH),
588 	 doff_io32},
589 	{"DATA", 0644, HOSTADDR(RGF_DMA_ITR_RX_CNT_DATA),
590 	 doff_io32},
591 	{"CTL",  0644, HOSTADDR(RGF_DMA_ITR_RX_CNT_CTL),
592 	 doff_io32},
593 	{"IDL_TRSH", 0644, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_TRSH),
594 	 doff_io32},
595 	{"IDL_DATA", 0644, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_DATA),
596 	 doff_io32},
597 	{"IDL_CTL",  0644, HOSTADDR(RGF_DMA_ITR_RX_IDL_CNT_CTL),
598 	 doff_io32},
599 	{},
600 };
601 
602 static int wil6210_debugfs_create_ITR_CNT(struct wil6210_priv *wil,
603 					  struct dentry *parent)
604 {
605 	struct dentry *d, *dtx, *drx;
606 
607 	d = debugfs_create_dir("ITR_CNT", parent);
608 	if (IS_ERR_OR_NULL(d))
609 		return -ENODEV;
610 
611 	dtx = debugfs_create_dir("TX", d);
612 	drx = debugfs_create_dir("RX", d);
613 	if (IS_ERR_OR_NULL(dtx) || IS_ERR_OR_NULL(drx))
614 		return -ENODEV;
615 
616 	wil6210_debugfs_init_offset(wil, d, (void * __force)wil->csr,
617 				    lgc_itr_cnt_off);
618 
619 	wil6210_debugfs_init_offset(wil, dtx, (void * __force)wil->csr,
620 				    tx_itr_cnt_off);
621 
622 	wil6210_debugfs_init_offset(wil, drx, (void * __force)wil->csr,
623 				    rx_itr_cnt_off);
624 	return 0;
625 }
626 
627 static int wil_memread_debugfs_show(struct seq_file *s, void *data)
628 {
629 	struct wil6210_priv *wil = s->private;
630 	void __iomem *a;
631 	int ret;
632 
633 	ret = wil_pm_runtime_get(wil);
634 	if (ret < 0)
635 		return ret;
636 
637 	a = wmi_buffer(wil, cpu_to_le32(mem_addr));
638 
639 	if (a)
640 		seq_printf(s, "[0x%08x] = 0x%08x\n", mem_addr, readl(a));
641 	else
642 		seq_printf(s, "[0x%08x] = INVALID\n", mem_addr);
643 
644 	wil_pm_runtime_put(wil);
645 
646 	return 0;
647 }
648 
649 static int wil_memread_seq_open(struct inode *inode, struct file *file)
650 {
651 	return single_open(file, wil_memread_debugfs_show, inode->i_private);
652 }
653 
654 static const struct file_operations fops_memread = {
655 	.open		= wil_memread_seq_open,
656 	.release	= single_release,
657 	.read		= seq_read,
658 	.llseek		= seq_lseek,
659 };
660 
661 static ssize_t wil_read_file_ioblob(struct file *file, char __user *user_buf,
662 				    size_t count, loff_t *ppos)
663 {
664 	enum { max_count = 4096 };
665 	struct wil_blob_wrapper *wil_blob = file->private_data;
666 	struct wil6210_priv *wil = wil_blob->wil;
667 	loff_t pos = *ppos;
668 	size_t available = wil_blob->blob.size;
669 	void *buf;
670 	size_t ret;
671 	int rc;
672 
673 	if (test_bit(wil_status_suspending, wil_blob->wil->status) ||
674 	    test_bit(wil_status_suspended, wil_blob->wil->status))
675 		return 0;
676 
677 	if (pos < 0)
678 		return -EINVAL;
679 
680 	if (pos >= available || !count)
681 		return 0;
682 
683 	if (count > available - pos)
684 		count = available - pos;
685 	if (count > max_count)
686 		count = max_count;
687 
688 	buf = kmalloc(count, GFP_KERNEL);
689 	if (!buf)
690 		return -ENOMEM;
691 
692 	rc = wil_pm_runtime_get(wil);
693 	if (rc < 0) {
694 		kfree(buf);
695 		return rc;
696 	}
697 
698 	wil_memcpy_fromio_32(buf, (const void __iomem *)
699 			     wil_blob->blob.data + pos, count);
700 
701 	ret = copy_to_user(user_buf, buf, count);
702 
703 	wil_pm_runtime_put(wil);
704 
705 	kfree(buf);
706 	if (ret == count)
707 		return -EFAULT;
708 
709 	count -= ret;
710 	*ppos = pos + count;
711 
712 	return count;
713 }
714 
715 static const struct file_operations fops_ioblob = {
716 	.read =		wil_read_file_ioblob,
717 	.open =		simple_open,
718 	.llseek =	default_llseek,
719 };
720 
721 static
722 struct dentry *wil_debugfs_create_ioblob(const char *name,
723 					 umode_t mode,
724 					 struct dentry *parent,
725 					 struct wil_blob_wrapper *wil_blob)
726 {
727 	return debugfs_create_file(name, mode, parent, wil_blob, &fops_ioblob);
728 }
729 
730 /*---write channel 1..4 to rxon for it, 0 to rxoff---*/
731 static ssize_t wil_write_file_rxon(struct file *file, const char __user *buf,
732 				   size_t len, loff_t *ppos)
733 {
734 	struct wil6210_priv *wil = file->private_data;
735 	int rc;
736 	long channel;
737 	bool on;
738 
739 	char *kbuf = memdup_user_nul(buf, len);
740 
741 	if (IS_ERR(kbuf))
742 		return PTR_ERR(kbuf);
743 	rc = kstrtol(kbuf, 0, &channel);
744 	kfree(kbuf);
745 	if (rc)
746 		return rc;
747 
748 	if ((channel < 0) || (channel > 4)) {
749 		wil_err(wil, "Invalid channel %ld\n", channel);
750 		return -EINVAL;
751 	}
752 	on = !!channel;
753 
754 	if (on) {
755 		rc = wmi_set_channel(wil, (int)channel);
756 		if (rc)
757 			return rc;
758 	}
759 
760 	rc = wmi_rxon(wil, on);
761 	if (rc)
762 		return rc;
763 
764 	return len;
765 }
766 
767 static const struct file_operations fops_rxon = {
768 	.write = wil_write_file_rxon,
769 	.open  = simple_open,
770 };
771 
772 /* block ack control, write:
773  * - "add <ringid> <agg_size> <timeout>" to trigger ADDBA
774  * - "del_tx <ringid> <reason>" to trigger DELBA for Tx side
775  * - "del_rx <CID> <TID> <reason>" to trigger DELBA for Rx side
776  */
777 static ssize_t wil_write_back(struct file *file, const char __user *buf,
778 			      size_t len, loff_t *ppos)
779 {
780 	struct wil6210_priv *wil = file->private_data;
781 	int rc;
782 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
783 	char cmd[9];
784 	int p1, p2, p3;
785 
786 	if (!kbuf)
787 		return -ENOMEM;
788 
789 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
790 	if (rc != len) {
791 		kfree(kbuf);
792 		return rc >= 0 ? -EIO : rc;
793 	}
794 
795 	kbuf[len] = '\0';
796 	rc = sscanf(kbuf, "%8s %d %d %d", cmd, &p1, &p2, &p3);
797 	kfree(kbuf);
798 
799 	if (rc < 0)
800 		return rc;
801 	if (rc < 2)
802 		return -EINVAL;
803 
804 	if ((strcmp(cmd, "add") == 0) ||
805 	    (strcmp(cmd, "del_tx") == 0)) {
806 		struct wil_ring_tx_data *txdata;
807 
808 		if (p1 < 0 || p1 >= WIL6210_MAX_TX_RINGS) {
809 			wil_err(wil, "BACK: invalid ring id %d\n", p1);
810 			return -EINVAL;
811 		}
812 		txdata = &wil->ring_tx_data[p1];
813 		if (strcmp(cmd, "add") == 0) {
814 			if (rc < 3) {
815 				wil_err(wil, "BACK: add require at least 2 params\n");
816 				return -EINVAL;
817 			}
818 			if (rc < 4)
819 				p3 = 0;
820 			wmi_addba(wil, txdata->mid, p1, p2, p3);
821 		} else {
822 			if (rc < 3)
823 				p2 = WLAN_REASON_QSTA_LEAVE_QBSS;
824 			wmi_delba_tx(wil, txdata->mid, p1, p2);
825 		}
826 	} else if (strcmp(cmd, "del_rx") == 0) {
827 		struct wil_sta_info *sta;
828 
829 		if (rc < 3) {
830 			wil_err(wil,
831 				"BACK: del_rx require at least 2 params\n");
832 			return -EINVAL;
833 		}
834 		if (p1 < 0 || p1 >= WIL6210_MAX_CID) {
835 			wil_err(wil, "BACK: invalid CID %d\n", p1);
836 			return -EINVAL;
837 		}
838 		if (rc < 4)
839 			p3 = WLAN_REASON_QSTA_LEAVE_QBSS;
840 		sta = &wil->sta[p1];
841 		wmi_delba_rx(wil, sta->mid, mk_cidxtid(p1, p2), p3);
842 	} else {
843 		wil_err(wil, "BACK: Unrecognized command \"%s\"\n", cmd);
844 		return -EINVAL;
845 	}
846 
847 	return len;
848 }
849 
850 static ssize_t wil_read_back(struct file *file, char __user *user_buf,
851 			     size_t count, loff_t *ppos)
852 {
853 	static const char text[] = "block ack control, write:\n"
854 	" - \"add <ringid> <agg_size> <timeout>\" to trigger ADDBA\n"
855 	"If missing, <timeout> defaults to 0\n"
856 	" - \"del_tx <ringid> <reason>\" to trigger DELBA for Tx side\n"
857 	" - \"del_rx <CID> <TID> <reason>\" to trigger DELBA for Rx side\n"
858 	"If missing, <reason> set to \"STA_LEAVING\" (36)\n";
859 
860 	return simple_read_from_buffer(user_buf, count, ppos, text,
861 				       sizeof(text));
862 }
863 
864 static const struct file_operations fops_back = {
865 	.read = wil_read_back,
866 	.write = wil_write_back,
867 	.open  = simple_open,
868 };
869 
870 /* pmc control, write:
871  * - "alloc <num descriptors> <descriptor_size>" to allocate PMC
872  * - "free" to release memory allocated for PMC
873  */
874 static ssize_t wil_write_pmccfg(struct file *file, const char __user *buf,
875 				size_t len, loff_t *ppos)
876 {
877 	struct wil6210_priv *wil = file->private_data;
878 	int rc;
879 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
880 	char cmd[9];
881 	int num_descs, desc_size;
882 
883 	if (!kbuf)
884 		return -ENOMEM;
885 
886 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
887 	if (rc != len) {
888 		kfree(kbuf);
889 		return rc >= 0 ? -EIO : rc;
890 	}
891 
892 	kbuf[len] = '\0';
893 	rc = sscanf(kbuf, "%8s %d %d", cmd, &num_descs, &desc_size);
894 	kfree(kbuf);
895 
896 	if (rc < 0)
897 		return rc;
898 
899 	if (rc < 1) {
900 		wil_err(wil, "pmccfg: no params given\n");
901 		return -EINVAL;
902 	}
903 
904 	if (0 == strcmp(cmd, "alloc")) {
905 		if (rc != 3) {
906 			wil_err(wil, "pmccfg: alloc requires 2 params\n");
907 			return -EINVAL;
908 		}
909 		wil_pmc_alloc(wil, num_descs, desc_size);
910 	} else if (0 == strcmp(cmd, "free")) {
911 		if (rc != 1) {
912 			wil_err(wil, "pmccfg: free does not have any params\n");
913 			return -EINVAL;
914 		}
915 		wil_pmc_free(wil, true);
916 	} else {
917 		wil_err(wil, "pmccfg: Unrecognized command \"%s\"\n", cmd);
918 		return -EINVAL;
919 	}
920 
921 	return len;
922 }
923 
924 static ssize_t wil_read_pmccfg(struct file *file, char __user *user_buf,
925 			       size_t count, loff_t *ppos)
926 {
927 	struct wil6210_priv *wil = file->private_data;
928 	char text[256];
929 	char help[] = "pmc control, write:\n"
930 	" - \"alloc <num descriptors> <descriptor_size>\" to allocate pmc\n"
931 	" - \"free\" to free memory allocated for pmc\n";
932 
933 	sprintf(text, "Last command status: %d\n\n%s",
934 		wil_pmc_last_cmd_status(wil),
935 		help);
936 
937 	return simple_read_from_buffer(user_buf, count, ppos, text,
938 				       strlen(text) + 1);
939 }
940 
941 static const struct file_operations fops_pmccfg = {
942 	.read = wil_read_pmccfg,
943 	.write = wil_write_pmccfg,
944 	.open  = simple_open,
945 };
946 
947 static const struct file_operations fops_pmcdata = {
948 	.open		= simple_open,
949 	.read		= wil_pmc_read,
950 	.llseek		= wil_pmc_llseek,
951 };
952 
953 /*---tx_mgmt---*/
954 /* Write mgmt frame to this file to send it */
955 static ssize_t wil_write_file_txmgmt(struct file *file, const char __user *buf,
956 				     size_t len, loff_t *ppos)
957 {
958 	struct wil6210_priv *wil = file->private_data;
959 	struct wiphy *wiphy = wil_to_wiphy(wil);
960 	struct wireless_dev *wdev = wil->main_ndev->ieee80211_ptr;
961 	struct cfg80211_mgmt_tx_params params;
962 	int rc;
963 	void *frame;
964 
965 	if (!len)
966 		return -EINVAL;
967 
968 	frame = memdup_user(buf, len);
969 	if (IS_ERR(frame))
970 		return PTR_ERR(frame);
971 
972 	params.buf = frame;
973 	params.len = len;
974 
975 	rc = wil_cfg80211_mgmt_tx(wiphy, wdev, &params, NULL);
976 
977 	kfree(frame);
978 	wil_info(wil, "-> %d\n", rc);
979 
980 	return len;
981 }
982 
983 static const struct file_operations fops_txmgmt = {
984 	.write = wil_write_file_txmgmt,
985 	.open  = simple_open,
986 };
987 
988 /* Write WMI command (w/o mbox header) to this file to send it
989  * WMI starts from wil6210_mbox_hdr_wmi header
990  */
991 static ssize_t wil_write_file_wmi(struct file *file, const char __user *buf,
992 				  size_t len, loff_t *ppos)
993 {
994 	struct wil6210_priv *wil = file->private_data;
995 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
996 	struct wmi_cmd_hdr *wmi;
997 	void *cmd;
998 	int cmdlen = len - sizeof(struct wmi_cmd_hdr);
999 	u16 cmdid;
1000 	int rc, rc1;
1001 
1002 	if (cmdlen < 0)
1003 		return -EINVAL;
1004 
1005 	wmi = kmalloc(len, GFP_KERNEL);
1006 	if (!wmi)
1007 		return -ENOMEM;
1008 
1009 	rc = simple_write_to_buffer(wmi, len, ppos, buf, len);
1010 	if (rc < 0) {
1011 		kfree(wmi);
1012 		return rc;
1013 	}
1014 
1015 	cmd = (cmdlen > 0) ? &wmi[1] : NULL;
1016 	cmdid = le16_to_cpu(wmi->command_id);
1017 
1018 	rc1 = wmi_send(wil, cmdid, vif->mid, cmd, cmdlen);
1019 	kfree(wmi);
1020 
1021 	wil_info(wil, "0x%04x[%d] -> %d\n", cmdid, cmdlen, rc1);
1022 
1023 	return rc;
1024 }
1025 
1026 static const struct file_operations fops_wmi = {
1027 	.write = wil_write_file_wmi,
1028 	.open  = simple_open,
1029 };
1030 
1031 static void wil_seq_print_skb(struct seq_file *s, struct sk_buff *skb)
1032 {
1033 	int i = 0;
1034 	int len = skb_headlen(skb);
1035 	void *p = skb->data;
1036 	int nr_frags = skb_shinfo(skb)->nr_frags;
1037 
1038 	seq_printf(s, "    len = %d\n", len);
1039 	wil_seq_hexdump(s, p, len, "      : ");
1040 
1041 	if (nr_frags) {
1042 		seq_printf(s, "    nr_frags = %d\n", nr_frags);
1043 		for (i = 0; i < nr_frags; i++) {
1044 			const struct skb_frag_struct *frag =
1045 					&skb_shinfo(skb)->frags[i];
1046 
1047 			len = skb_frag_size(frag);
1048 			p = skb_frag_address_safe(frag);
1049 			seq_printf(s, "    [%2d] : len = %d\n", i, len);
1050 			wil_seq_hexdump(s, p, len, "      : ");
1051 		}
1052 	}
1053 }
1054 
1055 /*---------Tx/Rx descriptor------------*/
1056 static int wil_txdesc_debugfs_show(struct seq_file *s, void *data)
1057 {
1058 	struct wil6210_priv *wil = s->private;
1059 	struct wil_ring *ring;
1060 	bool tx;
1061 	int ring_idx = dbg_ring_index;
1062 	int txdesc_idx = dbg_txdesc_index;
1063 	volatile struct vring_tx_desc *d;
1064 	volatile u32 *u;
1065 	struct sk_buff *skb;
1066 
1067 	if (wil->use_enhanced_dma_hw) {
1068 		/* RX ring index == 0 */
1069 		if (ring_idx >= WIL6210_MAX_TX_RINGS) {
1070 			seq_printf(s, "invalid ring index %d\n", ring_idx);
1071 			return 0;
1072 		}
1073 		tx = ring_idx > 0; /* desc ring 0 is reserved for RX */
1074 	} else {
1075 		/* RX ring index == WIL6210_MAX_TX_RINGS */
1076 		if (ring_idx > WIL6210_MAX_TX_RINGS) {
1077 			seq_printf(s, "invalid ring index %d\n", ring_idx);
1078 			return 0;
1079 		}
1080 		tx = (ring_idx < WIL6210_MAX_TX_RINGS);
1081 	}
1082 
1083 	ring = tx ? &wil->ring_tx[ring_idx] : &wil->ring_rx;
1084 
1085 	if (!ring->va) {
1086 		if (tx)
1087 			seq_printf(s, "No Tx[%2d] RING\n", ring_idx);
1088 		else
1089 			seq_puts(s, "No Rx RING\n");
1090 		return 0;
1091 	}
1092 
1093 	if (txdesc_idx >= ring->size) {
1094 		if (tx)
1095 			seq_printf(s, "[%2d] TxDesc index (%d) >= size (%d)\n",
1096 				   ring_idx, txdesc_idx, ring->size);
1097 		else
1098 			seq_printf(s, "RxDesc index (%d) >= size (%d)\n",
1099 				   txdesc_idx, ring->size);
1100 		return 0;
1101 	}
1102 
1103 	/* use struct vring_tx_desc for Rx as well,
1104 	 * only field used, .dma.length, is the same
1105 	 */
1106 	d = &ring->va[txdesc_idx].tx.legacy;
1107 	u = (volatile u32 *)d;
1108 	skb = NULL;
1109 
1110 	if (wil->use_enhanced_dma_hw) {
1111 		if (tx) {
1112 			skb = ring->ctx[txdesc_idx].skb;
1113 		} else {
1114 			struct wil_rx_enhanced_desc *rx_d =
1115 				(struct wil_rx_enhanced_desc *)
1116 				&ring->va[txdesc_idx].rx.enhanced;
1117 			u16 buff_id = le16_to_cpu(rx_d->mac.buff_id);
1118 
1119 			if (!wil_val_in_range(buff_id, 0,
1120 					      wil->rx_buff_mgmt.size)) {
1121 				seq_printf(s, "invalid buff_id %d\n", buff_id);
1122 				return 0;
1123 			}
1124 			skb = wil->rx_buff_mgmt.buff_arr[buff_id].skb;
1125 		}
1126 	} else {
1127 		skb = ring->ctx[txdesc_idx].skb;
1128 	}
1129 	if (tx)
1130 		seq_printf(s, "Tx[%2d][%3d] = {\n", ring_idx,
1131 			   txdesc_idx);
1132 	else
1133 		seq_printf(s, "Rx[%3d] = {\n", txdesc_idx);
1134 	seq_printf(s, "  MAC = 0x%08x 0x%08x 0x%08x 0x%08x\n",
1135 		   u[0], u[1], u[2], u[3]);
1136 	seq_printf(s, "  DMA = 0x%08x 0x%08x 0x%08x 0x%08x\n",
1137 		   u[4], u[5], u[6], u[7]);
1138 	seq_printf(s, "  SKB = 0x%p\n", skb);
1139 
1140 	if (skb) {
1141 		skb_get(skb);
1142 		wil_seq_print_skb(s, skb);
1143 		kfree_skb(skb);
1144 	}
1145 	seq_puts(s, "}\n");
1146 
1147 	return 0;
1148 }
1149 
1150 static int wil_txdesc_seq_open(struct inode *inode, struct file *file)
1151 {
1152 	return single_open(file, wil_txdesc_debugfs_show, inode->i_private);
1153 }
1154 
1155 static const struct file_operations fops_txdesc = {
1156 	.open		= wil_txdesc_seq_open,
1157 	.release	= single_release,
1158 	.read		= seq_read,
1159 	.llseek		= seq_lseek,
1160 };
1161 
1162 /*---------Tx/Rx status message------------*/
1163 static int wil_status_msg_debugfs_show(struct seq_file *s, void *data)
1164 {
1165 	struct wil6210_priv *wil = s->private;
1166 	int sring_idx = dbg_sring_index;
1167 	struct wil_status_ring *sring;
1168 	bool tx = sring_idx == wil->tx_sring_idx ? 1 : 0;
1169 	u32 status_msg_idx = dbg_status_msg_index;
1170 	u32 *u;
1171 
1172 	if (sring_idx >= WIL6210_MAX_STATUS_RINGS) {
1173 		seq_printf(s, "invalid status ring index %d\n", sring_idx);
1174 		return 0;
1175 	}
1176 
1177 	sring = &wil->srings[sring_idx];
1178 
1179 	if (!sring->va) {
1180 		seq_printf(s, "No %cX status ring\n", tx ? 'T' : 'R');
1181 		return 0;
1182 	}
1183 
1184 	if (status_msg_idx >= sring->size) {
1185 		seq_printf(s, "%cxDesc index (%d) >= size (%d)\n",
1186 			   tx ? 'T' : 'R', status_msg_idx, sring->size);
1187 		return 0;
1188 	}
1189 
1190 	u = sring->va + (sring->elem_size * status_msg_idx);
1191 
1192 	seq_printf(s, "%cx[%d][%3d] = {\n",
1193 		   tx ? 'T' : 'R', sring_idx, status_msg_idx);
1194 
1195 	seq_printf(s, "  0x%08x 0x%08x 0x%08x 0x%08x\n",
1196 		   u[0], u[1], u[2], u[3]);
1197 	if (!tx && !wil->use_compressed_rx_status)
1198 		seq_printf(s, "  0x%08x 0x%08x 0x%08x 0x%08x\n",
1199 			   u[4], u[5], u[6], u[7]);
1200 
1201 	seq_puts(s, "}\n");
1202 
1203 	return 0;
1204 }
1205 
1206 static int wil_status_msg_seq_open(struct inode *inode, struct file *file)
1207 {
1208 	return single_open(file, wil_status_msg_debugfs_show,
1209 			   inode->i_private);
1210 }
1211 
1212 static const struct file_operations fops_status_msg = {
1213 	.open		= wil_status_msg_seq_open,
1214 	.release	= single_release,
1215 	.read		= seq_read,
1216 	.llseek		= seq_lseek,
1217 };
1218 
1219 static int wil_print_rx_buff(struct seq_file *s, struct list_head *lh)
1220 {
1221 	struct wil_rx_buff *it;
1222 	int i = 0;
1223 
1224 	list_for_each_entry(it, lh, list) {
1225 		if ((i % 16) == 0 && i != 0)
1226 			seq_puts(s, "\n    ");
1227 		seq_printf(s, "[%4d] ", it->id);
1228 		i++;
1229 	}
1230 	seq_printf(s, "\nNumber of buffers: %u\n", i);
1231 
1232 	return i;
1233 }
1234 
1235 static int wil_rx_buff_mgmt_debugfs_show(struct seq_file *s, void *data)
1236 {
1237 	struct wil6210_priv *wil = s->private;
1238 	struct wil_rx_buff_mgmt *rbm = &wil->rx_buff_mgmt;
1239 	int num_active;
1240 	int num_free;
1241 
1242 	if (!rbm->buff_arr)
1243 		return -EINVAL;
1244 
1245 	seq_printf(s, "  size = %zu\n", rbm->size);
1246 	seq_printf(s, "  free_list_empty_cnt = %lu\n",
1247 		   rbm->free_list_empty_cnt);
1248 
1249 	/* Print active list */
1250 	seq_puts(s, "  Active list:\n");
1251 	num_active = wil_print_rx_buff(s, &rbm->active);
1252 	seq_puts(s, "\n  Free list:\n");
1253 	num_free = wil_print_rx_buff(s, &rbm->free);
1254 
1255 	seq_printf(s, "  Total number of buffers: %u\n",
1256 		   num_active + num_free);
1257 
1258 	return 0;
1259 }
1260 
1261 static int wil_rx_buff_mgmt_seq_open(struct inode *inode, struct file *file)
1262 {
1263 	return single_open(file, wil_rx_buff_mgmt_debugfs_show,
1264 			   inode->i_private);
1265 }
1266 
1267 static const struct file_operations fops_rx_buff_mgmt = {
1268 	.open		= wil_rx_buff_mgmt_seq_open,
1269 	.release	= single_release,
1270 	.read		= seq_read,
1271 	.llseek		= seq_lseek,
1272 };
1273 
1274 /*---------beamforming------------*/
1275 static char *wil_bfstatus_str(u32 status)
1276 {
1277 	switch (status) {
1278 	case 0:
1279 		return "Failed";
1280 	case 1:
1281 		return "OK";
1282 	case 2:
1283 		return "Retrying";
1284 	default:
1285 		return "??";
1286 	}
1287 }
1288 
1289 static bool is_all_zeros(void * const x_, size_t sz)
1290 {
1291 	/* if reply is all-0, ignore this CID */
1292 	u32 *x = x_;
1293 	int n;
1294 
1295 	for (n = 0; n < sz / sizeof(*x); n++)
1296 		if (x[n])
1297 			return false;
1298 
1299 	return true;
1300 }
1301 
1302 static int wil_bf_debugfs_show(struct seq_file *s, void *data)
1303 {
1304 	int rc;
1305 	int i;
1306 	struct wil6210_priv *wil = s->private;
1307 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1308 	struct wmi_notify_req_cmd cmd = {
1309 		.interval_usec = 0,
1310 	};
1311 	struct {
1312 		struct wmi_cmd_hdr wmi;
1313 		struct wmi_notify_req_done_event evt;
1314 	} __packed reply;
1315 
1316 	memset(&reply, 0, sizeof(reply));
1317 
1318 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1319 		u32 status;
1320 
1321 		cmd.cid = i;
1322 		rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, vif->mid,
1323 			      &cmd, sizeof(cmd),
1324 			      WMI_NOTIFY_REQ_DONE_EVENTID, &reply,
1325 			      sizeof(reply), 20);
1326 		/* if reply is all-0, ignore this CID */
1327 		if (rc || is_all_zeros(&reply.evt, sizeof(reply.evt)))
1328 			continue;
1329 
1330 		status = le32_to_cpu(reply.evt.status);
1331 		seq_printf(s, "CID %d {\n"
1332 			   "  TSF = 0x%016llx\n"
1333 			   "  TxMCS = %2d TxTpt = %4d\n"
1334 			   "  SQI = %4d\n"
1335 			   "  RSSI = %4d\n"
1336 			   "  Status = 0x%08x %s\n"
1337 			   "  Sectors(rx:tx) my %2d:%2d peer %2d:%2d\n"
1338 			   "  Goodput(rx:tx) %4d:%4d\n"
1339 			   "}\n",
1340 			   i,
1341 			   le64_to_cpu(reply.evt.tsf),
1342 			   le16_to_cpu(reply.evt.bf_mcs),
1343 			   le32_to_cpu(reply.evt.tx_tpt),
1344 			   reply.evt.sqi,
1345 			   reply.evt.rssi,
1346 			   status, wil_bfstatus_str(status),
1347 			   le16_to_cpu(reply.evt.my_rx_sector),
1348 			   le16_to_cpu(reply.evt.my_tx_sector),
1349 			   le16_to_cpu(reply.evt.other_rx_sector),
1350 			   le16_to_cpu(reply.evt.other_tx_sector),
1351 			   le32_to_cpu(reply.evt.rx_goodput),
1352 			   le32_to_cpu(reply.evt.tx_goodput));
1353 	}
1354 	return 0;
1355 }
1356 
1357 static int wil_bf_seq_open(struct inode *inode, struct file *file)
1358 {
1359 	return single_open(file, wil_bf_debugfs_show, inode->i_private);
1360 }
1361 
1362 static const struct file_operations fops_bf = {
1363 	.open		= wil_bf_seq_open,
1364 	.release	= single_release,
1365 	.read		= seq_read,
1366 	.llseek		= seq_lseek,
1367 };
1368 
1369 /*---------temp------------*/
1370 static void print_temp(struct seq_file *s, const char *prefix, s32 t)
1371 {
1372 	switch (t) {
1373 	case 0:
1374 	case ~(u32)0:
1375 		seq_printf(s, "%s N/A\n", prefix);
1376 	break;
1377 	default:
1378 		seq_printf(s, "%s %s%d.%03d\n", prefix, (t < 0 ? "-" : ""),
1379 			   abs(t / 1000), abs(t % 1000));
1380 		break;
1381 	}
1382 }
1383 
1384 static int wil_temp_debugfs_show(struct seq_file *s, void *data)
1385 {
1386 	struct wil6210_priv *wil = s->private;
1387 	s32 t_m, t_r;
1388 	int rc = wmi_get_temperature(wil, &t_m, &t_r);
1389 
1390 	if (rc) {
1391 		seq_puts(s, "Failed\n");
1392 		return 0;
1393 	}
1394 
1395 	print_temp(s, "T_mac   =", t_m);
1396 	print_temp(s, "T_radio =", t_r);
1397 
1398 	return 0;
1399 }
1400 
1401 static int wil_temp_seq_open(struct inode *inode, struct file *file)
1402 {
1403 	return single_open(file, wil_temp_debugfs_show, inode->i_private);
1404 }
1405 
1406 static const struct file_operations fops_temp = {
1407 	.open		= wil_temp_seq_open,
1408 	.release	= single_release,
1409 	.read		= seq_read,
1410 	.llseek		= seq_lseek,
1411 };
1412 
1413 /*---------freq------------*/
1414 static int wil_freq_debugfs_show(struct seq_file *s, void *data)
1415 {
1416 	struct wil6210_priv *wil = s->private;
1417 	struct wireless_dev *wdev = wil->main_ndev->ieee80211_ptr;
1418 	u32 freq = wdev->chandef.chan ? wdev->chandef.chan->center_freq : 0;
1419 
1420 	seq_printf(s, "Freq = %d\n", freq);
1421 
1422 	return 0;
1423 }
1424 
1425 static int wil_freq_seq_open(struct inode *inode, struct file *file)
1426 {
1427 	return single_open(file, wil_freq_debugfs_show, inode->i_private);
1428 }
1429 
1430 static const struct file_operations fops_freq = {
1431 	.open		= wil_freq_seq_open,
1432 	.release	= single_release,
1433 	.read		= seq_read,
1434 	.llseek		= seq_lseek,
1435 };
1436 
1437 /*---------link------------*/
1438 static int wil_link_debugfs_show(struct seq_file *s, void *data)
1439 {
1440 	struct wil6210_priv *wil = s->private;
1441 	struct station_info *sinfo;
1442 	int i, rc = 0;
1443 
1444 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1445 	if (!sinfo)
1446 		return -ENOMEM;
1447 
1448 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1449 		struct wil_sta_info *p = &wil->sta[i];
1450 		char *status = "unknown";
1451 		struct wil6210_vif *vif;
1452 		u8 mid;
1453 
1454 		switch (p->status) {
1455 		case wil_sta_unused:
1456 			status = "unused   ";
1457 			break;
1458 		case wil_sta_conn_pending:
1459 			status = "pending  ";
1460 			break;
1461 		case wil_sta_connected:
1462 			status = "connected";
1463 			break;
1464 		}
1465 		mid = (p->status != wil_sta_unused) ? p->mid : U8_MAX;
1466 		seq_printf(s, "[%d][MID %d] %pM %s\n",
1467 			   i, mid, p->addr, status);
1468 
1469 		if (p->status != wil_sta_connected)
1470 			continue;
1471 
1472 		vif = (mid < wil->max_vifs) ? wil->vifs[mid] : NULL;
1473 		if (vif) {
1474 			rc = wil_cid_fill_sinfo(vif, i, sinfo);
1475 			if (rc)
1476 				goto out;
1477 
1478 			seq_printf(s, "  Tx_mcs = %d\n", sinfo->txrate.mcs);
1479 			seq_printf(s, "  Rx_mcs = %d\n", sinfo->rxrate.mcs);
1480 			seq_printf(s, "  SQ     = %d\n", sinfo->signal);
1481 		} else {
1482 			seq_puts(s, "  INVALID MID\n");
1483 		}
1484 	}
1485 
1486 out:
1487 	kfree(sinfo);
1488 	return rc;
1489 }
1490 
1491 static int wil_link_seq_open(struct inode *inode, struct file *file)
1492 {
1493 	return single_open(file, wil_link_debugfs_show, inode->i_private);
1494 }
1495 
1496 static const struct file_operations fops_link = {
1497 	.open		= wil_link_seq_open,
1498 	.release	= single_release,
1499 	.read		= seq_read,
1500 	.llseek		= seq_lseek,
1501 };
1502 
1503 /*---------info------------*/
1504 static int wil_info_debugfs_show(struct seq_file *s, void *data)
1505 {
1506 	struct wil6210_priv *wil = s->private;
1507 	struct net_device *ndev = wil->main_ndev;
1508 	int is_ac = power_supply_is_system_supplied();
1509 	int rx = atomic_xchg(&wil->isr_count_rx, 0);
1510 	int tx = atomic_xchg(&wil->isr_count_tx, 0);
1511 	static ulong rxf_old, txf_old;
1512 	ulong rxf = ndev->stats.rx_packets;
1513 	ulong txf = ndev->stats.tx_packets;
1514 	unsigned int i;
1515 
1516 	/* >0 : AC; 0 : battery; <0 : error */
1517 	seq_printf(s, "AC powered : %d\n", is_ac);
1518 	seq_printf(s, "Rx irqs:packets : %8d : %8ld\n", rx, rxf - rxf_old);
1519 	seq_printf(s, "Tx irqs:packets : %8d : %8ld\n", tx, txf - txf_old);
1520 	rxf_old = rxf;
1521 	txf_old = txf;
1522 
1523 #define CHECK_QSTATE(x) (state & BIT(__QUEUE_STATE_ ## x)) ? \
1524 	" " __stringify(x) : ""
1525 
1526 	for (i = 0; i < ndev->num_tx_queues; i++) {
1527 		struct netdev_queue *txq = netdev_get_tx_queue(ndev, i);
1528 		unsigned long state = txq->state;
1529 
1530 		seq_printf(s, "Tx queue[%i] state : 0x%lx%s%s%s\n", i, state,
1531 			   CHECK_QSTATE(DRV_XOFF),
1532 			   CHECK_QSTATE(STACK_XOFF),
1533 			   CHECK_QSTATE(FROZEN)
1534 			  );
1535 	}
1536 #undef CHECK_QSTATE
1537 	return 0;
1538 }
1539 
1540 static int wil_info_seq_open(struct inode *inode, struct file *file)
1541 {
1542 	return single_open(file, wil_info_debugfs_show, inode->i_private);
1543 }
1544 
1545 static const struct file_operations fops_info = {
1546 	.open		= wil_info_seq_open,
1547 	.release	= single_release,
1548 	.read		= seq_read,
1549 	.llseek		= seq_lseek,
1550 };
1551 
1552 /*---------recovery------------*/
1553 /* mode = [manual|auto]
1554  * state = [idle|pending|running]
1555  */
1556 static ssize_t wil_read_file_recovery(struct file *file, char __user *user_buf,
1557 				      size_t count, loff_t *ppos)
1558 {
1559 	struct wil6210_priv *wil = file->private_data;
1560 	char buf[80];
1561 	int n;
1562 	static const char * const sstate[] = {"idle", "pending", "running"};
1563 
1564 	n = snprintf(buf, sizeof(buf), "mode = %s\nstate = %s\n",
1565 		     no_fw_recovery ? "manual" : "auto",
1566 		     sstate[wil->recovery_state]);
1567 
1568 	n = min_t(int, n, sizeof(buf));
1569 
1570 	return simple_read_from_buffer(user_buf, count, ppos,
1571 				       buf, n);
1572 }
1573 
1574 static ssize_t wil_write_file_recovery(struct file *file,
1575 				       const char __user *buf_,
1576 				       size_t count, loff_t *ppos)
1577 {
1578 	struct wil6210_priv *wil = file->private_data;
1579 	static const char run_command[] = "run";
1580 	char buf[sizeof(run_command) + 1]; /* to detect "runx" */
1581 	ssize_t rc;
1582 
1583 	if (wil->recovery_state != fw_recovery_pending) {
1584 		wil_err(wil, "No recovery pending\n");
1585 		return -EINVAL;
1586 	}
1587 
1588 	if (*ppos != 0) {
1589 		wil_err(wil, "Offset [%d]\n", (int)*ppos);
1590 		return -EINVAL;
1591 	}
1592 
1593 	if (count > sizeof(buf)) {
1594 		wil_err(wil, "Input too long, len = %d\n", (int)count);
1595 		return -EINVAL;
1596 	}
1597 
1598 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, buf_, count);
1599 	if (rc < 0)
1600 		return rc;
1601 
1602 	buf[rc] = '\0';
1603 	if (0 == strcmp(buf, run_command))
1604 		wil_set_recovery_state(wil, fw_recovery_running);
1605 	else
1606 		wil_err(wil, "Bad recovery command \"%s\"\n", buf);
1607 
1608 	return rc;
1609 }
1610 
1611 static const struct file_operations fops_recovery = {
1612 	.read = wil_read_file_recovery,
1613 	.write = wil_write_file_recovery,
1614 	.open  = simple_open,
1615 };
1616 
1617 /*---------Station matrix------------*/
1618 static void wil_print_rxtid(struct seq_file *s, struct wil_tid_ampdu_rx *r)
1619 {
1620 	int i;
1621 	u16 index = ((r->head_seq_num - r->ssn) & 0xfff) % r->buf_size;
1622 	unsigned long long drop_dup = r->drop_dup, drop_old = r->drop_old;
1623 	unsigned long long drop_dup_mcast = r->drop_dup_mcast;
1624 
1625 	seq_printf(s, "([%2d]) 0x%03x [", r->buf_size, r->head_seq_num);
1626 	for (i = 0; i < r->buf_size; i++) {
1627 		if (i == index)
1628 			seq_printf(s, "%c", r->reorder_buf[i] ? 'O' : '|');
1629 		else
1630 			seq_printf(s, "%c", r->reorder_buf[i] ? '*' : '_');
1631 	}
1632 	seq_printf(s,
1633 		   "] total %llu drop %llu (dup %llu + old %llu + dup mcast %llu) last 0x%03x\n",
1634 		   r->total, drop_dup + drop_old + drop_dup_mcast, drop_dup,
1635 		   drop_old, drop_dup_mcast, r->ssn_last_drop);
1636 }
1637 
1638 static void wil_print_rxtid_crypto(struct seq_file *s, int tid,
1639 				   struct wil_tid_crypto_rx *c)
1640 {
1641 	int i;
1642 
1643 	for (i = 0; i < 4; i++) {
1644 		struct wil_tid_crypto_rx_single *cc = &c->key_id[i];
1645 
1646 		if (cc->key_set)
1647 			goto has_keys;
1648 	}
1649 	return;
1650 
1651 has_keys:
1652 	if (tid < WIL_STA_TID_NUM)
1653 		seq_printf(s, "  [%2d] PN", tid);
1654 	else
1655 		seq_puts(s, "  [GR] PN");
1656 
1657 	for (i = 0; i < 4; i++) {
1658 		struct wil_tid_crypto_rx_single *cc = &c->key_id[i];
1659 
1660 		seq_printf(s, " [%i%s]%6phN", i, cc->key_set ? "+" : "-",
1661 			   cc->pn);
1662 	}
1663 	seq_puts(s, "\n");
1664 }
1665 
1666 static int wil_sta_debugfs_show(struct seq_file *s, void *data)
1667 __acquires(&p->tid_rx_lock) __releases(&p->tid_rx_lock)
1668 {
1669 	struct wil6210_priv *wil = s->private;
1670 	int i, tid, mcs;
1671 
1672 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1673 		struct wil_sta_info *p = &wil->sta[i];
1674 		char *status = "unknown";
1675 		u8 aid = 0;
1676 		u8 mid;
1677 		bool sta_connected = false;
1678 
1679 		switch (p->status) {
1680 		case wil_sta_unused:
1681 			status = "unused   ";
1682 			break;
1683 		case wil_sta_conn_pending:
1684 			status = "pending  ";
1685 			break;
1686 		case wil_sta_connected:
1687 			status = "connected";
1688 			aid = p->aid;
1689 			break;
1690 		}
1691 		mid = (p->status != wil_sta_unused) ? p->mid : U8_MAX;
1692 		if (mid < wil->max_vifs) {
1693 			struct wil6210_vif *vif = wil->vifs[mid];
1694 
1695 			if (vif->wdev.iftype == NL80211_IFTYPE_STATION &&
1696 			    p->status == wil_sta_connected)
1697 				sta_connected = true;
1698 		}
1699 		/* print roam counter only for connected stations */
1700 		if (sta_connected)
1701 			seq_printf(s, "[%d] %pM connected (roam counter %d) MID %d AID %d\n",
1702 				   i, p->addr, p->stats.ft_roams, mid, aid);
1703 		else
1704 			seq_printf(s, "[%d] %pM %s MID %d AID %d\n", i,
1705 				   p->addr, status, mid, aid);
1706 
1707 		if (p->status == wil_sta_connected) {
1708 			spin_lock_bh(&p->tid_rx_lock);
1709 			for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1710 				struct wil_tid_ampdu_rx *r = p->tid_rx[tid];
1711 				struct wil_tid_crypto_rx *c =
1712 						&p->tid_crypto_rx[tid];
1713 
1714 				if (r) {
1715 					seq_printf(s, "  [%2d] ", tid);
1716 					wil_print_rxtid(s, r);
1717 				}
1718 
1719 				wil_print_rxtid_crypto(s, tid, c);
1720 			}
1721 			wil_print_rxtid_crypto(s, WIL_STA_TID_NUM,
1722 					       &p->group_crypto_rx);
1723 			spin_unlock_bh(&p->tid_rx_lock);
1724 			seq_printf(s,
1725 				   "Rx invalid frame: non-data %lu, short %lu, large %lu, replay %lu\n",
1726 				   p->stats.rx_non_data_frame,
1727 				   p->stats.rx_short_frame,
1728 				   p->stats.rx_large_frame,
1729 				   p->stats.rx_replay);
1730 			seq_printf(s,
1731 				   "mic error %lu, key error %lu, amsdu error %lu, csum error %lu\n",
1732 				   p->stats.rx_mic_error,
1733 				   p->stats.rx_key_error,
1734 				   p->stats.rx_amsdu_error,
1735 				   p->stats.rx_csum_err);
1736 
1737 			seq_puts(s, "Rx/MCS:");
1738 			for (mcs = 0; mcs < ARRAY_SIZE(p->stats.rx_per_mcs);
1739 			     mcs++)
1740 				seq_printf(s, " %lld",
1741 					   p->stats.rx_per_mcs[mcs]);
1742 			seq_puts(s, "\n");
1743 		}
1744 	}
1745 
1746 	return 0;
1747 }
1748 
1749 static int wil_sta_seq_open(struct inode *inode, struct file *file)
1750 {
1751 	return single_open(file, wil_sta_debugfs_show, inode->i_private);
1752 }
1753 
1754 static const struct file_operations fops_sta = {
1755 	.open		= wil_sta_seq_open,
1756 	.release	= single_release,
1757 	.read		= seq_read,
1758 	.llseek		= seq_lseek,
1759 };
1760 
1761 static int wil_mids_debugfs_show(struct seq_file *s, void *data)
1762 {
1763 	struct wil6210_priv *wil = s->private;
1764 	struct wil6210_vif *vif;
1765 	struct net_device *ndev;
1766 	int i;
1767 
1768 	mutex_lock(&wil->vif_mutex);
1769 	for (i = 0; i < wil->max_vifs; i++) {
1770 		vif = wil->vifs[i];
1771 
1772 		if (vif) {
1773 			ndev = vif_to_ndev(vif);
1774 			seq_printf(s, "[%d] %pM %s\n", i, ndev->dev_addr,
1775 				   ndev->name);
1776 		} else {
1777 			seq_printf(s, "[%d] unused\n", i);
1778 		}
1779 	}
1780 	mutex_unlock(&wil->vif_mutex);
1781 
1782 	return 0;
1783 }
1784 
1785 static int wil_mids_seq_open(struct inode *inode, struct file *file)
1786 {
1787 	return single_open(file, wil_mids_debugfs_show, inode->i_private);
1788 }
1789 
1790 static const struct file_operations fops_mids = {
1791 	.open		= wil_mids_seq_open,
1792 	.release	= single_release,
1793 	.read		= seq_read,
1794 	.llseek		= seq_lseek,
1795 };
1796 
1797 static int wil_tx_latency_debugfs_show(struct seq_file *s, void *data)
1798 __acquires(&p->tid_rx_lock) __releases(&p->tid_rx_lock)
1799 {
1800 	struct wil6210_priv *wil = s->private;
1801 	int i, bin;
1802 
1803 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1804 		struct wil_sta_info *p = &wil->sta[i];
1805 		char *status = "unknown";
1806 		u8 aid = 0;
1807 		u8 mid;
1808 
1809 		if (!p->tx_latency_bins)
1810 			continue;
1811 
1812 		switch (p->status) {
1813 		case wil_sta_unused:
1814 			status = "unused   ";
1815 			break;
1816 		case wil_sta_conn_pending:
1817 			status = "pending  ";
1818 			break;
1819 		case wil_sta_connected:
1820 			status = "connected";
1821 			aid = p->aid;
1822 			break;
1823 		}
1824 		mid = (p->status != wil_sta_unused) ? p->mid : U8_MAX;
1825 		seq_printf(s, "[%d] %pM %s MID %d AID %d\n", i, p->addr, status,
1826 			   mid, aid);
1827 
1828 		if (p->status == wil_sta_connected) {
1829 			u64 num_packets = 0;
1830 			u64 tx_latency_avg = p->stats.tx_latency_total_us;
1831 
1832 			seq_puts(s, "Tx/Latency bin:");
1833 			for (bin = 0; bin < WIL_NUM_LATENCY_BINS; bin++) {
1834 				seq_printf(s, " %lld",
1835 					   p->tx_latency_bins[bin]);
1836 				num_packets += p->tx_latency_bins[bin];
1837 			}
1838 			seq_puts(s, "\n");
1839 			if (!num_packets)
1840 				continue;
1841 			do_div(tx_latency_avg, num_packets);
1842 			seq_printf(s, "Tx/Latency min/avg/max (us): %d/%lld/%d",
1843 				   p->stats.tx_latency_min_us,
1844 				   tx_latency_avg,
1845 				   p->stats.tx_latency_max_us);
1846 
1847 			seq_puts(s, "\n");
1848 		}
1849 	}
1850 
1851 	return 0;
1852 }
1853 
1854 static int wil_tx_latency_seq_open(struct inode *inode, struct file *file)
1855 {
1856 	return single_open(file, wil_tx_latency_debugfs_show,
1857 			   inode->i_private);
1858 }
1859 
1860 static ssize_t wil_tx_latency_write(struct file *file, const char __user *buf,
1861 				    size_t len, loff_t *ppos)
1862 {
1863 	struct seq_file *s = file->private_data;
1864 	struct wil6210_priv *wil = s->private;
1865 	int val, rc, i;
1866 	bool enable;
1867 
1868 	rc = kstrtoint_from_user(buf, len, 0, &val);
1869 	if (rc) {
1870 		wil_err(wil, "Invalid argument\n");
1871 		return rc;
1872 	}
1873 	if (val == 1)
1874 		/* default resolution */
1875 		val = 500;
1876 	if (val && (val < 50 || val > 1000)) {
1877 		wil_err(wil, "Invalid resolution %d\n", val);
1878 		return -EINVAL;
1879 	}
1880 
1881 	enable = !!val;
1882 	if (wil->tx_latency == enable)
1883 		return len;
1884 
1885 	wil_info(wil, "%s TX latency measurements (resolution %dusec)\n",
1886 		 enable ? "Enabling" : "Disabling", val);
1887 
1888 	if (enable) {
1889 		size_t sz = sizeof(u64) * WIL_NUM_LATENCY_BINS;
1890 
1891 		wil->tx_latency_res = val;
1892 		for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1893 			struct wil_sta_info *sta = &wil->sta[i];
1894 
1895 			kfree(sta->tx_latency_bins);
1896 			sta->tx_latency_bins = kzalloc(sz, GFP_KERNEL);
1897 			if (!sta->tx_latency_bins)
1898 				return -ENOMEM;
1899 			sta->stats.tx_latency_min_us = U32_MAX;
1900 			sta->stats.tx_latency_max_us = 0;
1901 			sta->stats.tx_latency_total_us = 0;
1902 		}
1903 	}
1904 	wil->tx_latency = enable;
1905 
1906 	return len;
1907 }
1908 
1909 static const struct file_operations fops_tx_latency = {
1910 	.open		= wil_tx_latency_seq_open,
1911 	.release	= single_release,
1912 	.read		= seq_read,
1913 	.write		= wil_tx_latency_write,
1914 	.llseek		= seq_lseek,
1915 };
1916 
1917 static void wil_link_stats_print_basic(struct wil6210_vif *vif,
1918 				       struct seq_file *s,
1919 				       struct wmi_link_stats_basic *basic)
1920 {
1921 	char per[5] = "?";
1922 
1923 	if (basic->per_average != 0xff)
1924 		snprintf(per, sizeof(per), "%d%%", basic->per_average);
1925 
1926 	seq_printf(s, "CID %d {\n"
1927 		   "\tTxMCS %d TxTpt %d\n"
1928 		   "\tGoodput(rx:tx) %d:%d\n"
1929 		   "\tRxBcastFrames %d\n"
1930 		   "\tRSSI %d SQI %d SNR %d PER %s\n"
1931 		   "\tRx RFC %d Ant num %d\n"
1932 		   "\tSectors(rx:tx) my %d:%d peer %d:%d\n"
1933 		   "}\n",
1934 		   basic->cid,
1935 		   basic->bf_mcs, le32_to_cpu(basic->tx_tpt),
1936 		   le32_to_cpu(basic->rx_goodput),
1937 		   le32_to_cpu(basic->tx_goodput),
1938 		   le32_to_cpu(basic->rx_bcast_frames),
1939 		   basic->rssi, basic->sqi, basic->snr, per,
1940 		   basic->selected_rfc, basic->rx_effective_ant_num,
1941 		   basic->my_rx_sector, basic->my_tx_sector,
1942 		   basic->other_rx_sector, basic->other_tx_sector);
1943 }
1944 
1945 static void wil_link_stats_print_global(struct wil6210_priv *wil,
1946 					struct seq_file *s,
1947 					struct wmi_link_stats_global *global)
1948 {
1949 	seq_printf(s, "Frames(rx:tx) %d:%d\n"
1950 		   "BA Frames(rx:tx) %d:%d\n"
1951 		   "Beacons %d\n"
1952 		   "Rx Errors (MIC:CRC) %d:%d\n"
1953 		   "Tx Errors (no ack) %d\n",
1954 		   le32_to_cpu(global->rx_frames),
1955 		   le32_to_cpu(global->tx_frames),
1956 		   le32_to_cpu(global->rx_ba_frames),
1957 		   le32_to_cpu(global->tx_ba_frames),
1958 		   le32_to_cpu(global->tx_beacons),
1959 		   le32_to_cpu(global->rx_mic_errors),
1960 		   le32_to_cpu(global->rx_crc_errors),
1961 		   le32_to_cpu(global->tx_fail_no_ack));
1962 }
1963 
1964 static void wil_link_stats_debugfs_show_vif(struct wil6210_vif *vif,
1965 					    struct seq_file *s)
1966 {
1967 	struct wil6210_priv *wil = vif_to_wil(vif);
1968 	struct wmi_link_stats_basic *stats;
1969 	int i;
1970 
1971 	if (!vif->fw_stats_ready) {
1972 		seq_puts(s, "no statistics\n");
1973 		return;
1974 	}
1975 
1976 	seq_printf(s, "TSF %lld\n", vif->fw_stats_tsf);
1977 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1978 		if (wil->sta[i].status == wil_sta_unused)
1979 			continue;
1980 		if (wil->sta[i].mid != vif->mid)
1981 			continue;
1982 
1983 		stats = &wil->sta[i].fw_stats_basic;
1984 		wil_link_stats_print_basic(vif, s, stats);
1985 	}
1986 }
1987 
1988 static int wil_link_stats_debugfs_show(struct seq_file *s, void *data)
1989 {
1990 	struct wil6210_priv *wil = s->private;
1991 	struct wil6210_vif *vif;
1992 	int i, rc;
1993 
1994 	rc = mutex_lock_interruptible(&wil->vif_mutex);
1995 	if (rc)
1996 		return rc;
1997 
1998 	/* iterate over all MIDs and show per-cid statistics. Then show the
1999 	 * global statistics
2000 	 */
2001 	for (i = 0; i < wil->max_vifs; i++) {
2002 		vif = wil->vifs[i];
2003 
2004 		seq_printf(s, "MID %d ", i);
2005 		if (!vif) {
2006 			seq_puts(s, "unused\n");
2007 			continue;
2008 		}
2009 
2010 		wil_link_stats_debugfs_show_vif(vif, s);
2011 	}
2012 
2013 	mutex_unlock(&wil->vif_mutex);
2014 
2015 	return 0;
2016 }
2017 
2018 static int wil_link_stats_seq_open(struct inode *inode, struct file *file)
2019 {
2020 	return single_open(file, wil_link_stats_debugfs_show, inode->i_private);
2021 }
2022 
2023 static ssize_t wil_link_stats_write(struct file *file, const char __user *buf,
2024 				    size_t len, loff_t *ppos)
2025 {
2026 	struct seq_file *s = file->private_data;
2027 	struct wil6210_priv *wil = s->private;
2028 	int cid, interval, rc, i;
2029 	struct wil6210_vif *vif;
2030 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
2031 
2032 	if (!kbuf)
2033 		return -ENOMEM;
2034 
2035 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
2036 	if (rc != len) {
2037 		kfree(kbuf);
2038 		return rc >= 0 ? -EIO : rc;
2039 	}
2040 
2041 	kbuf[len] = '\0';
2042 	/* specify cid (use -1 for all cids) and snapshot interval in ms */
2043 	rc = sscanf(kbuf, "%d %d", &cid, &interval);
2044 	kfree(kbuf);
2045 	if (rc < 0)
2046 		return rc;
2047 	if (rc < 2 || interval < 0)
2048 		return -EINVAL;
2049 
2050 	wil_info(wil, "request link statistics, cid %d interval %d\n",
2051 		 cid, interval);
2052 
2053 	rc = mutex_lock_interruptible(&wil->vif_mutex);
2054 	if (rc)
2055 		return rc;
2056 
2057 	for (i = 0; i < wil->max_vifs; i++) {
2058 		vif = wil->vifs[i];
2059 		if (!vif)
2060 			continue;
2061 
2062 		rc = wmi_link_stats_cfg(vif, WMI_LINK_STATS_TYPE_BASIC,
2063 					(cid == -1 ? 0xff : cid), interval);
2064 		if (rc)
2065 			wil_err(wil, "link statistics failed for mid %d\n", i);
2066 	}
2067 	mutex_unlock(&wil->vif_mutex);
2068 
2069 	return len;
2070 }
2071 
2072 static const struct file_operations fops_link_stats = {
2073 	.open		= wil_link_stats_seq_open,
2074 	.release	= single_release,
2075 	.read		= seq_read,
2076 	.write		= wil_link_stats_write,
2077 	.llseek		= seq_lseek,
2078 };
2079 
2080 static int
2081 wil_link_stats_global_debugfs_show(struct seq_file *s, void *data)
2082 {
2083 	struct wil6210_priv *wil = s->private;
2084 
2085 	if (!wil->fw_stats_global.ready)
2086 		return 0;
2087 
2088 	seq_printf(s, "TSF %lld\n", wil->fw_stats_global.tsf);
2089 	wil_link_stats_print_global(wil, s, &wil->fw_stats_global.stats);
2090 
2091 	return 0;
2092 }
2093 
2094 static int
2095 wil_link_stats_global_seq_open(struct inode *inode, struct file *file)
2096 {
2097 	return single_open(file, wil_link_stats_global_debugfs_show,
2098 			   inode->i_private);
2099 }
2100 
2101 static ssize_t
2102 wil_link_stats_global_write(struct file *file, const char __user *buf,
2103 			    size_t len, loff_t *ppos)
2104 {
2105 	struct seq_file *s = file->private_data;
2106 	struct wil6210_priv *wil = s->private;
2107 	int interval, rc;
2108 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2109 
2110 	/* specify snapshot interval in ms */
2111 	rc = kstrtoint_from_user(buf, len, 0, &interval);
2112 	if (rc || interval < 0) {
2113 		wil_err(wil, "Invalid argument\n");
2114 		return -EINVAL;
2115 	}
2116 
2117 	wil_info(wil, "request global link stats, interval %d\n", interval);
2118 
2119 	rc = wmi_link_stats_cfg(vif, WMI_LINK_STATS_TYPE_GLOBAL, 0, interval);
2120 	if (rc)
2121 		wil_err(wil, "global link stats failed %d\n", rc);
2122 
2123 	return rc ? rc : len;
2124 }
2125 
2126 static const struct file_operations fops_link_stats_global = {
2127 	.open		= wil_link_stats_global_seq_open,
2128 	.release	= single_release,
2129 	.read		= seq_read,
2130 	.write		= wil_link_stats_global_write,
2131 	.llseek		= seq_lseek,
2132 };
2133 
2134 static ssize_t wil_read_file_led_cfg(struct file *file, char __user *user_buf,
2135 				     size_t count, loff_t *ppos)
2136 {
2137 	char buf[80];
2138 	int n;
2139 
2140 	n = snprintf(buf, sizeof(buf),
2141 		     "led_id is set to %d, echo 1 to enable, 0 to disable\n",
2142 		     led_id);
2143 
2144 	n = min_t(int, n, sizeof(buf));
2145 
2146 	return simple_read_from_buffer(user_buf, count, ppos,
2147 				       buf, n);
2148 }
2149 
2150 static ssize_t wil_write_file_led_cfg(struct file *file,
2151 				      const char __user *buf_,
2152 				      size_t count, loff_t *ppos)
2153 {
2154 	struct wil6210_priv *wil = file->private_data;
2155 	int val;
2156 	int rc;
2157 
2158 	rc = kstrtoint_from_user(buf_, count, 0, &val);
2159 	if (rc) {
2160 		wil_err(wil, "Invalid argument\n");
2161 		return rc;
2162 	}
2163 
2164 	wil_info(wil, "%s led %d\n", val ? "Enabling" : "Disabling", led_id);
2165 	rc = wmi_led_cfg(wil, val);
2166 	if (rc) {
2167 		wil_info(wil, "%s led %d failed\n",
2168 			 val ? "Enabling" : "Disabling", led_id);
2169 		return rc;
2170 	}
2171 
2172 	return count;
2173 }
2174 
2175 static const struct file_operations fops_led_cfg = {
2176 	.read = wil_read_file_led_cfg,
2177 	.write = wil_write_file_led_cfg,
2178 	.open  = simple_open,
2179 };
2180 
2181 /* led_blink_time, write:
2182  * "<blink_on_slow> <blink_off_slow> <blink_on_med> <blink_off_med> <blink_on_fast> <blink_off_fast>
2183  */
2184 static ssize_t wil_write_led_blink_time(struct file *file,
2185 					const char __user *buf,
2186 					size_t len, loff_t *ppos)
2187 {
2188 	int rc;
2189 	char *kbuf = kmalloc(len + 1, GFP_KERNEL);
2190 
2191 	if (!kbuf)
2192 		return -ENOMEM;
2193 
2194 	rc = simple_write_to_buffer(kbuf, len, ppos, buf, len);
2195 	if (rc != len) {
2196 		kfree(kbuf);
2197 		return rc >= 0 ? -EIO : rc;
2198 	}
2199 
2200 	kbuf[len] = '\0';
2201 	rc = sscanf(kbuf, "%d %d %d %d %d %d",
2202 		    &led_blink_time[WIL_LED_TIME_SLOW].on_ms,
2203 		    &led_blink_time[WIL_LED_TIME_SLOW].off_ms,
2204 		    &led_blink_time[WIL_LED_TIME_MED].on_ms,
2205 		    &led_blink_time[WIL_LED_TIME_MED].off_ms,
2206 		    &led_blink_time[WIL_LED_TIME_FAST].on_ms,
2207 		    &led_blink_time[WIL_LED_TIME_FAST].off_ms);
2208 	kfree(kbuf);
2209 
2210 	if (rc < 0)
2211 		return rc;
2212 	if (rc < 6)
2213 		return -EINVAL;
2214 
2215 	return len;
2216 }
2217 
2218 static ssize_t wil_read_led_blink_time(struct file *file, char __user *user_buf,
2219 				       size_t count, loff_t *ppos)
2220 {
2221 	static char text[400];
2222 
2223 	snprintf(text, sizeof(text),
2224 		 "To set led blink on/off time variables write:\n"
2225 		 "<blink_on_slow> <blink_off_slow> <blink_on_med> "
2226 		 "<blink_off_med> <blink_on_fast> <blink_off_fast>\n"
2227 		 "The current values are:\n"
2228 		 "%d %d %d %d %d %d\n",
2229 		 led_blink_time[WIL_LED_TIME_SLOW].on_ms,
2230 		 led_blink_time[WIL_LED_TIME_SLOW].off_ms,
2231 		 led_blink_time[WIL_LED_TIME_MED].on_ms,
2232 		 led_blink_time[WIL_LED_TIME_MED].off_ms,
2233 		 led_blink_time[WIL_LED_TIME_FAST].on_ms,
2234 		 led_blink_time[WIL_LED_TIME_FAST].off_ms);
2235 
2236 	return simple_read_from_buffer(user_buf, count, ppos, text,
2237 				       sizeof(text));
2238 }
2239 
2240 static const struct file_operations fops_led_blink_time = {
2241 	.read = wil_read_led_blink_time,
2242 	.write = wil_write_led_blink_time,
2243 	.open  = simple_open,
2244 };
2245 
2246 /*---------FW capabilities------------*/
2247 static int wil_fw_capabilities_debugfs_show(struct seq_file *s, void *data)
2248 {
2249 	struct wil6210_priv *wil = s->private;
2250 
2251 	seq_printf(s, "fw_capabilities : %*pb\n", WMI_FW_CAPABILITY_MAX,
2252 		   wil->fw_capabilities);
2253 
2254 	return 0;
2255 }
2256 
2257 static int wil_fw_capabilities_seq_open(struct inode *inode, struct file *file)
2258 {
2259 	return single_open(file, wil_fw_capabilities_debugfs_show,
2260 			   inode->i_private);
2261 }
2262 
2263 static const struct file_operations fops_fw_capabilities = {
2264 	.open		= wil_fw_capabilities_seq_open,
2265 	.release	= single_release,
2266 	.read		= seq_read,
2267 	.llseek		= seq_lseek,
2268 };
2269 
2270 /*---------FW version------------*/
2271 static int wil_fw_version_debugfs_show(struct seq_file *s, void *data)
2272 {
2273 	struct wil6210_priv *wil = s->private;
2274 
2275 	if (wil->fw_version[0])
2276 		seq_printf(s, "%s\n", wil->fw_version);
2277 	else
2278 		seq_puts(s, "N/A\n");
2279 
2280 	return 0;
2281 }
2282 
2283 static int wil_fw_version_seq_open(struct inode *inode, struct file *file)
2284 {
2285 	return single_open(file, wil_fw_version_debugfs_show,
2286 			   inode->i_private);
2287 }
2288 
2289 static const struct file_operations fops_fw_version = {
2290 	.open		= wil_fw_version_seq_open,
2291 	.release	= single_release,
2292 	.read		= seq_read,
2293 	.llseek		= seq_lseek,
2294 };
2295 
2296 /*---------suspend_stats---------*/
2297 static ssize_t wil_write_suspend_stats(struct file *file,
2298 				       const char __user *buf,
2299 				       size_t len, loff_t *ppos)
2300 {
2301 	struct wil6210_priv *wil = file->private_data;
2302 
2303 	memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
2304 
2305 	return len;
2306 }
2307 
2308 static ssize_t wil_read_suspend_stats(struct file *file,
2309 				      char __user *user_buf,
2310 				      size_t count, loff_t *ppos)
2311 {
2312 	struct wil6210_priv *wil = file->private_data;
2313 	char *text;
2314 	int n, ret, text_size = 500;
2315 
2316 	text = kmalloc(text_size, GFP_KERNEL);
2317 	if (!text)
2318 		return -ENOMEM;
2319 
2320 	n = snprintf(text, text_size,
2321 		     "Radio on suspend statistics:\n"
2322 		     "successful suspends:%ld failed suspends:%ld\n"
2323 		     "successful resumes:%ld failed resumes:%ld\n"
2324 		     "rejected by device:%ld\n"
2325 		     "Radio off suspend statistics:\n"
2326 		     "successful suspends:%ld failed suspends:%ld\n"
2327 		     "successful resumes:%ld failed resumes:%ld\n"
2328 		     "General statistics:\n"
2329 		     "rejected by host:%ld\n",
2330 		     wil->suspend_stats.r_on.successful_suspends,
2331 		     wil->suspend_stats.r_on.failed_suspends,
2332 		     wil->suspend_stats.r_on.successful_resumes,
2333 		     wil->suspend_stats.r_on.failed_resumes,
2334 		     wil->suspend_stats.rejected_by_device,
2335 		     wil->suspend_stats.r_off.successful_suspends,
2336 		     wil->suspend_stats.r_off.failed_suspends,
2337 		     wil->suspend_stats.r_off.successful_resumes,
2338 		     wil->suspend_stats.r_off.failed_resumes,
2339 		     wil->suspend_stats.rejected_by_host);
2340 
2341 	n = min_t(int, n, text_size);
2342 
2343 	ret = simple_read_from_buffer(user_buf, count, ppos, text, n);
2344 
2345 	kfree(text);
2346 
2347 	return ret;
2348 }
2349 
2350 static const struct file_operations fops_suspend_stats = {
2351 	.read = wil_read_suspend_stats,
2352 	.write = wil_write_suspend_stats,
2353 	.open  = simple_open,
2354 };
2355 
2356 /*---------compressed_rx_status---------*/
2357 static ssize_t wil_compressed_rx_status_write(struct file *file,
2358 					      const char __user *buf,
2359 					      size_t len, loff_t *ppos)
2360 {
2361 	struct seq_file *s = file->private_data;
2362 	struct wil6210_priv *wil = s->private;
2363 	int compressed_rx_status;
2364 	int rc;
2365 
2366 	rc = kstrtoint_from_user(buf, len, 0, &compressed_rx_status);
2367 	if (rc) {
2368 		wil_err(wil, "Invalid argument\n");
2369 		return rc;
2370 	}
2371 
2372 	if (wil_has_active_ifaces(wil, true, false)) {
2373 		wil_err(wil, "cannot change edma config after iface is up\n");
2374 		return -EPERM;
2375 	}
2376 
2377 	wil_info(wil, "%sable compressed_rx_status\n",
2378 		 compressed_rx_status ? "En" : "Dis");
2379 
2380 	wil->use_compressed_rx_status = compressed_rx_status;
2381 
2382 	return len;
2383 }
2384 
2385 static int
2386 wil_compressed_rx_status_show(struct seq_file *s, void *data)
2387 {
2388 	struct wil6210_priv *wil = s->private;
2389 
2390 	seq_printf(s, "%d\n", wil->use_compressed_rx_status);
2391 
2392 	return 0;
2393 }
2394 
2395 static int
2396 wil_compressed_rx_status_seq_open(struct inode *inode, struct file *file)
2397 {
2398 	return single_open(file, wil_compressed_rx_status_show,
2399 			   inode->i_private);
2400 }
2401 
2402 static const struct file_operations fops_compressed_rx_status = {
2403 	.open  = wil_compressed_rx_status_seq_open,
2404 	.release = single_release,
2405 	.read = seq_read,
2406 	.write = wil_compressed_rx_status_write,
2407 	.llseek	= seq_lseek,
2408 };
2409 
2410 /*----------------*/
2411 static void wil6210_debugfs_init_blobs(struct wil6210_priv *wil,
2412 				       struct dentry *dbg)
2413 {
2414 	int i;
2415 	char name[32];
2416 
2417 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
2418 		struct wil_blob_wrapper *wil_blob = &wil->blobs[i];
2419 		struct debugfs_blob_wrapper *blob = &wil_blob->blob;
2420 		const struct fw_map *map = &fw_mapping[i];
2421 
2422 		if (!map->name)
2423 			continue;
2424 
2425 		wil_blob->wil = wil;
2426 		blob->data = (void * __force)wil->csr + HOSTADDR(map->host);
2427 		blob->size = map->to - map->from;
2428 		snprintf(name, sizeof(name), "blob_%s", map->name);
2429 		wil_debugfs_create_ioblob(name, 0444, dbg, wil_blob);
2430 	}
2431 }
2432 
2433 /* misc files */
2434 static const struct {
2435 	const char *name;
2436 	umode_t mode;
2437 	const struct file_operations *fops;
2438 } dbg_files[] = {
2439 	{"mbox",	0444,		&fops_mbox},
2440 	{"rings",	0444,		&fops_ring},
2441 	{"stations", 0444,		&fops_sta},
2442 	{"mids",	0444,		&fops_mids},
2443 	{"desc",	0444,		&fops_txdesc},
2444 	{"bf",		0444,		&fops_bf},
2445 	{"mem_val",	0644,		&fops_memread},
2446 	{"rxon",	0244,		&fops_rxon},
2447 	{"tx_mgmt",	0244,		&fops_txmgmt},
2448 	{"wmi_send", 0244,		&fops_wmi},
2449 	{"back",	0644,		&fops_back},
2450 	{"pmccfg",	0644,		&fops_pmccfg},
2451 	{"pmcdata",	0444,		&fops_pmcdata},
2452 	{"temp",	0444,		&fops_temp},
2453 	{"freq",	0444,		&fops_freq},
2454 	{"link",	0444,		&fops_link},
2455 	{"info",	0444,		&fops_info},
2456 	{"recovery", 0644,		&fops_recovery},
2457 	{"led_cfg",	0644,		&fops_led_cfg},
2458 	{"led_blink_time",	0644,	&fops_led_blink_time},
2459 	{"fw_capabilities",	0444,	&fops_fw_capabilities},
2460 	{"fw_version",	0444,		&fops_fw_version},
2461 	{"suspend_stats",	0644,	&fops_suspend_stats},
2462 	{"compressed_rx_status", 0644,	&fops_compressed_rx_status},
2463 	{"srings",	0444,		&fops_srings},
2464 	{"status_msg",	0444,		&fops_status_msg},
2465 	{"rx_buff_mgmt",	0444,	&fops_rx_buff_mgmt},
2466 	{"tx_latency",	0644,		&fops_tx_latency},
2467 	{"link_stats",	0644,		&fops_link_stats},
2468 	{"link_stats_global",	0644,	&fops_link_stats_global},
2469 };
2470 
2471 static void wil6210_debugfs_init_files(struct wil6210_priv *wil,
2472 				       struct dentry *dbg)
2473 {
2474 	int i;
2475 
2476 	for (i = 0; i < ARRAY_SIZE(dbg_files); i++)
2477 		debugfs_create_file(dbg_files[i].name, dbg_files[i].mode, dbg,
2478 				    wil, dbg_files[i].fops);
2479 }
2480 
2481 /* interrupt control blocks */
2482 static const struct {
2483 	const char *name;
2484 	u32 icr_off;
2485 } dbg_icr[] = {
2486 	{"USER_ICR",		HOSTADDR(RGF_USER_USER_ICR)},
2487 	{"DMA_EP_TX_ICR",	HOSTADDR(RGF_DMA_EP_TX_ICR)},
2488 	{"DMA_EP_RX_ICR",	HOSTADDR(RGF_DMA_EP_RX_ICR)},
2489 	{"DMA_EP_MISC_ICR",	HOSTADDR(RGF_DMA_EP_MISC_ICR)},
2490 };
2491 
2492 static void wil6210_debugfs_init_isr(struct wil6210_priv *wil,
2493 				     struct dentry *dbg)
2494 {
2495 	int i;
2496 
2497 	for (i = 0; i < ARRAY_SIZE(dbg_icr); i++)
2498 		wil6210_debugfs_create_ISR(wil, dbg_icr[i].name, dbg,
2499 					   dbg_icr[i].icr_off);
2500 }
2501 
2502 #define WIL_FIELD(name, mode, type) { __stringify(name), mode, \
2503 	offsetof(struct wil6210_priv, name), type}
2504 
2505 /* fields in struct wil6210_priv */
2506 static const struct dbg_off dbg_wil_off[] = {
2507 	WIL_FIELD(status[0],	0644,	doff_ulong),
2508 	WIL_FIELD(hw_version,	0444,	doff_x32),
2509 	WIL_FIELD(recovery_count, 0444,	doff_u32),
2510 	WIL_FIELD(discovery_mode, 0644,	doff_u8),
2511 	WIL_FIELD(chip_revision, 0444,	doff_u8),
2512 	WIL_FIELD(abft_len, 0644,		doff_u8),
2513 	WIL_FIELD(wakeup_trigger, 0644,		doff_u8),
2514 	WIL_FIELD(ring_idle_trsh, 0644,	doff_u32),
2515 	WIL_FIELD(num_rx_status_rings, 0644,	doff_u8),
2516 	WIL_FIELD(rx_status_ring_order, 0644,	doff_u32),
2517 	WIL_FIELD(tx_status_ring_order, 0644,	doff_u32),
2518 	WIL_FIELD(rx_buff_id_count, 0644,	doff_u32),
2519 	WIL_FIELD(amsdu_en, 0644,	doff_u8),
2520 	{},
2521 };
2522 
2523 static const struct dbg_off dbg_wil_regs[] = {
2524 	{"RGF_MAC_MTRL_COUNTER_0", 0444, HOSTADDR(RGF_MAC_MTRL_COUNTER_0),
2525 		doff_io32},
2526 	{"RGF_USER_USAGE_1", 0444, HOSTADDR(RGF_USER_USAGE_1), doff_io32},
2527 	{},
2528 };
2529 
2530 /* static parameters */
2531 static const struct dbg_off dbg_statics[] = {
2532 	{"desc_index",	0644, (ulong)&dbg_txdesc_index, doff_u32},
2533 	{"ring_index",	0644, (ulong)&dbg_ring_index, doff_u32},
2534 	{"mem_addr",	0644, (ulong)&mem_addr, doff_u32},
2535 	{"led_polarity", 0644, (ulong)&led_polarity, doff_u8},
2536 	{"status_index", 0644, (ulong)&dbg_status_msg_index, doff_u32},
2537 	{"sring_index",	0644, (ulong)&dbg_sring_index, doff_u32},
2538 	{},
2539 };
2540 
2541 static const int dbg_off_count = 4 * (ARRAY_SIZE(isr_off) - 1) +
2542 				ARRAY_SIZE(dbg_wil_regs) - 1 +
2543 				ARRAY_SIZE(pseudo_isr_off) - 1 +
2544 				ARRAY_SIZE(lgc_itr_cnt_off) - 1 +
2545 				ARRAY_SIZE(tx_itr_cnt_off) - 1 +
2546 				ARRAY_SIZE(rx_itr_cnt_off) - 1;
2547 
2548 int wil6210_debugfs_init(struct wil6210_priv *wil)
2549 {
2550 	struct dentry *dbg = wil->debug = debugfs_create_dir(WIL_NAME,
2551 			wil_to_wiphy(wil)->debugfsdir);
2552 	if (IS_ERR_OR_NULL(dbg))
2553 		return -ENODEV;
2554 
2555 	wil->dbg_data.data_arr = kcalloc(dbg_off_count,
2556 					 sizeof(struct wil_debugfs_iomem_data),
2557 					 GFP_KERNEL);
2558 	if (!wil->dbg_data.data_arr) {
2559 		debugfs_remove_recursive(dbg);
2560 		wil->debug = NULL;
2561 		return -ENOMEM;
2562 	}
2563 
2564 	wil->dbg_data.iomem_data_count = 0;
2565 
2566 	wil_pmc_init(wil);
2567 
2568 	wil6210_debugfs_init_files(wil, dbg);
2569 	wil6210_debugfs_init_isr(wil, dbg);
2570 	wil6210_debugfs_init_blobs(wil, dbg);
2571 	wil6210_debugfs_init_offset(wil, dbg, wil, dbg_wil_off);
2572 	wil6210_debugfs_init_offset(wil, dbg, (void * __force)wil->csr,
2573 				    dbg_wil_regs);
2574 	wil6210_debugfs_init_offset(wil, dbg, NULL, dbg_statics);
2575 
2576 	wil6210_debugfs_create_pseudo_ISR(wil, dbg);
2577 
2578 	wil6210_debugfs_create_ITR_CNT(wil, dbg);
2579 
2580 	return 0;
2581 }
2582 
2583 void wil6210_debugfs_remove(struct wil6210_priv *wil)
2584 {
2585 	int i;
2586 
2587 	debugfs_remove_recursive(wil->debug);
2588 	wil->debug = NULL;
2589 
2590 	kfree(wil->dbg_data.data_arr);
2591 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++)
2592 		kfree(wil->sta[i].tx_latency_bins);
2593 
2594 	/* free pmc memory without sending command to fw, as it will
2595 	 * be reset on the way down anyway
2596 	 */
2597 	wil_pmc_free(wil, false);
2598 }
2599