blob: f5c8e4eb62ae61fdea8e2312c9c40ae3094e08fe [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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;
Olivier Deprez0e641232021-09-23 10:07:05 +0200200 char str[XHCI_MSG_MAX];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000201
202 for (i = 0; i < TRBS_PER_SEGMENT; i++) {
203 trb = &seg->trbs[i];
204 dma = seg->dma + i * sizeof(*trb);
205 seq_printf(s, "%pad: %s\n", &dma,
Olivier Deprez0e641232021-09-23 10:07:05 +0200206 xhci_decode_trb(str, XHCI_MSG_MAX, le32_to_cpu(trb->generic.field[0]),
David Brazdil0f672f62019-12-10 10:32:29 +0000207 le32_to_cpu(trb->generic.field[1]),
208 le32_to_cpu(trb->generic.field[2]),
209 le32_to_cpu(trb->generic.field[3])));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210 }
211}
212
213static int xhci_ring_trb_show(struct seq_file *s, void *unused)
214{
215 int i;
216 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
217 struct xhci_segment *seg = ring->first_seg;
218
219 for (i = 0; i < ring->num_segs; i++) {
220 xhci_ring_dump_segment(s, seg);
221 seg = seg->next;
222 }
223
224 return 0;
225}
226
227static struct xhci_file_map ring_files[] = {
228 {"enqueue", xhci_ring_enqueue_show, },
229 {"dequeue", xhci_ring_dequeue_show, },
230 {"cycle", xhci_ring_cycle_show, },
231 {"trbs", xhci_ring_trb_show, },
232};
233
234static int xhci_ring_open(struct inode *inode, struct file *file)
235{
236 int i;
237 struct xhci_file_map *f_map;
238 const char *file_name = file_dentry(file)->d_iname;
239
240 for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
241 f_map = &ring_files[i];
242
243 if (strcmp(f_map->name, file_name) == 0)
244 break;
245 }
246
247 return single_open(file, f_map->show, inode->i_private);
248}
249
250static const struct file_operations xhci_ring_fops = {
251 .open = xhci_ring_open,
252 .read = seq_read,
253 .llseek = seq_lseek,
254 .release = single_release,
255};
256
257static int xhci_slot_context_show(struct seq_file *s, void *unused)
258{
259 struct xhci_hcd *xhci;
260 struct xhci_slot_ctx *slot_ctx;
261 struct xhci_slot_priv *priv = s->private;
262 struct xhci_virt_device *dev = priv->dev;
263
264 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
265 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
266 seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
David Brazdil0f672f62019-12-10 10:32:29 +0000267 xhci_decode_slot_context(le32_to_cpu(slot_ctx->dev_info),
268 le32_to_cpu(slot_ctx->dev_info2),
269 le32_to_cpu(slot_ctx->tt_info),
270 le32_to_cpu(slot_ctx->dev_state)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271
272 return 0;
273}
274
275static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
276{
Olivier Deprez0e641232021-09-23 10:07:05 +0200277 int ep_index;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278 dma_addr_t dma;
279 struct xhci_hcd *xhci;
280 struct xhci_ep_ctx *ep_ctx;
281 struct xhci_slot_priv *priv = s->private;
282 struct xhci_virt_device *dev = priv->dev;
283
284 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
285
Olivier Deprez0e641232021-09-23 10:07:05 +0200286 for (ep_index = 0; ep_index < 31; ep_index++) {
287 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
288 dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289 seq_printf(s, "%pad: %s\n", &dma,
David Brazdil0f672f62019-12-10 10:32:29 +0000290 xhci_decode_ep_context(le32_to_cpu(ep_ctx->ep_info),
291 le32_to_cpu(ep_ctx->ep_info2),
292 le64_to_cpu(ep_ctx->deq),
293 le32_to_cpu(ep_ctx->tx_info)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000294 }
295
296 return 0;
297}
298
299static int xhci_device_name_show(struct seq_file *s, void *unused)
300{
301 struct xhci_slot_priv *priv = s->private;
302 struct xhci_virt_device *dev = priv->dev;
303
304 seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
305
306 return 0;
307}
308
309static struct xhci_file_map context_files[] = {
310 {"name", xhci_device_name_show, },
311 {"slot-context", xhci_slot_context_show, },
312 {"ep-context", xhci_endpoint_context_show, },
313};
314
315static int xhci_context_open(struct inode *inode, struct file *file)
316{
317 int i;
318 struct xhci_file_map *f_map;
319 const char *file_name = file_dentry(file)->d_iname;
320
321 for (i = 0; i < ARRAY_SIZE(context_files); i++) {
322 f_map = &context_files[i];
323
324 if (strcmp(f_map->name, file_name) == 0)
325 break;
326 }
327
328 return single_open(file, f_map->show, inode->i_private);
329}
330
331static const struct file_operations xhci_context_fops = {
332 .open = xhci_context_open,
333 .read = seq_read,
334 .llseek = seq_lseek,
335 .release = single_release,
336};
337
338
339
340static int xhci_portsc_show(struct seq_file *s, void *unused)
341{
342 struct xhci_port *port = s->private;
343 u32 portsc;
Olivier Deprez0e641232021-09-23 10:07:05 +0200344 char str[XHCI_MSG_MAX];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345
346 portsc = readl(port->addr);
Olivier Deprez0e641232021-09-23 10:07:05 +0200347 seq_printf(s, "%s\n", xhci_decode_portsc(str, portsc));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348
349 return 0;
350}
351
352static int xhci_port_open(struct inode *inode, struct file *file)
353{
354 return single_open(file, xhci_portsc_show, inode->i_private);
355}
356
357static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,
358 size_t count, loff_t *ppos)
359{
360 struct seq_file *s = file->private_data;
361 struct xhci_port *port = s->private;
362 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
363 char buf[32];
364 u32 portsc;
365 unsigned long flags;
366
367 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
368 return -EFAULT;
369
370 if (!strncmp(buf, "compliance", 10)) {
371 /* If CTC is clear, compliance is enabled by default */
372 if (!HCC2_CTC(xhci->hcc_params2))
373 return count;
374 spin_lock_irqsave(&xhci->lock, flags);
375 /* compliance mode can only be enabled on ports in RxDetect */
376 portsc = readl(port->addr);
377 if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
378 spin_unlock_irqrestore(&xhci->lock, flags);
379 return -EPERM;
380 }
381 portsc = xhci_port_state_to_neutral(portsc);
382 portsc &= ~PORT_PLS_MASK;
383 portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
384 writel(portsc, port->addr);
385 spin_unlock_irqrestore(&xhci->lock, flags);
386 } else {
387 return -EINVAL;
388 }
389 return count;
390}
391
392static const struct file_operations port_fops = {
393 .open = xhci_port_open,
394 .write = xhci_port_write,
395 .read = seq_read,
396 .llseek = seq_lseek,
397 .release = single_release,
398};
399
400static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
401 struct xhci_file_map *files,
402 size_t nentries, void *data,
403 struct dentry *parent,
404 const struct file_operations *fops)
405{
406 int i;
407
408 for (i = 0; i < nentries; i++)
409 debugfs_create_file(files[i].name, 0444, parent, data, fops);
410}
411
412static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
413 struct xhci_ring **ring,
414 const char *name,
415 struct dentry *parent)
416{
417 struct dentry *dir;
418
419 dir = debugfs_create_dir(name, parent);
420 xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
421 ring, dir, &xhci_ring_fops);
422
423 return dir;
424}
425
426static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
427 struct dentry *parent,
428 int slot_id)
429{
430 struct xhci_virt_device *dev = xhci->devs[slot_id];
431
432 xhci_debugfs_create_files(xhci, context_files,
433 ARRAY_SIZE(context_files),
434 dev->debugfs_private,
435 parent, &xhci_context_fops);
436}
437
438void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
439 struct xhci_virt_device *dev,
440 int ep_index)
441{
442 struct xhci_ep_priv *epriv;
443 struct xhci_slot_priv *spriv = dev->debugfs_private;
444
David Brazdil0f672f62019-12-10 10:32:29 +0000445 if (!spriv)
446 return;
447
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448 if (spriv->eps[ep_index])
449 return;
450
451 epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
452 if (!epriv)
453 return;
454
455 snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
456 epriv->root = xhci_debugfs_create_ring_dir(xhci,
457 &dev->eps[ep_index].ring,
458 epriv->name,
459 spriv->root);
460 spriv->eps[ep_index] = epriv;
461}
462
463void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
464 struct xhci_virt_device *dev,
465 int ep_index)
466{
467 struct xhci_ep_priv *epriv;
468 struct xhci_slot_priv *spriv = dev->debugfs_private;
469
470 if (!spriv || !spriv->eps[ep_index])
471 return;
472
473 epriv = spriv->eps[ep_index];
474 debugfs_remove_recursive(epriv->root);
475 spriv->eps[ep_index] = NULL;
476 kfree(epriv);
477}
478
479void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
480{
481 struct xhci_slot_priv *priv;
482 struct xhci_virt_device *dev = xhci->devs[slot_id];
483
484 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
485 if (!priv)
486 return;
487
488 snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
489 priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
490 priv->dev = dev;
491 dev->debugfs_private = priv;
492
493 xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
494 "ep00", priv->root);
495
496 xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
497}
498
499void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
500{
501 int i;
502 struct xhci_slot_priv *priv;
503 struct xhci_virt_device *dev = xhci->devs[slot_id];
504
505 if (!dev || !dev->debugfs_private)
506 return;
507
508 priv = dev->debugfs_private;
509
510 debugfs_remove_recursive(priv->root);
511
512 for (i = 0; i < 31; i++)
513 kfree(priv->eps[i]);
514
515 kfree(priv);
516 dev->debugfs_private = NULL;
517}
518
519static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
520 struct dentry *parent)
521{
522 unsigned int num_ports;
523 char port_name[8];
524 struct xhci_port *port;
525 struct dentry *dir;
526
527 num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
528
529 parent = debugfs_create_dir("ports", parent);
530
531 while (num_ports--) {
532 scnprintf(port_name, sizeof(port_name), "port%02d",
533 num_ports + 1);
534 dir = debugfs_create_dir(port_name, parent);
535 port = &xhci->hw_ports[num_ports];
536 debugfs_create_file("portsc", 0644, dir, port, &port_fops);
537 }
538}
539
540void xhci_debugfs_init(struct xhci_hcd *xhci)
541{
542 struct device *dev = xhci_to_hcd(xhci)->self.controller;
543
544 xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
545 xhci_debugfs_root);
546
547 INIT_LIST_HEAD(&xhci->regset_list);
548
549 xhci_debugfs_regset(xhci,
550 0,
551 xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
552 xhci->debugfs_root, "reg-cap");
553
554 xhci_debugfs_regset(xhci,
555 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
556 xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
557 xhci->debugfs_root, "reg-op");
558
559 xhci_debugfs_regset(xhci,
560 readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
561 xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
562 xhci->debugfs_root, "reg-runtime");
563
564 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
565 xhci_extcap_legsup,
566 ARRAY_SIZE(xhci_extcap_legsup),
567 "reg-ext-legsup");
568
569 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
570 xhci_extcap_protocol,
571 ARRAY_SIZE(xhci_extcap_protocol),
572 "reg-ext-protocol");
573
574 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
575 xhci_extcap_dbc,
576 ARRAY_SIZE(xhci_extcap_dbc),
577 "reg-ext-dbc");
578
579 xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
580 "command-ring",
581 xhci->debugfs_root);
582
583 xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
584 "event-ring",
585 xhci->debugfs_root);
586
587 xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
588
589 xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
590}
591
592void xhci_debugfs_exit(struct xhci_hcd *xhci)
593{
594 struct xhci_regset *rgs, *tmp;
595
596 debugfs_remove_recursive(xhci->debugfs_root);
597 xhci->debugfs_root = NULL;
598 xhci->debugfs_slots = NULL;
599
600 list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
601 xhci_debugfs_free_regset(rgs);
602}
603
604void __init xhci_debugfs_create_root(void)
605{
606 xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
607}
608
609void __exit xhci_debugfs_remove_root(void)
610{
611 debugfs_remove_recursive(xhci_debugfs_root);
612 xhci_debugfs_root = NULL;
613}