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