xref: /linux/drivers/usb/host/xhci-debugfs.c (revision 3efe891f61479a98edc5c3c7b91b92b69d99e592)
1 /*
2  * xhci-debugfs.c - xHCI debugfs interface
3  *
4  * Copyright (C) 2017 Intel Corporation
5  *
6  * Author: Lu Baolu <baolu.lu@linux.intel.com>
7  */
8 
9 #include <linux/slab.h>
10 
11 #include "xhci.h"
12 #include "xhci-debugfs.h"
13 
14 static const struct debugfs_reg32 xhci_cap_regs[] = {
15 	dump_register(CAPLENGTH),
16 	dump_register(HCSPARAMS1),
17 	dump_register(HCSPARAMS2),
18 	dump_register(HCSPARAMS3),
19 	dump_register(HCCPARAMS1),
20 	dump_register(DOORBELLOFF),
21 	dump_register(RUNTIMEOFF),
22 	dump_register(HCCPARAMS2),
23 };
24 
25 static const struct debugfs_reg32 xhci_op_regs[] = {
26 	dump_register(USBCMD),
27 	dump_register(USBSTS),
28 	dump_register(PAGESIZE),
29 	dump_register(DNCTRL),
30 	dump_register(CRCR),
31 	dump_register(DCBAAP_LOW),
32 	dump_register(DCBAAP_HIGH),
33 	dump_register(CONFIG),
34 };
35 
36 static const struct debugfs_reg32 xhci_runtime_regs[] = {
37 	dump_register(MFINDEX),
38 	dump_register(IR0_IMAN),
39 	dump_register(IR0_IMOD),
40 	dump_register(IR0_ERSTSZ),
41 	dump_register(IR0_ERSTBA_LOW),
42 	dump_register(IR0_ERSTBA_HIGH),
43 	dump_register(IR0_ERDP_LOW),
44 	dump_register(IR0_ERDP_HIGH),
45 };
46 
47 static const struct debugfs_reg32 xhci_extcap_legsup[] = {
48 	dump_register(EXTCAP_USBLEGSUP),
49 	dump_register(EXTCAP_USBLEGCTLSTS),
50 };
51 
52 static const struct debugfs_reg32 xhci_extcap_protocol[] = {
53 	dump_register(EXTCAP_REVISION),
54 	dump_register(EXTCAP_NAME),
55 	dump_register(EXTCAP_PORTINFO),
56 	dump_register(EXTCAP_PORTTYPE),
57 	dump_register(EXTCAP_MANTISSA1),
58 	dump_register(EXTCAP_MANTISSA2),
59 	dump_register(EXTCAP_MANTISSA3),
60 	dump_register(EXTCAP_MANTISSA4),
61 	dump_register(EXTCAP_MANTISSA5),
62 	dump_register(EXTCAP_MANTISSA6),
63 };
64 
65 static const struct debugfs_reg32 xhci_extcap_dbc[] = {
66 	dump_register(EXTCAP_DBC_CAPABILITY),
67 	dump_register(EXTCAP_DBC_DOORBELL),
68 	dump_register(EXTCAP_DBC_ERSTSIZE),
69 	dump_register(EXTCAP_DBC_ERST_LOW),
70 	dump_register(EXTCAP_DBC_ERST_HIGH),
71 	dump_register(EXTCAP_DBC_ERDP_LOW),
72 	dump_register(EXTCAP_DBC_ERDP_HIGH),
73 	dump_register(EXTCAP_DBC_CONTROL),
74 	dump_register(EXTCAP_DBC_STATUS),
75 	dump_register(EXTCAP_DBC_PORTSC),
76 	dump_register(EXTCAP_DBC_CONT_LOW),
77 	dump_register(EXTCAP_DBC_CONT_HIGH),
78 	dump_register(EXTCAP_DBC_DEVINFO1),
79 	dump_register(EXTCAP_DBC_DEVINFO2),
80 };
81 
82 static struct dentry *xhci_debugfs_root;
83 
84 static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
85 {
86 	struct xhci_regset	*regset;
87 
88 	regset = kzalloc(sizeof(*regset), GFP_KERNEL);
89 	if (!regset)
90 		return NULL;
91 
92 	/*
93 	 * The allocation and free of regset are executed in order.
94 	 * We needn't a lock here.
95 	 */
96 	INIT_LIST_HEAD(&regset->list);
97 	list_add_tail(&regset->list, &xhci->regset_list);
98 
99 	return regset;
100 }
101 
102 static void xhci_debugfs_free_regset(struct xhci_regset *regset)
103 {
104 	if (!regset)
105 		return;
106 
107 	list_del(&regset->list);
108 	kfree(regset);
109 }
110 
111 static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
112 				const struct debugfs_reg32 *regs,
113 				size_t nregs, struct dentry *parent,
114 				const char *fmt, ...)
115 {
116 	struct xhci_regset	*rgs;
117 	va_list			args;
118 	struct debugfs_regset32	*regset;
119 	struct usb_hcd		*hcd = xhci_to_hcd(xhci);
120 
121 	rgs = xhci_debugfs_alloc_regset(xhci);
122 	if (!rgs)
123 		return;
124 
125 	va_start(args, fmt);
126 	vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
127 	va_end(args);
128 
129 	regset = &rgs->regset;
130 	regset->regs = regs;
131 	regset->nregs = nregs;
132 	regset->base = hcd->regs + base;
133 
134 	debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
135 }
136 
137 static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
138 				       const struct debugfs_reg32 *regs,
139 				       size_t n, const char *cap_name)
140 {
141 	u32			offset;
142 	int			index = 0;
143 	size_t			psic, nregs = n;
144 	void __iomem		*base = &xhci->cap_regs->hc_capbase;
145 
146 	offset = xhci_find_next_ext_cap(base, 0, cap_id);
147 	while (offset) {
148 		if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
149 			psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
150 			nregs = min(4 + psic, n);
151 		}
152 
153 		xhci_debugfs_regset(xhci, offset, regs, nregs,
154 				    xhci->debugfs_root, "%s:%02d",
155 				    cap_name, index);
156 		offset = xhci_find_next_ext_cap(base, offset, cap_id);
157 		index++;
158 	}
159 }
160 
161 static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
162 {
163 	dma_addr_t		dma;
164 	struct xhci_ring	*ring = s->private;
165 
166 	dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
167 	seq_printf(s, "%pad\n", &dma);
168 
169 	return 0;
170 }
171 
172 static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
173 {
174 	dma_addr_t		dma;
175 	struct xhci_ring	*ring = s->private;
176 
177 	dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
178 	seq_printf(s, "%pad\n", &dma);
179 
180 	return 0;
181 }
182 
183 static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
184 {
185 	struct xhci_ring	*ring = s->private;
186 
187 	seq_printf(s, "%d\n", ring->cycle_state);
188 
189 	return 0;
190 }
191 
192 static void xhci_ring_dump_segment(struct seq_file *s,
193 				   struct xhci_segment *seg)
194 {
195 	int			i;
196 	dma_addr_t		dma;
197 	union xhci_trb		*trb;
198 
199 	for (i = 0; i < TRBS_PER_SEGMENT; i++) {
200 		trb = &seg->trbs[i];
201 		dma = seg->dma + i * sizeof(*trb);
202 		seq_printf(s, "%pad: %s\n", &dma,
203 			   xhci_decode_trb(trb->generic.field[0],
204 					   trb->generic.field[1],
205 					   trb->generic.field[2],
206 					   trb->generic.field[3]));
207 	}
208 }
209 
210 static int xhci_ring_trb_show(struct seq_file *s, void *unused)
211 {
212 	int			i;
213 	struct xhci_ring	*ring = s->private;
214 	struct xhci_segment	*seg = ring->first_seg;
215 
216 	for (i = 0; i < ring->num_segs; i++) {
217 		xhci_ring_dump_segment(s, seg);
218 		seg = seg->next;
219 	}
220 
221 	return 0;
222 }
223 
224 static struct xhci_file_map ring_files[] = {
225 	{"enqueue",		xhci_ring_enqueue_show, },
226 	{"dequeue",		xhci_ring_dequeue_show, },
227 	{"cycle",		xhci_ring_cycle_show, },
228 	{"trbs",		xhci_ring_trb_show, },
229 };
230 
231 static int xhci_ring_open(struct inode *inode, struct file *file)
232 {
233 	int			i;
234 	struct xhci_file_map	*f_map;
235 	const char		*file_name = file_dentry(file)->d_iname;
236 
237 	for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
238 		f_map = &ring_files[i];
239 
240 		if (strcmp(f_map->name, file_name) == 0)
241 			break;
242 	}
243 
244 	return single_open(file, f_map->show, inode->i_private);
245 }
246 
247 static const struct file_operations xhci_ring_fops = {
248 	.open			= xhci_ring_open,
249 	.read			= seq_read,
250 	.llseek			= seq_lseek,
251 	.release		= single_release,
252 };
253 
254 static int xhci_slot_context_show(struct seq_file *s, void *unused)
255 {
256 	struct xhci_hcd		*xhci;
257 	struct xhci_slot_ctx	*slot_ctx;
258 	struct xhci_slot_priv	*priv = s->private;
259 	struct xhci_virt_device	*dev = priv->dev;
260 
261 	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
262 	slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
263 	seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
264 		   xhci_decode_slot_context(slot_ctx->dev_info,
265 					    slot_ctx->dev_info2,
266 					    slot_ctx->tt_info,
267 					    slot_ctx->dev_state));
268 
269 	return 0;
270 }
271 
272 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
273 {
274 	int			dci;
275 	dma_addr_t		dma;
276 	struct xhci_hcd		*xhci;
277 	struct xhci_ep_ctx	*ep_ctx;
278 	struct xhci_slot_priv	*priv = s->private;
279 	struct xhci_virt_device	*dev = priv->dev;
280 
281 	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
282 
283 	for (dci = 1; dci < 32; dci++) {
284 		ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, dci);
285 		dma = dev->out_ctx->dma + dci * CTX_SIZE(xhci->hcc_params);
286 		seq_printf(s, "%pad: %s\n", &dma,
287 			   xhci_decode_ep_context(ep_ctx->ep_info,
288 						  ep_ctx->ep_info2,
289 						  ep_ctx->deq,
290 						  ep_ctx->tx_info));
291 	}
292 
293 	return 0;
294 }
295 
296 static int xhci_device_name_show(struct seq_file *s, void *unused)
297 {
298 	struct xhci_slot_priv	*priv = s->private;
299 	struct xhci_virt_device	*dev = priv->dev;
300 
301 	seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
302 
303 	return 0;
304 }
305 
306 static struct xhci_file_map context_files[] = {
307 	{"name",		xhci_device_name_show, },
308 	{"slot-context",	xhci_slot_context_show, },
309 	{"ep-context",		xhci_endpoint_context_show, },
310 };
311 
312 static int xhci_context_open(struct inode *inode, struct file *file)
313 {
314 	int			i;
315 	struct xhci_file_map	*f_map;
316 	const char		*file_name = file_dentry(file)->d_iname;
317 
318 	for (i = 0; i < ARRAY_SIZE(context_files); i++) {
319 		f_map = &context_files[i];
320 
321 		if (strcmp(f_map->name, file_name) == 0)
322 			break;
323 	}
324 
325 	return single_open(file, f_map->show, inode->i_private);
326 }
327 
328 static const struct file_operations xhci_context_fops = {
329 	.open			= xhci_context_open,
330 	.read			= seq_read,
331 	.llseek			= seq_lseek,
332 	.release		= single_release,
333 };
334 
335 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
336 				      struct xhci_file_map *files,
337 				      size_t nentries, void *data,
338 				      struct dentry *parent,
339 				      const struct file_operations *fops)
340 {
341 	int			i;
342 
343 	for (i = 0; i < nentries; i++)
344 		debugfs_create_file(files[i].name, 0444, parent, data, fops);
345 }
346 
347 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
348 						   struct xhci_ring *ring,
349 						   const char *name,
350 						   struct dentry *parent)
351 {
352 	struct dentry		*dir;
353 
354 	dir = debugfs_create_dir(name, parent);
355 	xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
356 				  ring, dir, &xhci_ring_fops);
357 
358 	return dir;
359 }
360 
361 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
362 					      struct dentry *parent,
363 					      int slot_id)
364 {
365 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
366 
367 	xhci_debugfs_create_files(xhci, context_files,
368 				  ARRAY_SIZE(context_files),
369 				  dev->debugfs_private,
370 				  parent, &xhci_context_fops);
371 }
372 
373 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
374 				  struct xhci_virt_device *dev,
375 				  int ep_index)
376 {
377 	struct xhci_ep_priv	*epriv;
378 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
379 
380 	if (spriv->eps[ep_index])
381 		return;
382 
383 	epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
384 	if (!epriv)
385 		return;
386 
387 	snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
388 	epriv->root = xhci_debugfs_create_ring_dir(xhci,
389 						   dev->eps[ep_index].new_ring,
390 						   epriv->name,
391 						   spriv->root);
392 	spriv->eps[ep_index] = epriv;
393 }
394 
395 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
396 				  struct xhci_virt_device *dev,
397 				  int ep_index)
398 {
399 	struct xhci_ep_priv	*epriv;
400 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
401 
402 	if (!spriv || !spriv->eps[ep_index])
403 		return;
404 
405 	epriv = spriv->eps[ep_index];
406 	debugfs_remove_recursive(epriv->root);
407 	spriv->eps[ep_index] = NULL;
408 	kfree(epriv);
409 }
410 
411 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
412 {
413 	struct xhci_slot_priv	*priv;
414 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
415 
416 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
417 	if (!priv)
418 		return;
419 
420 	snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
421 	priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
422 	priv->dev = dev;
423 	dev->debugfs_private = priv;
424 
425 	xhci_debugfs_create_ring_dir(xhci, dev->eps[0].ring,
426 				     "ep00", priv->root);
427 
428 	xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
429 }
430 
431 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
432 {
433 	int			i;
434 	struct xhci_slot_priv	*priv;
435 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
436 
437 	if (!dev || !dev->debugfs_private)
438 		return;
439 
440 	priv = dev->debugfs_private;
441 
442 	debugfs_remove_recursive(priv->root);
443 
444 	for (i = 0; i < 31; i++)
445 		kfree(priv->eps[i]);
446 
447 	kfree(priv);
448 	dev->debugfs_private = NULL;
449 }
450 
451 void xhci_debugfs_init(struct xhci_hcd *xhci)
452 {
453 	struct device		*dev = xhci_to_hcd(xhci)->self.controller;
454 
455 	xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
456 						xhci_debugfs_root);
457 
458 	INIT_LIST_HEAD(&xhci->regset_list);
459 
460 	xhci_debugfs_regset(xhci,
461 			    0,
462 			    xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
463 			    xhci->debugfs_root, "reg-cap");
464 
465 	xhci_debugfs_regset(xhci,
466 			    HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
467 			    xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
468 			    xhci->debugfs_root, "reg-op");
469 
470 	xhci_debugfs_regset(xhci,
471 			    readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
472 			    xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
473 			    xhci->debugfs_root, "reg-runtime");
474 
475 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
476 				   xhci_extcap_legsup,
477 				   ARRAY_SIZE(xhci_extcap_legsup),
478 				   "reg-ext-legsup");
479 
480 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
481 				   xhci_extcap_protocol,
482 				   ARRAY_SIZE(xhci_extcap_protocol),
483 				   "reg-ext-protocol");
484 
485 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
486 				   xhci_extcap_dbc,
487 				   ARRAY_SIZE(xhci_extcap_dbc),
488 				   "reg-ext-dbc");
489 
490 	xhci_debugfs_create_ring_dir(xhci, xhci->cmd_ring,
491 				     "command-ring",
492 				     xhci->debugfs_root);
493 
494 	xhci_debugfs_create_ring_dir(xhci, xhci->event_ring,
495 				     "event-ring",
496 				     xhci->debugfs_root);
497 
498 	xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
499 }
500 
501 void xhci_debugfs_exit(struct xhci_hcd *xhci)
502 {
503 	struct xhci_regset	*rgs, *tmp;
504 
505 	debugfs_remove_recursive(xhci->debugfs_root);
506 	xhci->debugfs_root = NULL;
507 	xhci->debugfs_slots = NULL;
508 
509 	list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
510 		xhci_debugfs_free_regset(rgs);
511 }
512 
513 void __init xhci_debugfs_create_root(void)
514 {
515 	xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
516 }
517 
518 void __exit xhci_debugfs_remove_root(void)
519 {
520 	debugfs_remove_recursive(xhci_debugfs_root);
521 	xhci_debugfs_root = NULL;
522 }
523