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