blob: a136b1b77eac0114870a7a869026521f4c0a4777 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * xhci-debugfs.c - xHCI debugfs interface
4 *
5 * Copyright (C) 2017 Intel Corporation
6 *
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 */
9
10#include <linux/slab.h>
11#include <linux/uaccess.h>
12
13#include "xhci.h"
14#include "xhci-debugfs.h"
15
16static const struct debugfs_reg32 xhci_cap_regs[] = {
17 dump_register(CAPLENGTH),
18 dump_register(HCSPARAMS1),
19 dump_register(HCSPARAMS2),
20 dump_register(HCSPARAMS3),
21 dump_register(HCCPARAMS1),
22 dump_register(DOORBELLOFF),
23 dump_register(RUNTIMEOFF),
24 dump_register(HCCPARAMS2),
25};
26
27static const struct debugfs_reg32 xhci_op_regs[] = {
28 dump_register(USBCMD),
29 dump_register(USBSTS),
30 dump_register(PAGESIZE),
31 dump_register(DNCTRL),
32 dump_register(CRCR),
33 dump_register(DCBAAP_LOW),
34 dump_register(DCBAAP_HIGH),
35 dump_register(CONFIG),
36};
37
38static const struct debugfs_reg32 xhci_runtime_regs[] = {
39 dump_register(MFINDEX),
40 dump_register(IR0_IMAN),
41 dump_register(IR0_IMOD),
42 dump_register(IR0_ERSTSZ),
43 dump_register(IR0_ERSTBA_LOW),
44 dump_register(IR0_ERSTBA_HIGH),
45 dump_register(IR0_ERDP_LOW),
46 dump_register(IR0_ERDP_HIGH),
47};
48
49static const struct debugfs_reg32 xhci_extcap_legsup[] = {
50 dump_register(EXTCAP_USBLEGSUP),
51 dump_register(EXTCAP_USBLEGCTLSTS),
52};
53
54static const struct debugfs_reg32 xhci_extcap_protocol[] = {
55 dump_register(EXTCAP_REVISION),
56 dump_register(EXTCAP_NAME),
57 dump_register(EXTCAP_PORTINFO),
58 dump_register(EXTCAP_PORTTYPE),
59 dump_register(EXTCAP_MANTISSA1),
60 dump_register(EXTCAP_MANTISSA2),
61 dump_register(EXTCAP_MANTISSA3),
62 dump_register(EXTCAP_MANTISSA4),
63 dump_register(EXTCAP_MANTISSA5),
64 dump_register(EXTCAP_MANTISSA6),
65};
66
67static const struct debugfs_reg32 xhci_extcap_dbc[] = {
68 dump_register(EXTCAP_DBC_CAPABILITY),
69 dump_register(EXTCAP_DBC_DOORBELL),
70 dump_register(EXTCAP_DBC_ERSTSIZE),
71 dump_register(EXTCAP_DBC_ERST_LOW),
72 dump_register(EXTCAP_DBC_ERST_HIGH),
73 dump_register(EXTCAP_DBC_ERDP_LOW),
74 dump_register(EXTCAP_DBC_ERDP_HIGH),
75 dump_register(EXTCAP_DBC_CONTROL),
76 dump_register(EXTCAP_DBC_STATUS),
77 dump_register(EXTCAP_DBC_PORTSC),
78 dump_register(EXTCAP_DBC_CONT_LOW),
79 dump_register(EXTCAP_DBC_CONT_HIGH),
80 dump_register(EXTCAP_DBC_DEVINFO1),
81 dump_register(EXTCAP_DBC_DEVINFO2),
82};
83
84static struct dentry *xhci_debugfs_root;
85
86static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
87{
88 struct xhci_regset *regset;
89
90 regset = kzalloc(sizeof(*regset), GFP_KERNEL);
91 if (!regset)
92 return NULL;
93
94 /*
95 * The allocation and free of regset are executed in order.
96 * We needn't a lock here.
97 */
98 INIT_LIST_HEAD(&regset->list);
99 list_add_tail(&regset->list, &xhci->regset_list);
100
101 return regset;
102}
103
104static void xhci_debugfs_free_regset(struct xhci_regset *regset)
105{
106 if (!regset)
107 return;
108
109 list_del(&regset->list);
110 kfree(regset);
111}
112
113static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
114 const struct debugfs_reg32 *regs,
115 size_t nregs, struct dentry *parent,
116 const char *fmt, ...)
117{
118 struct xhci_regset *rgs;
119 va_list args;
120 struct debugfs_regset32 *regset;
121 struct usb_hcd *hcd = xhci_to_hcd(xhci);
122
123 rgs = xhci_debugfs_alloc_regset(xhci);
124 if (!rgs)
125 return;
126
127 va_start(args, fmt);
128 vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
129 va_end(args);
130
131 regset = &rgs->regset;
132 regset->regs = regs;
133 regset->nregs = nregs;
134 regset->base = hcd->regs + base;
135
136 debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
137}
138
139static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
140 const struct debugfs_reg32 *regs,
141 size_t n, const char *cap_name)
142{
143 u32 offset;
144 int index = 0;
145 size_t psic, nregs = n;
146 void __iomem *base = &xhci->cap_regs->hc_capbase;
147
148 offset = xhci_find_next_ext_cap(base, 0, cap_id);
149 while (offset) {
150 if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
151 psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
152 nregs = min(4 + psic, n);
153 }
154
155 xhci_debugfs_regset(xhci, offset, regs, nregs,
156 xhci->debugfs_root, "%s:%02d",
157 cap_name, index);
158 offset = xhci_find_next_ext_cap(base, offset, cap_id);
159 index++;
160 }
161}
162
163static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
164{
165 dma_addr_t dma;
166 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
167
168 dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
169 seq_printf(s, "%pad\n", &dma);
170
171 return 0;
172}
173
174static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
175{
176 dma_addr_t dma;
177 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
178
179 dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
180 seq_printf(s, "%pad\n", &dma);
181
182 return 0;
183}
184
185static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
186{
187 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
188
189 seq_printf(s, "%d\n", ring->cycle_state);
190
191 return 0;
192}
193
194static void xhci_ring_dump_segment(struct seq_file *s,
195 struct xhci_segment *seg)
196{
197 int i;
198 dma_addr_t dma;
199 union xhci_trb *trb;
200
201 for (i = 0; i < TRBS_PER_SEGMENT; i++) {
202 trb = &seg->trbs[i];
203 dma = seg->dma + i * sizeof(*trb);
204 seq_printf(s, "%pad: %s\n", &dma,
205 xhci_decode_trb(le32_to_cpu(trb->generic.field[0]),
206 le32_to_cpu(trb->generic.field[1]),
207 le32_to_cpu(trb->generic.field[2]),
208 le32_to_cpu(trb->generic.field[3])));
209 }
210}
211
212static int xhci_ring_trb_show(struct seq_file *s, void *unused)
213{
214 int i;
215 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
216 struct xhci_segment *seg = ring->first_seg;
217
218 for (i = 0; i < ring->num_segs; i++) {
219 xhci_ring_dump_segment(s, seg);
220 seg = seg->next;
221 }
222
223 return 0;
224}
225
226static struct xhci_file_map ring_files[] = {
227 {"enqueue", xhci_ring_enqueue_show, },
228 {"dequeue", xhci_ring_dequeue_show, },
229 {"cycle", xhci_ring_cycle_show, },
230 {"trbs", xhci_ring_trb_show, },
231};
232
233static int xhci_ring_open(struct inode *inode, struct file *file)
234{
235 int i;
236 struct xhci_file_map *f_map;
237 const char *file_name = file_dentry(file)->d_iname;
238
239 for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
240 f_map = &ring_files[i];
241
242 if (strcmp(f_map->name, file_name) == 0)
243 break;
244 }
245
246 return single_open(file, f_map->show, inode->i_private);
247}
248
249static const struct file_operations xhci_ring_fops = {
250 .open = xhci_ring_open,
251 .read = seq_read,
252 .llseek = seq_lseek,
253 .release = single_release,
254};
255
256static int xhci_slot_context_show(struct seq_file *s, void *unused)
257{
258 struct xhci_hcd *xhci;
259 struct xhci_slot_ctx *slot_ctx;
260 struct xhci_slot_priv *priv = s->private;
261 struct xhci_virt_device *dev = priv->dev;
262
263 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
264 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
265 seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
266 xhci_decode_slot_context(le32_to_cpu(slot_ctx->dev_info),
267 le32_to_cpu(slot_ctx->dev_info2),
268 le32_to_cpu(slot_ctx->tt_info),
269 le32_to_cpu(slot_ctx->dev_state)));
270
271 return 0;
272}
273
274static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
275{
276 int dci;
277 dma_addr_t dma;
278 struct xhci_hcd *xhci;
279 struct xhci_ep_ctx *ep_ctx;
280 struct xhci_slot_priv *priv = s->private;
281 struct xhci_virt_device *dev = priv->dev;
282
283 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
284
285 for (dci = 1; dci < 32; dci++) {
286 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, dci);
287 dma = dev->out_ctx->dma + dci * CTX_SIZE(xhci->hcc_params);
288 seq_printf(s, "%pad: %s\n", &dma,
289 xhci_decode_ep_context(le32_to_cpu(ep_ctx->ep_info),
290 le32_to_cpu(ep_ctx->ep_info2),
291 le64_to_cpu(ep_ctx->deq),
292 le32_to_cpu(ep_ctx->tx_info)));
293 }
294
295 return 0;
296}
297
298static int xhci_device_name_show(struct seq_file *s, void *unused)
299{
300 struct xhci_slot_priv *priv = s->private;
301 struct xhci_virt_device *dev = priv->dev;
302
303 seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
304
305 return 0;
306}
307
308static struct xhci_file_map context_files[] = {
309 {"name", xhci_device_name_show, },
310 {"slot-context", xhci_slot_context_show, },
311 {"ep-context", xhci_endpoint_context_show, },
312};
313
314static int xhci_context_open(struct inode *inode, struct file *file)
315{
316 int i;
317 struct xhci_file_map *f_map;
318 const char *file_name = file_dentry(file)->d_iname;
319
320 for (i = 0; i < ARRAY_SIZE(context_files); i++) {
321 f_map = &context_files[i];
322
323 if (strcmp(f_map->name, file_name) == 0)
324 break;
325 }
326
327 return single_open(file, f_map->show, inode->i_private);
328}
329
330static const struct file_operations xhci_context_fops = {
331 .open = xhci_context_open,
332 .read = seq_read,
333 .llseek = seq_lseek,
334 .release = single_release,
335};
336
337static int xhci_testmode_show(struct seq_file *s, void *unused)
338{
339 struct xhci_port *port = s->private;
340 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
341
342 seq_printf(s, "%d\n", xhci->test_mode);
343
344 return 0;
345}
346
347static int xhci_testmode_open(struct inode *inode, struct file *file)
348{
349 return single_open(file, xhci_testmode_show, inode->i_private);
350}
351
352static ssize_t xhci_testmode_write(struct file *file, const char __user *ubuf,
353 size_t count, loff_t *ppos)
354{
355 struct seq_file *s = file->private_data;
356 struct xhci_port *port = s->private;
357 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
358 char buf[32];
359 unsigned long flags;
360 u8 testmode = 0;
361 u32 temp;
362 u32 __iomem *addr;
363
364 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
365 return -EFAULT;
366
367 if (!strncmp(buf, "test packet", 10))
368 testmode = TEST_PACKET;
369 else if (!strncmp(buf, "test K", 6))
370 testmode = TEST_K;
371 else if (!strncmp(buf, "test J", 6))
372 testmode = TEST_J;
373 else if (!strncmp(buf, "test SE0 NAK", 12))
374 testmode = TEST_SE0_NAK;
375
376 if (testmode) {
377 xhci_info(xhci, "set test mode %d\n", testmode);
378
379 spin_lock_irqsave(&xhci->lock, flags);
380
381 /* set the Run/Stop in USBCMD to 0 */
382 addr = &xhci->op_regs->command;
383 temp = readl(addr);
384 temp &= ~CMD_RUN;
385 writel(temp, addr);
386
387 /* wait for HCHalted */
388 xhci_halt(xhci);
389
390 /* test mode */
391 addr = &xhci->op_regs->port_power_base +
392 NUM_PORT_REGS * (port->hw_portnum & 0xff);
393 temp = readl(addr);
394 temp &= ~(0xf << PORT_TEST_MODE_SHIFT);
395 temp |= (testmode << PORT_TEST_MODE_SHIFT);
396 writel(temp, addr);
397 xhci->test_mode = testmode;
398 spin_unlock_irqrestore(&xhci->lock, flags);
399 } else {
400 return -EINVAL;
401 }
402 return count;
403}
404
405static const struct file_operations testmode_fops = {
406 .open = xhci_testmode_open,
407 .write = xhci_testmode_write,
408 .read = seq_read,
409 .llseek = seq_lseek,
410 .release = single_release,
411};
412
413static int xhci_portsc_show(struct seq_file *s, void *unused)
414{
415 struct xhci_port *port = s->private;
416 u32 portsc;
417
418 portsc = readl(port->addr);
419 seq_printf(s, "%s\n", xhci_decode_portsc(portsc));
420
421 return 0;
422}
423
424static int xhci_port_open(struct inode *inode, struct file *file)
425{
426 return single_open(file, xhci_portsc_show, inode->i_private);
427}
428
429static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,
430 size_t count, loff_t *ppos)
431{
432 struct seq_file *s = file->private_data;
433 struct xhci_port *port = s->private;
434 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
435 char buf[32];
436 u32 portsc;
437 unsigned long flags;
438
439 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
440 return -EFAULT;
441
442 if (!strncmp(buf, "compliance", 10)) {
443 /* If CTC is clear, compliance is enabled by default */
444 if (!HCC2_CTC(xhci->hcc_params2))
445 return count;
446 spin_lock_irqsave(&xhci->lock, flags);
447 /* compliance mode can only be enabled on ports in RxDetect */
448 portsc = readl(port->addr);
449 if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
450 spin_unlock_irqrestore(&xhci->lock, flags);
451 return -EPERM;
452 }
453 portsc = xhci_port_state_to_neutral(portsc);
454 portsc &= ~PORT_PLS_MASK;
455 portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
456 writel(portsc, port->addr);
457 spin_unlock_irqrestore(&xhci->lock, flags);
458 } else {
459 return -EINVAL;
460 }
461 return count;
462}
463
464static const struct file_operations port_fops = {
465 .open = xhci_port_open,
466 .write = xhci_port_write,
467 .read = seq_read,
468 .llseek = seq_lseek,
469 .release = single_release,
470};
471
472static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
473 struct xhci_file_map *files,
474 size_t nentries, void *data,
475 struct dentry *parent,
476 const struct file_operations *fops)
477{
478 int i;
479
480 for (i = 0; i < nentries; i++)
481 debugfs_create_file(files[i].name, 0444, parent, data, fops);
482}
483
484static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
485 struct xhci_ring **ring,
486 const char *name,
487 struct dentry *parent)
488{
489 struct dentry *dir;
490
491 dir = debugfs_create_dir(name, parent);
492 xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
493 ring, dir, &xhci_ring_fops);
494
495 return dir;
496}
497
498static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
499 struct dentry *parent,
500 int slot_id)
501{
502 struct xhci_virt_device *dev = xhci->devs[slot_id];
503
504 xhci_debugfs_create_files(xhci, context_files,
505 ARRAY_SIZE(context_files),
506 dev->debugfs_private,
507 parent, &xhci_context_fops);
508}
509
510void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
511 struct xhci_virt_device *dev,
512 int ep_index)
513{
514 struct xhci_ep_priv *epriv;
515 struct xhci_slot_priv *spriv = dev->debugfs_private;
516
517 if (!spriv)
518 return;
519
520 if (spriv->eps[ep_index])
521 return;
522
523 epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
524 if (!epriv)
525 return;
526
527 snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
528 epriv->root = xhci_debugfs_create_ring_dir(xhci,
529 &dev->eps[ep_index].ring,
530 epriv->name,
531 spriv->root);
532 spriv->eps[ep_index] = epriv;
533}
534
535void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
536 struct xhci_virt_device *dev,
537 int ep_index)
538{
539 struct xhci_ep_priv *epriv;
540 struct xhci_slot_priv *spriv = dev->debugfs_private;
541
542 if (!spriv || !spriv->eps[ep_index])
543 return;
544
545 epriv = spriv->eps[ep_index];
546 debugfs_remove_recursive(epriv->root);
547 spriv->eps[ep_index] = NULL;
548 kfree(epriv);
549}
550
551void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
552{
553 struct xhci_slot_priv *priv;
554 struct xhci_virt_device *dev = xhci->devs[slot_id];
555
556 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
557 if (!priv)
558 return;
559
560 snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
561 priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
562 priv->dev = dev;
563 dev->debugfs_private = priv;
564
565 xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
566 "ep00", priv->root);
567
568 xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
569}
570
571void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
572{
573 int i;
574 struct xhci_slot_priv *priv;
575 struct xhci_virt_device *dev = xhci->devs[slot_id];
576
577 if (!dev || !dev->debugfs_private)
578 return;
579
580 priv = dev->debugfs_private;
581
582 debugfs_remove_recursive(priv->root);
583
584 for (i = 0; i < 31; i++)
585 kfree(priv->eps[i]);
586
587 kfree(priv);
588 dev->debugfs_private = NULL;
589}
590
591static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
592 struct dentry *parent)
593{
594 unsigned int num_ports;
595 char port_name[8];
596 struct xhci_port *port;
597 struct dentry *dir;
598
599 num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
600
601 parent = debugfs_create_dir("ports", parent);
602
603 while (num_ports--) {
604 scnprintf(port_name, sizeof(port_name), "port%02d",
605 num_ports + 1);
606 dir = debugfs_create_dir(port_name, parent);
607 port = &xhci->hw_ports[num_ports];
608 debugfs_create_file("portsc", 0644, dir, port, &port_fops);
609 debugfs_create_file("testmode", 0644, dir, port, &testmode_fops);
610 }
611}
612
613void xhci_debugfs_init(struct xhci_hcd *xhci)
614{
615 struct device *dev = xhci_to_hcd(xhci)->self.controller;
616
617 xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
618 xhci_debugfs_root);
619
620 INIT_LIST_HEAD(&xhci->regset_list);
621
622 xhci_debugfs_regset(xhci,
623 0,
624 xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
625 xhci->debugfs_root, "reg-cap");
626
627 xhci_debugfs_regset(xhci,
628 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
629 xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
630 xhci->debugfs_root, "reg-op");
631
632 xhci_debugfs_regset(xhci,
633 readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
634 xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
635 xhci->debugfs_root, "reg-runtime");
636
637 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
638 xhci_extcap_legsup,
639 ARRAY_SIZE(xhci_extcap_legsup),
640 "reg-ext-legsup");
641
642 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
643 xhci_extcap_protocol,
644 ARRAY_SIZE(xhci_extcap_protocol),
645 "reg-ext-protocol");
646
647 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
648 xhci_extcap_dbc,
649 ARRAY_SIZE(xhci_extcap_dbc),
650 "reg-ext-dbc");
651
652 xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
653 "command-ring",
654 xhci->debugfs_root);
655
656 xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
657 "event-ring",
658 xhci->debugfs_root);
659
660 xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
661
662 xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
663}
664
665void xhci_debugfs_exit(struct xhci_hcd *xhci)
666{
667 struct xhci_regset *rgs, *tmp;
668
669 debugfs_remove_recursive(xhci->debugfs_root);
670 xhci->debugfs_root = NULL;
671 xhci->debugfs_slots = NULL;
672
673 list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
674 xhci_debugfs_free_regset(rgs);
675}
676
677void __init xhci_debugfs_create_root(void)
678{
679 xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
680}
681
682void __exit xhci_debugfs_remove_root(void)
683{
684 debugfs_remove_recursive(xhci_debugfs_root);
685 xhci_debugfs_root = NULL;
686}