• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(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 
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(&regset->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 	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 
249 static 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 
xhci_slot_context_show(struct seq_file * s,void * unused)256 static 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 	char			str[XHCI_MSG_MAX];
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,
267 		   xhci_decode_slot_context(str,
268 					    le32_to_cpu(slot_ctx->dev_info),
269 					    le32_to_cpu(slot_ctx->dev_info2),
270 					    le32_to_cpu(slot_ctx->tt_info),
271 					    le32_to_cpu(slot_ctx->dev_state)));
272 
273 	return 0;
274 }
275 
xhci_endpoint_context_show(struct seq_file * s,void * unused)276 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
277 {
278 	int			ep_index;
279 	dma_addr_t		dma;
280 	struct xhci_hcd		*xhci;
281 	struct xhci_ep_ctx	*ep_ctx;
282 	struct xhci_slot_priv	*priv = s->private;
283 	struct xhci_virt_device	*dev = priv->dev;
284 	char			str[XHCI_MSG_MAX];
285 
286 	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
287 
288 	for (ep_index = 0; ep_index < 31; ep_index++) {
289 		ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
290 		dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
291 		seq_printf(s, "%pad: %s\n", &dma,
292 			   xhci_decode_ep_context(str,
293 						  le32_to_cpu(ep_ctx->ep_info),
294 						  le32_to_cpu(ep_ctx->ep_info2),
295 						  le64_to_cpu(ep_ctx->deq),
296 						  le32_to_cpu(ep_ctx->tx_info)));
297 	}
298 
299 	return 0;
300 }
301 
xhci_device_name_show(struct seq_file * s,void * unused)302 static int xhci_device_name_show(struct seq_file *s, void *unused)
303 {
304 	struct xhci_slot_priv	*priv = s->private;
305 	struct xhci_virt_device	*dev = priv->dev;
306 
307 	seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
308 
309 	return 0;
310 }
311 
312 static struct xhci_file_map context_files[] = {
313 	{"name",		xhci_device_name_show, },
314 	{"slot-context",	xhci_slot_context_show, },
315 	{"ep-context",		xhci_endpoint_context_show, },
316 };
317 
xhci_context_open(struct inode * inode,struct file * file)318 static int xhci_context_open(struct inode *inode, struct file *file)
319 {
320 	int			i;
321 	struct xhci_file_map	*f_map;
322 	const char		*file_name = file_dentry(file)->d_iname;
323 
324 	for (i = 0; i < ARRAY_SIZE(context_files); i++) {
325 		f_map = &context_files[i];
326 
327 		if (strcmp(f_map->name, file_name) == 0)
328 			break;
329 	}
330 
331 	return single_open(file, f_map->show, inode->i_private);
332 }
333 
334 static const struct file_operations xhci_context_fops = {
335 	.open			= xhci_context_open,
336 	.read			= seq_read,
337 	.llseek			= seq_lseek,
338 	.release		= single_release,
339 };
340 
341 
342 
xhci_portsc_show(struct seq_file * s,void * unused)343 static int xhci_portsc_show(struct seq_file *s, void *unused)
344 {
345 	struct xhci_port	*port = s->private;
346 	u32			portsc;
347 	char			str[XHCI_MSG_MAX];
348 
349 	portsc = readl(port->addr);
350 	seq_printf(s, "%s\n", xhci_decode_portsc(str, portsc));
351 
352 	return 0;
353 }
354 
xhci_port_open(struct inode * inode,struct file * file)355 static int xhci_port_open(struct inode *inode, struct file *file)
356 {
357 	return single_open(file, xhci_portsc_show, inode->i_private);
358 }
359 
xhci_port_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)360 static ssize_t xhci_port_write(struct file *file,  const char __user *ubuf,
361 			       size_t count, loff_t *ppos)
362 {
363 	struct seq_file         *s = file->private_data;
364 	struct xhci_port	*port = s->private;
365 	struct xhci_hcd		*xhci = hcd_to_xhci(port->rhub->hcd);
366 	char                    buf[32];
367 	u32			portsc;
368 	unsigned long		flags;
369 
370 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
371 		return -EFAULT;
372 
373 	if (!strncmp(buf, "compliance", 10)) {
374 		/* If CTC is clear, compliance is enabled by default */
375 		if (!HCC2_CTC(xhci->hcc_params2))
376 			return count;
377 		spin_lock_irqsave(&xhci->lock, flags);
378 		/* compliance mode can only be enabled on ports in RxDetect */
379 		portsc = readl(port->addr);
380 		if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
381 			spin_unlock_irqrestore(&xhci->lock, flags);
382 			return -EPERM;
383 		}
384 		portsc = xhci_port_state_to_neutral(portsc);
385 		portsc &= ~PORT_PLS_MASK;
386 		portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
387 		writel(portsc, port->addr);
388 		spin_unlock_irqrestore(&xhci->lock, flags);
389 	} else {
390 		return -EINVAL;
391 	}
392 	return count;
393 }
394 
395 static const struct file_operations port_fops = {
396 	.open			= xhci_port_open,
397 	.write                  = xhci_port_write,
398 	.read			= seq_read,
399 	.llseek			= seq_lseek,
400 	.release		= single_release,
401 };
402 
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)403 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
404 				      struct xhci_file_map *files,
405 				      size_t nentries, void *data,
406 				      struct dentry *parent,
407 				      const struct file_operations *fops)
408 {
409 	int			i;
410 
411 	for (i = 0; i < nentries; i++)
412 		debugfs_create_file(files[i].name, 0444, parent, data, fops);
413 }
414 
xhci_debugfs_create_ring_dir(struct xhci_hcd * xhci,struct xhci_ring ** ring,const char * name,struct dentry * parent)415 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
416 						   struct xhci_ring **ring,
417 						   const char *name,
418 						   struct dentry *parent)
419 {
420 	struct dentry		*dir;
421 
422 	dir = debugfs_create_dir(name, parent);
423 	xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
424 				  ring, dir, &xhci_ring_fops);
425 
426 	return dir;
427 }
428 
xhci_debugfs_create_context_files(struct xhci_hcd * xhci,struct dentry * parent,int slot_id)429 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
430 					      struct dentry *parent,
431 					      int slot_id)
432 {
433 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
434 
435 	xhci_debugfs_create_files(xhci, context_files,
436 				  ARRAY_SIZE(context_files),
437 				  dev->debugfs_private,
438 				  parent, &xhci_context_fops);
439 }
440 
xhci_debugfs_create_endpoint(struct xhci_hcd * xhci,struct xhci_virt_device * dev,int ep_index)441 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
442 				  struct xhci_virt_device *dev,
443 				  int ep_index)
444 {
445 	struct xhci_ep_priv	*epriv;
446 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
447 
448 	if (!spriv)
449 		return;
450 
451 	if (spriv->eps[ep_index])
452 		return;
453 
454 	epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
455 	if (!epriv)
456 		return;
457 
458 	epriv->show_ring = dev->eps[ep_index].ring;
459 
460 	snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
461 	epriv->root = xhci_debugfs_create_ring_dir(xhci,
462 						   &epriv->show_ring,
463 						   epriv->name,
464 						   spriv->root);
465 	spriv->eps[ep_index] = epriv;
466 }
467 
xhci_debugfs_remove_endpoint(struct xhci_hcd * xhci,struct xhci_virt_device * dev,int ep_index)468 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
469 				  struct xhci_virt_device *dev,
470 				  int ep_index)
471 {
472 	struct xhci_ep_priv	*epriv;
473 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
474 
475 	if (!spriv || !spriv->eps[ep_index])
476 		return;
477 
478 	epriv = spriv->eps[ep_index];
479 	debugfs_remove_recursive(epriv->root);
480 	spriv->eps[ep_index] = NULL;
481 	kfree(epriv);
482 }
483 
xhci_stream_id_show(struct seq_file * s,void * unused)484 static int xhci_stream_id_show(struct seq_file *s, void *unused)
485 {
486 	struct xhci_ep_priv	*epriv = s->private;
487 
488 	if (!epriv->stream_info)
489 		return -EPERM;
490 
491 	seq_printf(s, "Show stream ID %d trb ring, supported [1 - %d]\n",
492 		   epriv->stream_id, epriv->stream_info->num_streams - 1);
493 
494 	return 0;
495 }
496 
xhci_stream_id_open(struct inode * inode,struct file * file)497 static int xhci_stream_id_open(struct inode *inode, struct file *file)
498 {
499 	return single_open(file, xhci_stream_id_show, inode->i_private);
500 }
501 
xhci_stream_id_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)502 static ssize_t xhci_stream_id_write(struct file *file,  const char __user *ubuf,
503 			       size_t count, loff_t *ppos)
504 {
505 	struct seq_file         *s = file->private_data;
506 	struct xhci_ep_priv	*epriv = s->private;
507 	int			ret;
508 	u16			stream_id; /* MaxPStreams + 1 <= 16 */
509 
510 	if (!epriv->stream_info)
511 		return -EPERM;
512 
513 	/* Decimal number */
514 	ret = kstrtou16_from_user(ubuf, count, 10, &stream_id);
515 	if (ret)
516 		return ret;
517 
518 	if (stream_id == 0 || stream_id >= epriv->stream_info->num_streams)
519 		return -EINVAL;
520 
521 	epriv->stream_id = stream_id;
522 	epriv->show_ring = epriv->stream_info->stream_rings[stream_id];
523 
524 	return count;
525 }
526 
527 static const struct file_operations stream_id_fops = {
528 	.open			= xhci_stream_id_open,
529 	.write                  = xhci_stream_id_write,
530 	.read			= seq_read,
531 	.llseek			= seq_lseek,
532 	.release		= single_release,
533 };
534 
xhci_stream_context_array_show(struct seq_file * s,void * unused)535 static int xhci_stream_context_array_show(struct seq_file *s, void *unused)
536 {
537 	struct xhci_ep_priv	*epriv = s->private;
538 	struct xhci_stream_ctx	*stream_ctx;
539 	dma_addr_t		dma;
540 	int			id;
541 
542 	if (!epriv->stream_info)
543 		return -EPERM;
544 
545 	seq_printf(s, "Allocated %d streams and %d stream context array entries\n",
546 			epriv->stream_info->num_streams,
547 			epriv->stream_info->num_stream_ctxs);
548 
549 	for (id = 0; id < epriv->stream_info->num_stream_ctxs; id++) {
550 		stream_ctx = epriv->stream_info->stream_ctx_array + id;
551 		dma = epriv->stream_info->ctx_array_dma + id * 16;
552 		if (id < epriv->stream_info->num_streams)
553 			seq_printf(s, "%pad stream id %d deq %016llx\n", &dma,
554 				   id, le64_to_cpu(stream_ctx->stream_ring));
555 		else
556 			seq_printf(s, "%pad stream context entry not used deq %016llx\n",
557 				   &dma, le64_to_cpu(stream_ctx->stream_ring));
558 	}
559 
560 	return 0;
561 }
562 DEFINE_SHOW_ATTRIBUTE(xhci_stream_context_array);
563 
xhci_debugfs_create_stream_files(struct xhci_hcd * xhci,struct xhci_virt_device * dev,int ep_index)564 void xhci_debugfs_create_stream_files(struct xhci_hcd *xhci,
565 				      struct xhci_virt_device *dev,
566 				      int ep_index)
567 {
568 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
569 	struct xhci_ep_priv	*epriv;
570 
571 	if (!spriv || !spriv->eps[ep_index] ||
572 	    !dev->eps[ep_index].stream_info)
573 		return;
574 
575 	epriv = spriv->eps[ep_index];
576 	epriv->stream_info = dev->eps[ep_index].stream_info;
577 
578 	/* Show trb ring of stream ID 1 by default */
579 	epriv->stream_id = 1;
580 	epriv->show_ring = epriv->stream_info->stream_rings[1];
581 	debugfs_create_file("stream_id", 0644,
582 			    epriv->root, epriv,
583 			    &stream_id_fops);
584 	debugfs_create_file("stream_context_array", 0444,
585 			    epriv->root, epriv,
586 			    &xhci_stream_context_array_fops);
587 }
588 
xhci_debugfs_create_slot(struct xhci_hcd * xhci,int slot_id)589 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
590 {
591 	struct xhci_slot_priv	*priv;
592 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
593 
594 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
595 	if (!priv)
596 		return;
597 
598 	snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
599 	priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
600 	priv->dev = dev;
601 	dev->debugfs_private = priv;
602 
603 	xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
604 				     "ep00", priv->root);
605 
606 	xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
607 }
608 
xhci_debugfs_remove_slot(struct xhci_hcd * xhci,int slot_id)609 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
610 {
611 	int			i;
612 	struct xhci_slot_priv	*priv;
613 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
614 
615 	if (!dev || !dev->debugfs_private)
616 		return;
617 
618 	priv = dev->debugfs_private;
619 
620 	debugfs_remove_recursive(priv->root);
621 
622 	for (i = 0; i < 31; i++)
623 		kfree(priv->eps[i]);
624 
625 	kfree(priv);
626 	dev->debugfs_private = NULL;
627 }
628 
xhci_debugfs_create_ports(struct xhci_hcd * xhci,struct dentry * parent)629 static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
630 				      struct dentry *parent)
631 {
632 	unsigned int		num_ports;
633 	char			port_name[8];
634 	struct xhci_port	*port;
635 	struct dentry		*dir;
636 
637 	num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
638 
639 	parent = debugfs_create_dir("ports", parent);
640 
641 	while (num_ports--) {
642 		scnprintf(port_name, sizeof(port_name), "port%02d",
643 			  num_ports + 1);
644 		dir = debugfs_create_dir(port_name, parent);
645 		port = &xhci->hw_ports[num_ports];
646 		debugfs_create_file("portsc", 0644, dir, port, &port_fops);
647 	}
648 }
649 
xhci_debugfs_init(struct xhci_hcd * xhci)650 void xhci_debugfs_init(struct xhci_hcd *xhci)
651 {
652 	struct device		*dev = xhci_to_hcd(xhci)->self.controller;
653 
654 	xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
655 						xhci_debugfs_root);
656 
657 	INIT_LIST_HEAD(&xhci->regset_list);
658 
659 	xhci_debugfs_regset(xhci,
660 			    0,
661 			    xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
662 			    xhci->debugfs_root, "reg-cap");
663 
664 	xhci_debugfs_regset(xhci,
665 			    HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
666 			    xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
667 			    xhci->debugfs_root, "reg-op");
668 
669 	xhci_debugfs_regset(xhci,
670 			    readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
671 			    xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
672 			    xhci->debugfs_root, "reg-runtime");
673 
674 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
675 				   xhci_extcap_legsup,
676 				   ARRAY_SIZE(xhci_extcap_legsup),
677 				   "reg-ext-legsup");
678 
679 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
680 				   xhci_extcap_protocol,
681 				   ARRAY_SIZE(xhci_extcap_protocol),
682 				   "reg-ext-protocol");
683 
684 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
685 				   xhci_extcap_dbc,
686 				   ARRAY_SIZE(xhci_extcap_dbc),
687 				   "reg-ext-dbc");
688 
689 	xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
690 				     "command-ring",
691 				     xhci->debugfs_root);
692 
693 	xhci_debugfs_create_ring_dir(xhci, &xhci->interrupters[0]->event_ring,
694 				     "event-ring",
695 				     xhci->debugfs_root);
696 
697 	xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
698 
699 	xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
700 }
701 
xhci_debugfs_exit(struct xhci_hcd * xhci)702 void xhci_debugfs_exit(struct xhci_hcd *xhci)
703 {
704 	struct xhci_regset	*rgs, *tmp;
705 
706 	debugfs_remove_recursive(xhci->debugfs_root);
707 	xhci->debugfs_root = NULL;
708 	xhci->debugfs_slots = NULL;
709 
710 	list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
711 		xhci_debugfs_free_regset(rgs);
712 }
713 
xhci_debugfs_create_root(void)714 void __init xhci_debugfs_create_root(void)
715 {
716 	xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
717 }
718 
xhci_debugfs_remove_root(void)719 void __exit xhci_debugfs_remove_root(void)
720 {
721 	debugfs_remove_recursive(xhci_debugfs_root);
722 	xhci_debugfs_root = NULL;
723 }
724