xref: /linux/arch/s390/pci/pci_debug.c (revision 982164d62a4b2097c0db28ae7c31fc905af26bb8)
1adbb3901SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d0b08853SJan Glauber /*
3f9dc447eSSebastian Ott  *  Copyright IBM Corp. 2012,2015
4d0b08853SJan Glauber  *
5d0b08853SJan Glauber  *  Author(s):
6d0b08853SJan Glauber  *    Jan Glauber <jang@linux.vnet.ibm.com>
7d0b08853SJan Glauber  */
8d0b08853SJan Glauber 
9896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci"
10896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11d0b08853SJan Glauber 
12d0b08853SJan Glauber #include <linux/kernel.h>
13d0b08853SJan Glauber #include <linux/seq_file.h>
14d0b08853SJan Glauber #include <linux/debugfs.h>
15a2ab8333SSebastian Ott #include <linux/export.h>
16d0b08853SJan Glauber #include <linux/pci.h>
17d0b08853SJan Glauber #include <asm/debug.h>
18d0b08853SJan Glauber 
19d0b08853SJan Glauber #include <asm/pci_dma.h>
20d0b08853SJan Glauber 
21d0b08853SJan Glauber static struct dentry *debugfs_root;
22a2ab8333SSebastian Ott debug_info_t *pci_debug_msg_id;
23a2ab8333SSebastian Ott EXPORT_SYMBOL_GPL(pci_debug_msg_id);
24a2ab8333SSebastian Ott debug_info_t *pci_debug_err_id;
25a2ab8333SSebastian Ott EXPORT_SYMBOL_GPL(pci_debug_err_id);
26d0b08853SJan Glauber 
27f9dc447eSSebastian Ott static char *pci_common_names[] = {
28d0b08853SJan Glauber 	"Load operations",
29d0b08853SJan Glauber 	"Store operations",
30d0b08853SJan Glauber 	"Store block operations",
31d0b08853SJan Glauber 	"Refresh operations",
32f9dc447eSSebastian Ott };
33f9dc447eSSebastian Ott 
34f9dc447eSSebastian Ott static char *pci_fmt0_names[] = {
35d0b08853SJan Glauber 	"DMA read bytes",
36d0b08853SJan Glauber 	"DMA write bytes",
376001018aSSebastian Ott };
386001018aSSebastian Ott 
39f9dc447eSSebastian Ott static char *pci_fmt1_names[] = {
40f9dc447eSSebastian Ott 	"Received bytes",
41f9dc447eSSebastian Ott 	"Received packets",
42f9dc447eSSebastian Ott 	"Transmitted bytes",
43f9dc447eSSebastian Ott 	"Transmitted packets",
44f9dc447eSSebastian Ott };
45f9dc447eSSebastian Ott 
46f9dc447eSSebastian Ott static char *pci_fmt2_names[] = {
47f9dc447eSSebastian Ott 	"Consumed work units",
48f9dc447eSSebastian Ott 	"Maximum work units",
49f9dc447eSSebastian Ott };
50f9dc447eSSebastian Ott 
510ac94282SSebastian Ott static char *pci_fmt3_names[] = {
520ac94282SSebastian Ott 	"Transmitted bytes",
530ac94282SSebastian Ott };
540ac94282SSebastian Ott 
556001018aSSebastian Ott static char *pci_sw_names[] = {
56d0b08853SJan Glauber 	"Allocated pages",
57d0b08853SJan Glauber 	"Mapped pages",
58d0b08853SJan Glauber 	"Unmapped pages",
59d0b08853SJan Glauber };
60d0b08853SJan Glauber 
61f9dc447eSSebastian Ott static void pci_fmb_show(struct seq_file *m, char *name[], int length,
62f9dc447eSSebastian Ott 			 u64 *data)
63f9dc447eSSebastian Ott {
64f9dc447eSSebastian Ott 	int i;
65f9dc447eSSebastian Ott 
66f9dc447eSSebastian Ott 	for (i = 0; i < length; i++, data++)
67f9dc447eSSebastian Ott 		seq_printf(m, "%26s:\t%llu\n", name[i], *data);
68f9dc447eSSebastian Ott }
69f9dc447eSSebastian Ott 
706001018aSSebastian Ott static void pci_sw_counter_show(struct seq_file *m)
716001018aSSebastian Ott {
726001018aSSebastian Ott 	struct zpci_dev *zdev = m->private;
736001018aSSebastian Ott 	atomic64_t *counter = &zdev->allocated_pages;
746001018aSSebastian Ott 	int i;
756001018aSSebastian Ott 
766001018aSSebastian Ott 	for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++)
77*982164d6SMark Rutland 		seq_printf(m, "%26s:\t%llu\n", pci_sw_names[i],
78*982164d6SMark Rutland 			   (s64)atomic64_read(counter));
796001018aSSebastian Ott }
806001018aSSebastian Ott 
81d0b08853SJan Glauber static int pci_perf_show(struct seq_file *m, void *v)
82d0b08853SJan Glauber {
83d0b08853SJan Glauber 	struct zpci_dev *zdev = m->private;
84d0b08853SJan Glauber 
85d0b08853SJan Glauber 	if (!zdev)
86d0b08853SJan Glauber 		return 0;
8780ed156aSSebastian Ott 
8880ed156aSSebastian Ott 	mutex_lock(&zdev->lock);
89c2f0b61dSJoe Perches 	if (!zdev->fmb) {
9080ed156aSSebastian Ott 		mutex_unlock(&zdev->lock);
91c2f0b61dSJoe Perches 		seq_puts(m, "FMB statistics disabled\n");
92c2f0b61dSJoe Perches 		return 0;
93c2f0b61dSJoe Perches 	}
94d0b08853SJan Glauber 
95d0b08853SJan Glauber 	/* header */
96d0b08853SJan Glauber 	seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
97d0b08853SJan Glauber 	seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
98d0b08853SJan Glauber 	seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
99d0b08853SJan Glauber 
100f9dc447eSSebastian Ott 	pci_fmb_show(m, pci_common_names, ARRAY_SIZE(pci_common_names),
101f9dc447eSSebastian Ott 		     &zdev->fmb->ld_ops);
102f9dc447eSSebastian Ott 
103f9dc447eSSebastian Ott 	switch (zdev->fmb->format) {
104f9dc447eSSebastian Ott 	case 0:
105f9dc447eSSebastian Ott 		if (!(zdev->fmb->fmt_ind & ZPCI_FMB_DMA_COUNTER_VALID))
106f9dc447eSSebastian Ott 			break;
107f9dc447eSSebastian Ott 		pci_fmb_show(m, pci_fmt0_names, ARRAY_SIZE(pci_fmt0_names),
108f9dc447eSSebastian Ott 			     &zdev->fmb->fmt0.dma_rbytes);
109f9dc447eSSebastian Ott 		break;
110f9dc447eSSebastian Ott 	case 1:
111f9dc447eSSebastian Ott 		pci_fmb_show(m, pci_fmt1_names, ARRAY_SIZE(pci_fmt1_names),
112f9dc447eSSebastian Ott 			     &zdev->fmb->fmt1.rx_bytes);
113f9dc447eSSebastian Ott 		break;
114f9dc447eSSebastian Ott 	case 2:
115f9dc447eSSebastian Ott 		pci_fmb_show(m, pci_fmt2_names, ARRAY_SIZE(pci_fmt2_names),
116f9dc447eSSebastian Ott 			     &zdev->fmb->fmt2.consumed_work_units);
117f9dc447eSSebastian Ott 		break;
1180ac94282SSebastian Ott 	case 3:
1190ac94282SSebastian Ott 		pci_fmb_show(m, pci_fmt3_names, ARRAY_SIZE(pci_fmt3_names),
1200ac94282SSebastian Ott 			     &zdev->fmb->fmt3.tx_bytes);
1210ac94282SSebastian Ott 		break;
122f9dc447eSSebastian Ott 	default:
123f9dc447eSSebastian Ott 		seq_puts(m, "Unknown format\n");
124f9dc447eSSebastian Ott 	}
125d0b08853SJan Glauber 
1266001018aSSebastian Ott 	pci_sw_counter_show(m);
12780ed156aSSebastian Ott 	mutex_unlock(&zdev->lock);
128d0b08853SJan Glauber 	return 0;
129d0b08853SJan Glauber }
130d0b08853SJan Glauber 
131d0b08853SJan Glauber static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
132d0b08853SJan Glauber 				  size_t count, loff_t *off)
133d0b08853SJan Glauber {
134d0b08853SJan Glauber 	struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
135d0b08853SJan Glauber 	unsigned long val;
136d0b08853SJan Glauber 	int rc;
137d0b08853SJan Glauber 
138d0b08853SJan Glauber 	if (!zdev)
139d0b08853SJan Glauber 		return 0;
140d0b08853SJan Glauber 
141d0b08853SJan Glauber 	rc = kstrtoul_from_user(ubuf, count, 10, &val);
142d0b08853SJan Glauber 	if (rc)
143d0b08853SJan Glauber 		return rc;
144d0b08853SJan Glauber 
14580ed156aSSebastian Ott 	mutex_lock(&zdev->lock);
146d0b08853SJan Glauber 	switch (val) {
147d0b08853SJan Glauber 	case 0:
148d0b08853SJan Glauber 		rc = zpci_fmb_disable_device(zdev);
149d0b08853SJan Glauber 		break;
150d0b08853SJan Glauber 	case 1:
151d0b08853SJan Glauber 		rc = zpci_fmb_enable_device(zdev);
152d0b08853SJan Glauber 		break;
153d0b08853SJan Glauber 	}
15480ed156aSSebastian Ott 	mutex_unlock(&zdev->lock);
15580ed156aSSebastian Ott 	return rc ? rc : count;
156d0b08853SJan Glauber }
157d0b08853SJan Glauber 
158d0b08853SJan Glauber static int pci_perf_seq_open(struct inode *inode, struct file *filp)
159d0b08853SJan Glauber {
160d0b08853SJan Glauber 	return single_open(filp, pci_perf_show,
161496ad9aaSAl Viro 			   file_inode(filp)->i_private);
162d0b08853SJan Glauber }
163d0b08853SJan Glauber 
164d0b08853SJan Glauber static const struct file_operations debugfs_pci_perf_fops = {
165d0b08853SJan Glauber 	.open	 = pci_perf_seq_open,
166d0b08853SJan Glauber 	.read	 = seq_read,
167d0b08853SJan Glauber 	.write	 = pci_perf_seq_write,
168d0b08853SJan Glauber 	.llseek  = seq_lseek,
169d0b08853SJan Glauber 	.release = single_release,
170d0b08853SJan Glauber };
171d0b08853SJan Glauber 
1729a99649fSSebastian Ott void zpci_debug_init_device(struct zpci_dev *zdev, const char *name)
173d0b08853SJan Glauber {
1749a99649fSSebastian Ott 	zdev->debugfs_dev = debugfs_create_dir(name, debugfs_root);
175d0b08853SJan Glauber 
176d7f2f7c7SGreg Kroah-Hartman 	debugfs_create_file("statistics", S_IFREG | S_IRUGO | S_IWUSR,
177d7f2f7c7SGreg Kroah-Hartman 			    zdev->debugfs_dev, zdev, &debugfs_pci_perf_fops);
178d0b08853SJan Glauber }
179d0b08853SJan Glauber 
180d0b08853SJan Glauber void zpci_debug_exit_device(struct zpci_dev *zdev)
181d0b08853SJan Glauber {
182d7f2f7c7SGreg Kroah-Hartman 	debugfs_remove_recursive(zdev->debugfs_dev);
183d0b08853SJan Glauber }
184d0b08853SJan Glauber 
185d0b08853SJan Glauber int __init zpci_debug_init(void)
186d0b08853SJan Glauber {
187d0b08853SJan Glauber 	/* event trace buffer */
188f7e1e65dSSebastian Ott 	pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
189d0b08853SJan Glauber 	if (!pci_debug_msg_id)
190d0b08853SJan Glauber 		return -EINVAL;
191d0b08853SJan Glauber 	debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
192d0b08853SJan Glauber 	debug_set_level(pci_debug_msg_id, 3);
193d0b08853SJan Glauber 
194d0b08853SJan Glauber 	/* error log */
195d0b08853SJan Glauber 	pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
196d0b08853SJan Glauber 	if (!pci_debug_err_id)
197d0b08853SJan Glauber 		return -EINVAL;
198d0b08853SJan Glauber 	debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
199d0b08853SJan Glauber 	debug_set_level(pci_debug_err_id, 6);
200d0b08853SJan Glauber 
201d0b08853SJan Glauber 	debugfs_root = debugfs_create_dir("pci", NULL);
202d0b08853SJan Glauber 	return 0;
203d0b08853SJan Glauber }
204d0b08853SJan Glauber 
205d0b08853SJan Glauber void zpci_debug_exit(void)
206d0b08853SJan Glauber {
207d0b08853SJan Glauber 	debug_unregister(pci_debug_msg_id);
208d0b08853SJan Glauber 	debug_unregister(pci_debug_err_id);
209d0b08853SJan Glauber 	debugfs_remove(debugfs_root);
210d0b08853SJan Glauber }
211