1 // 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
16 static 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
27 static 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
38 static 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
49 static const struct debugfs_reg32 xhci_extcap_legsup[] = {
50 dump_register(EXTCAP_USBLEGSUP),
51 dump_register(EXTCAP_USBLEGCTLSTS),
52 };
53
54 static 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
67 static 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
84 static struct dentry *xhci_debugfs_root;
85
xhci_debugfs_alloc_regset(struct xhci_hcd * xhci)86 static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
87 {
88 struct xhci_regset *regset;
89
90 regset = kzalloc_obj(*regset);
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(®set->list);
99 list_add_tail(®set->list, &xhci->regset_list);
100
101 return regset;
102 }
103
xhci_debugfs_free_regset(struct xhci_regset * regset)104 static void xhci_debugfs_free_regset(struct xhci_regset *regset)
105 {
106 if (!regset)
107 return;
108
109 list_del(®set->list);
110 kfree(regset);
111 }
112
113 __printf(6, 7)
xhci_debugfs_regset(struct xhci_hcd * xhci,u32 base,const struct debugfs_reg32 * regs,size_t nregs,struct dentry * parent,const char * fmt,...)114 static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
115 const struct debugfs_reg32 *regs,
116 size_t nregs, struct dentry *parent,
117 const char *fmt, ...)
118 {
119 struct xhci_regset *rgs;
120 va_list args;
121 struct debugfs_regset32 *regset;
122 struct usb_hcd *hcd = xhci_to_hcd(xhci);
123
124 rgs = xhci_debugfs_alloc_regset(xhci);
125 if (!rgs)
126 return;
127
128 va_start(args, fmt);
129 vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
130 va_end(args);
131
132 regset = &rgs->regset;
133 regset->regs = regs;
134 regset->nregs = nregs;
135 regset->base = hcd->regs + base;
136 regset->dev = hcd->self.controller;
137
138 debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
139 }
140
xhci_debugfs_extcap_regset(struct xhci_hcd * xhci,int cap_id,const struct debugfs_reg32 * regs,size_t n,const char * cap_name)141 static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
142 const struct debugfs_reg32 *regs,
143 size_t n, const char *cap_name)
144 {
145 u32 offset;
146 int index = 0;
147 size_t psic, nregs = n;
148 void __iomem *base = &xhci->cap_regs->hc_capbase;
149
150 offset = xhci_find_next_ext_cap(base, 0, cap_id);
151 while (offset) {
152 if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
153 psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
154 nregs = min(4 + psic, n);
155 }
156
157 xhci_debugfs_regset(xhci, offset, regs, nregs,
158 xhci->debugfs_root, "%s:%02d",
159 cap_name, index);
160 offset = xhci_find_next_ext_cap(base, offset, cap_id);
161 index++;
162 }
163 }
164
xhci_ring_enqueue_show(struct seq_file * s,void * unused)165 static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
166 {
167 dma_addr_t dma;
168 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
169
170 dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
171 seq_printf(s, "%pad\n", &dma);
172
173 return 0;
174 }
175
xhci_ring_dequeue_show(struct seq_file * s,void * unused)176 static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
177 {
178 dma_addr_t dma;
179 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
180
181 dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
182 seq_printf(s, "%pad\n", &dma);
183
184 return 0;
185 }
186
xhci_ring_cycle_show(struct seq_file * s,void * unused)187 static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
188 {
189 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
190
191 seq_printf(s, "%d\n", ring->cycle_state);
192
193 return 0;
194 }
195
xhci_ring_dump_segment(struct seq_file * s,struct xhci_segment * seg)196 static void xhci_ring_dump_segment(struct seq_file *s,
197 struct xhci_segment *seg)
198 {
199 int i;
200 dma_addr_t dma;
201 union xhci_trb *trb;
202 char str[XHCI_MSG_MAX];
203
204 for (i = 0; i < TRBS_PER_SEGMENT; i++) {
205 trb = &seg->trbs[i];
206 dma = seg->dma + i * sizeof(*trb);
207 seq_printf(s, "%2u %pad: %s\n", seg->num, &dma,
208 xhci_decode_trb(str, XHCI_MSG_MAX, le32_to_cpu(trb->generic.field[0]),
209 le32_to_cpu(trb->generic.field[1]),
210 le32_to_cpu(trb->generic.field[2]),
211 le32_to_cpu(trb->generic.field[3])));
212 }
213 }
214
xhci_ring_trb_show(struct seq_file * s,void * unused)215 static int xhci_ring_trb_show(struct seq_file *s, void *unused)
216 {
217 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
218 struct xhci_segment *seg = ring->first_seg;
219
220 xhci_for_each_ring_seg(ring->first_seg, seg)
221 xhci_ring_dump_segment(s, seg);
222
223 return 0;
224 }
225
226 static 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
xhci_ring_open(struct inode * inode,struct file * file)233 static int xhci_ring_open(struct inode *inode, struct file *file)
234 {
235 const struct xhci_file_map *f_map = debugfs_get_aux(file);
236
237 return single_open(file, f_map->show, inode->i_private);
238 }
239
240 static const struct file_operations xhci_ring_fops = {
241 .open = xhci_ring_open,
242 .read = seq_read,
243 .llseek = seq_lseek,
244 .release = single_release,
245 };
246
xhci_slot_context_show(struct seq_file * s,void * unused)247 static int xhci_slot_context_show(struct seq_file *s, void *unused)
248 {
249 struct xhci_hcd *xhci;
250 struct xhci_slot_ctx *slot_ctx;
251 struct xhci_slot_priv *priv = s->private;
252 struct xhci_virt_device *dev = priv->dev;
253 char str[XHCI_MSG_MAX];
254
255 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
256 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
257 seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
258 xhci_decode_slot_context(str,
259 le32_to_cpu(slot_ctx->dev_info),
260 le32_to_cpu(slot_ctx->dev_info2),
261 le32_to_cpu(slot_ctx->tt_info),
262 le32_to_cpu(slot_ctx->dev_state)));
263
264 return 0;
265 }
266
xhci_endpoint_context_show(struct seq_file * s,void * unused)267 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
268 {
269 int ep_index;
270 dma_addr_t dma;
271 struct xhci_hcd *xhci;
272 struct xhci_ep_ctx *ep_ctx;
273 struct xhci_slot_priv *priv = s->private;
274 struct xhci_virt_device *dev = priv->dev;
275 char str[XHCI_MSG_MAX];
276
277 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
278
279 for (ep_index = 0; ep_index < 31; ep_index++) {
280 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
281 dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
282 seq_printf(s, "%pad: %s, virt_state:%#x\n", &dma,
283 xhci_decode_ep_context(str,
284 le32_to_cpu(ep_ctx->ep_info),
285 le32_to_cpu(ep_ctx->ep_info2),
286 le64_to_cpu(ep_ctx->deq),
287 le32_to_cpu(ep_ctx->tx_info)),
288 dev->eps[ep_index].ep_state);
289 }
290
291 return 0;
292 }
293
xhci_device_name_show(struct seq_file * s,void * unused)294 static int xhci_device_name_show(struct seq_file *s, void *unused)
295 {
296 struct xhci_slot_priv *priv = s->private;
297 struct xhci_virt_device *dev = priv->dev;
298
299 seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
300
301 return 0;
302 }
303
304 static struct xhci_file_map context_files[] = {
305 {"name", xhci_device_name_show, },
306 {"slot-context", xhci_slot_context_show, },
307 {"ep-context", xhci_endpoint_context_show, },
308 };
309
xhci_context_open(struct inode * inode,struct file * file)310 static int xhci_context_open(struct inode *inode, struct file *file)
311 {
312 const struct xhci_file_map *f_map = debugfs_get_aux(file);
313
314 return single_open(file, f_map->show, inode->i_private);
315 }
316
317 static const struct file_operations xhci_context_fops = {
318 .open = xhci_context_open,
319 .read = seq_read,
320 .llseek = seq_lseek,
321 .release = single_release,
322 };
323
324
325
xhci_portsc_show(struct seq_file * s,void * unused)326 static int xhci_portsc_show(struct seq_file *s, void *unused)
327 {
328 struct xhci_port *port = s->private;
329 u32 portsc;
330 char str[XHCI_MSG_MAX];
331
332 portsc = xhci_portsc_readl(port);
333 seq_printf(s, "%s\n", xhci_decode_portsc(str, portsc));
334
335 return 0;
336 }
337
xhci_port_open(struct inode * inode,struct file * file)338 static int xhci_port_open(struct inode *inode, struct file *file)
339 {
340 return single_open(file, xhci_portsc_show, inode->i_private);
341 }
342
xhci_port_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)343 static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,
344 size_t count, loff_t *ppos)
345 {
346 struct seq_file *s = file->private_data;
347 struct xhci_port *port = s->private;
348 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
349 char buf[32];
350 u32 portsc;
351 unsigned long flags;
352
353 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
354 return -EFAULT;
355
356 if (!strncmp(buf, "compliance", 10)) {
357 /* If CTC is clear, compliance is enabled by default */
358 if (!(xhci->hcc_params2 & HCC2_CTC))
359 return count;
360 spin_lock_irqsave(&xhci->lock, flags);
361 /* compliance mode can only be enabled on ports in RxDetect */
362 portsc = xhci_portsc_readl(port);
363 if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
364 spin_unlock_irqrestore(&xhci->lock, flags);
365 return -EPERM;
366 }
367 portsc = xhci_port_state_to_neutral(portsc);
368 portsc &= ~PORT_PLS_MASK;
369 portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
370 xhci_portsc_writel(port, portsc);
371 spin_unlock_irqrestore(&xhci->lock, flags);
372 } else {
373 return -EINVAL;
374 }
375 return count;
376 }
377
378 static const struct file_operations port_fops = {
379 .open = xhci_port_open,
380 .write = xhci_port_write,
381 .read = seq_read,
382 .llseek = seq_lseek,
383 .release = single_release,
384 };
385
xhci_portli_show(struct seq_file * s,void * unused)386 static int xhci_portli_show(struct seq_file *s, void *unused)
387 {
388 struct xhci_port *port = s->private;
389 struct xhci_hcd *xhci;
390 u32 portli;
391
392 portli = readl(&port->port_reg->portli);
393
394 /* port without protocol capability isn't added to a roothub */
395 if (!port->rhub) {
396 seq_printf(s, "0x%08x\n", portli);
397 return 0;
398 }
399
400 xhci = hcd_to_xhci(port->rhub->hcd);
401
402 /* PORTLI fields are valid if port is a USB3 or eUSB2V2 port */
403 if (port->rhub == &xhci->usb3_rhub)
404 seq_printf(s, "0x%08x LEC=%u RLC=%u TLC=%u\n", portli,
405 PORT_LEC(portli), PORT_RX_LANES(portli), PORT_TX_LANES(portli));
406 else if (xhci->hcc_params2 & HCC2_E2V2C)
407 seq_printf(s, "0x%08x RDR=%u TDR=%u\n", portli,
408 PORTLI_RDR(portli), PORTLI_TDR(portli));
409 else
410 seq_printf(s, "0x%08x RsvdP\n", portli);
411
412 return 0;
413 }
414
xhci_portli_open(struct inode * inode,struct file * file)415 static int xhci_portli_open(struct inode *inode, struct file *file)
416 {
417 return single_open(file, xhci_portli_show, inode->i_private);
418 }
419
420 static const struct file_operations portli_fops = {
421 .open = xhci_portli_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
424 .release = single_release,
425 };
426
xhci_debugfs_create_files(struct xhci_hcd * xhci,struct xhci_file_map * files,size_t nentries,void * data,struct dentry * parent,const struct file_operations * fops)427 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
428 struct xhci_file_map *files,
429 size_t nentries, void *data,
430 struct dentry *parent,
431 const struct file_operations *fops)
432 {
433 int i;
434
435 for (i = 0; i < nentries; i++)
436 debugfs_create_file_aux(files[i].name, 0444, parent,
437 data, &files[i], fops);
438 }
439
xhci_debugfs_create_ring_dir(struct xhci_hcd * xhci,struct xhci_ring ** ring,const char * name,struct dentry * parent)440 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
441 struct xhci_ring **ring,
442 const char *name,
443 struct dentry *parent)
444 {
445 struct dentry *dir;
446
447 dir = debugfs_create_dir(name, parent);
448 xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
449 ring, dir, &xhci_ring_fops);
450
451 return dir;
452 }
453
xhci_debugfs_create_context_files(struct xhci_hcd * xhci,struct dentry * parent,int slot_id)454 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
455 struct dentry *parent,
456 int slot_id)
457 {
458 struct xhci_virt_device *dev = xhci->devs[slot_id];
459
460 xhci_debugfs_create_files(xhci, context_files,
461 ARRAY_SIZE(context_files),
462 dev->debugfs_private,
463 parent, &xhci_context_fops);
464 }
465
xhci_debugfs_create_endpoint(struct xhci_hcd * xhci,struct xhci_virt_device * dev,int ep_index)466 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
467 struct xhci_virt_device *dev,
468 int ep_index)
469 {
470 struct xhci_ep_priv *epriv;
471 struct xhci_slot_priv *spriv = dev->debugfs_private;
472
473 if (!spriv)
474 return;
475
476 if (spriv->eps[ep_index])
477 return;
478
479 epriv = kzalloc_obj(*epriv);
480 if (!epriv)
481 return;
482
483 epriv->show_ring = dev->eps[ep_index].ring;
484
485 snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
486 epriv->root = xhci_debugfs_create_ring_dir(xhci,
487 &epriv->show_ring,
488 epriv->name,
489 spriv->root);
490 spriv->eps[ep_index] = epriv;
491 }
492
xhci_debugfs_remove_endpoint(struct xhci_hcd * xhci,struct xhci_virt_device * dev,int ep_index)493 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
494 struct xhci_virt_device *dev,
495 int ep_index)
496 {
497 struct xhci_ep_priv *epriv;
498 struct xhci_slot_priv *spriv = dev->debugfs_private;
499
500 if (!spriv || !spriv->eps[ep_index])
501 return;
502
503 epriv = spriv->eps[ep_index];
504 debugfs_remove_recursive(epriv->root);
505 spriv->eps[ep_index] = NULL;
506 kfree(epriv);
507 }
508
xhci_stream_id_show(struct seq_file * s,void * unused)509 static int xhci_stream_id_show(struct seq_file *s, void *unused)
510 {
511 struct xhci_ep_priv *epriv = s->private;
512
513 if (!epriv->stream_info)
514 return -EPERM;
515
516 seq_printf(s, "Show stream ID %d trb ring, supported [1 - %d]\n",
517 epriv->stream_id, epriv->stream_info->num_streams - 1);
518
519 return 0;
520 }
521
xhci_stream_id_open(struct inode * inode,struct file * file)522 static int xhci_stream_id_open(struct inode *inode, struct file *file)
523 {
524 return single_open(file, xhci_stream_id_show, inode->i_private);
525 }
526
xhci_stream_id_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)527 static ssize_t xhci_stream_id_write(struct file *file, const char __user *ubuf,
528 size_t count, loff_t *ppos)
529 {
530 struct seq_file *s = file->private_data;
531 struct xhci_ep_priv *epriv = s->private;
532 int ret;
533 u16 stream_id; /* MaxPStreams + 1 <= 16 */
534
535 if (!epriv->stream_info)
536 return -EPERM;
537
538 /* Decimal number */
539 ret = kstrtou16_from_user(ubuf, count, 10, &stream_id);
540 if (ret)
541 return ret;
542
543 if (stream_id == 0 || stream_id >= epriv->stream_info->num_streams)
544 return -EINVAL;
545
546 epriv->stream_id = stream_id;
547 epriv->show_ring = epriv->stream_info->stream_rings[stream_id];
548
549 return count;
550 }
551
552 static const struct file_operations stream_id_fops = {
553 .open = xhci_stream_id_open,
554 .write = xhci_stream_id_write,
555 .read = seq_read,
556 .llseek = seq_lseek,
557 .release = single_release,
558 };
559
xhci_stream_context_array_show(struct seq_file * s,void * unused)560 static int xhci_stream_context_array_show(struct seq_file *s, void *unused)
561 {
562 struct xhci_ep_priv *epriv = s->private;
563 struct xhci_stream_ctx *stream_ctx;
564 dma_addr_t dma;
565 int id;
566
567 if (!epriv->stream_info)
568 return -EPERM;
569
570 seq_printf(s, "Allocated %d streams and %d stream context array entries\n",
571 epriv->stream_info->num_streams,
572 epriv->stream_info->num_stream_ctxs);
573
574 for (id = 0; id < epriv->stream_info->num_stream_ctxs; id++) {
575 stream_ctx = epriv->stream_info->stream_ctx_array + id;
576 dma = epriv->stream_info->ctx_array_dma + id * 16;
577 if (id < epriv->stream_info->num_streams)
578 seq_printf(s, "%pad stream id %d deq %016llx\n", &dma,
579 id, le64_to_cpu(stream_ctx->stream_ring));
580 else
581 seq_printf(s, "%pad stream context entry not used deq %016llx\n",
582 &dma, le64_to_cpu(stream_ctx->stream_ring));
583 }
584
585 return 0;
586 }
587 DEFINE_SHOW_ATTRIBUTE(xhci_stream_context_array);
588
xhci_debugfs_create_stream_files(struct xhci_hcd * xhci,struct xhci_virt_device * dev,int ep_index)589 void xhci_debugfs_create_stream_files(struct xhci_hcd *xhci,
590 struct xhci_virt_device *dev,
591 int ep_index)
592 {
593 struct xhci_slot_priv *spriv = dev->debugfs_private;
594 struct xhci_ep_priv *epriv;
595
596 if (!spriv || !spriv->eps[ep_index] ||
597 !dev->eps[ep_index].stream_info)
598 return;
599
600 epriv = spriv->eps[ep_index];
601 epriv->stream_info = dev->eps[ep_index].stream_info;
602
603 /* Show trb ring of stream ID 1 by default */
604 epriv->stream_id = 1;
605 epriv->show_ring = epriv->stream_info->stream_rings[1];
606 debugfs_create_file("stream_id", 0644,
607 epriv->root, epriv,
608 &stream_id_fops);
609 debugfs_create_file("stream_context_array", 0444,
610 epriv->root, epriv,
611 &xhci_stream_context_array_fops);
612 }
613
xhci_debugfs_create_slot(struct xhci_hcd * xhci,int slot_id)614 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
615 {
616 struct xhci_slot_priv *priv;
617 struct xhci_virt_device *dev = xhci->devs[slot_id];
618
619 priv = kzalloc_obj(*priv);
620 if (!priv)
621 return;
622
623 snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
624 priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
625 priv->dev = dev;
626 dev->debugfs_private = priv;
627
628 xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
629 "ep00", priv->root);
630
631 xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
632 }
633
xhci_debugfs_remove_slot(struct xhci_hcd * xhci,int slot_id)634 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
635 {
636 int i;
637 struct xhci_slot_priv *priv;
638 struct xhci_virt_device *dev = xhci->devs[slot_id];
639
640 if (!dev || !dev->debugfs_private)
641 return;
642
643 priv = dev->debugfs_private;
644
645 debugfs_remove_recursive(priv->root);
646
647 for (i = 0; i < 31; i++)
648 kfree(priv->eps[i]);
649
650 kfree(priv);
651 dev->debugfs_private = NULL;
652 }
653
xhci_debugfs_create_ports(struct xhci_hcd * xhci,struct dentry * parent)654 static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
655 struct dentry *parent)
656 {
657 char port_name[8];
658 struct xhci_port *port;
659 struct dentry *dir;
660
661 parent = debugfs_create_dir("ports", parent);
662
663 for (int i = 0; i < xhci->max_ports; i++) {
664 scnprintf(port_name, sizeof(port_name), "port%02d", i + 1);
665 dir = debugfs_create_dir(port_name, parent);
666 port = &xhci->hw_ports[i];
667 debugfs_create_file("portsc", 0644, dir, port, &port_fops);
668 debugfs_create_file("portli", 0444, dir, port, &portli_fops);
669 }
670 }
671
xhci_port_bw_show(struct xhci_hcd * xhci,u8 dev_speed,struct seq_file * s)672 static int xhci_port_bw_show(struct xhci_hcd *xhci, u8 dev_speed,
673 struct seq_file *s)
674 {
675 unsigned int i;
676 int ret;
677 struct xhci_container_ctx *ctx;
678 struct usb_hcd *hcd = xhci_to_hcd(xhci);
679 struct device *dev = hcd->self.controller;
680
681 ret = pm_runtime_get_sync(dev);
682 if (ret < 0)
683 return ret;
684
685 ctx = xhci_alloc_port_bw_ctx(xhci, 0);
686 if (!ctx) {
687 pm_runtime_put_sync(dev);
688 return -ENOMEM;
689 }
690
691 /* get roothub port bandwidth */
692 ret = xhci_get_port_bandwidth(xhci, ctx, dev_speed);
693 if (ret)
694 goto err_out;
695
696 /* print all roothub ports available bandwidth
697 * refer to xhci rev1_2 protocol 6.2.6 , byte 0 is reserved
698 */
699 for (i = 1; i <= xhci->max_ports; i++)
700 seq_printf(s, "port[%d] available bw: %d%%.\n", i,
701 ctx->bytes[i]);
702 err_out:
703 pm_runtime_put_sync(dev);
704 xhci_free_port_bw_ctx(xhci, ctx);
705 return ret;
706 }
707
xhci_ss_bw_show(struct seq_file * s,void * unused)708 static int xhci_ss_bw_show(struct seq_file *s, void *unused)
709 {
710 int ret;
711 struct xhci_hcd *xhci = (struct xhci_hcd *)s->private;
712
713 ret = xhci_port_bw_show(xhci, USB_SPEED_SUPER, s);
714 return ret;
715 }
716
xhci_hs_bw_show(struct seq_file * s,void * unused)717 static int xhci_hs_bw_show(struct seq_file *s, void *unused)
718 {
719 int ret;
720 struct xhci_hcd *xhci = (struct xhci_hcd *)s->private;
721
722 ret = xhci_port_bw_show(xhci, USB_SPEED_HIGH, s);
723 return ret;
724 }
725
xhci_fs_bw_show(struct seq_file * s,void * unused)726 static int xhci_fs_bw_show(struct seq_file *s, void *unused)
727 {
728 int ret;
729 struct xhci_hcd *xhci = (struct xhci_hcd *)s->private;
730
731 ret = xhci_port_bw_show(xhci, USB_SPEED_FULL, s);
732 return ret;
733 }
734
735 static struct xhci_file_map bw_context_files[] = {
736 {"SS_BW", xhci_ss_bw_show, },
737 {"HS_BW", xhci_hs_bw_show, },
738 {"FS_BW", xhci_fs_bw_show, },
739 };
740
bw_context_open(struct inode * inode,struct file * file)741 static int bw_context_open(struct inode *inode, struct file *file)
742 {
743 int i;
744 struct xhci_file_map *f_map;
745 const char *file_name = file_dentry(file)->d_iname;
746
747 for (i = 0; i < ARRAY_SIZE(bw_context_files); i++) {
748 f_map = &bw_context_files[i];
749
750 if (strcmp(f_map->name, file_name) == 0)
751 break;
752 }
753
754 return single_open(file, f_map->show, inode->i_private);
755 }
756
757 static const struct file_operations bw_fops = {
758 .open = bw_context_open,
759 .read = seq_read,
760 .llseek = seq_lseek,
761 .release = single_release,
762 };
763
xhci_debugfs_create_bandwidth(struct xhci_hcd * xhci,struct dentry * parent)764 static void xhci_debugfs_create_bandwidth(struct xhci_hcd *xhci,
765 struct dentry *parent)
766 {
767 parent = debugfs_create_dir("port_bandwidth", parent);
768
769 xhci_debugfs_create_files(xhci, bw_context_files,
770 ARRAY_SIZE(bw_context_files),
771 xhci,
772 parent, &bw_fops);
773 }
774
xhci_debugfs_init(struct xhci_hcd * xhci)775 void xhci_debugfs_init(struct xhci_hcd *xhci)
776 {
777 struct device *dev = xhci_to_hcd(xhci)->self.controller;
778
779 xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
780 xhci_debugfs_root);
781
782 INIT_LIST_HEAD(&xhci->regset_list);
783
784 xhci_debugfs_regset(xhci,
785 0,
786 xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
787 xhci->debugfs_root, "reg-cap");
788
789 xhci_debugfs_regset(xhci,
790 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
791 xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
792 xhci->debugfs_root, "reg-op");
793
794 xhci_debugfs_regset(xhci,
795 readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
796 xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
797 xhci->debugfs_root, "reg-runtime");
798
799 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
800 xhci_extcap_legsup,
801 ARRAY_SIZE(xhci_extcap_legsup),
802 "reg-ext-legsup");
803
804 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
805 xhci_extcap_protocol,
806 ARRAY_SIZE(xhci_extcap_protocol),
807 "reg-ext-protocol");
808
809 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
810 xhci_extcap_dbc,
811 ARRAY_SIZE(xhci_extcap_dbc),
812 "reg-ext-dbc");
813
814 xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
815 "command-ring",
816 xhci->debugfs_root);
817
818 xhci_debugfs_create_ring_dir(xhci, &xhci->interrupters[0]->event_ring,
819 "event-ring",
820 xhci->debugfs_root);
821
822 xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
823
824 xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
825
826 xhci_debugfs_create_bandwidth(xhci, xhci->debugfs_root);
827 }
828
xhci_debugfs_exit(struct xhci_hcd * xhci)829 void xhci_debugfs_exit(struct xhci_hcd *xhci)
830 {
831 struct xhci_regset *rgs, *tmp;
832
833 debugfs_remove_recursive(xhci->debugfs_root);
834 xhci->debugfs_root = NULL;
835 xhci->debugfs_slots = NULL;
836
837 list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
838 xhci_debugfs_free_regset(rgs);
839 }
840
xhci_debugfs_create_root(void)841 void __init xhci_debugfs_create_root(void)
842 {
843 xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
844 }
845
xhci_debugfs_remove_root(void)846 void __exit xhci_debugfs_remove_root(void)
847 {
848 debugfs_remove_recursive(xhci_debugfs_root);
849 xhci_debugfs_root = NULL;
850 }
851