xref: /openbmc/linux/arch/s390/pci/pci_debug.c (revision a99237af)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright IBM Corp. 2012,2015
4  *
5  *  Author(s):
6  *    Jan Glauber <jang@linux.vnet.ibm.com>
7  */
8 
9 #define KMSG_COMPONENT "zpci"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/kernel.h>
13 #include <linux/seq_file.h>
14 #include <linux/debugfs.h>
15 #include <linux/export.h>
16 #include <linux/pci.h>
17 #include <asm/debug.h>
18 
19 #include <asm/pci_dma.h>
20 
21 static struct dentry *debugfs_root;
22 debug_info_t *pci_debug_msg_id;
23 EXPORT_SYMBOL_GPL(pci_debug_msg_id);
24 debug_info_t *pci_debug_err_id;
25 EXPORT_SYMBOL_GPL(pci_debug_err_id);
26 
27 static char *pci_common_names[] = {
28 	"Load operations",
29 	"Store operations",
30 	"Store block operations",
31 	"Refresh operations",
32 };
33 
34 static char *pci_fmt0_names[] = {
35 	"DMA read bytes",
36 	"DMA write bytes",
37 };
38 
39 static char *pci_fmt1_names[] = {
40 	"Received bytes",
41 	"Received packets",
42 	"Transmitted bytes",
43 	"Transmitted packets",
44 };
45 
46 static char *pci_fmt2_names[] = {
47 	"Consumed work units",
48 	"Maximum work units",
49 };
50 
51 static char *pci_fmt3_names[] = {
52 	"Transmitted bytes",
53 };
54 
55 static char *pci_sw_names[] = {
56 	"Allocated pages",
57 	"Mapped pages",
58 	"Unmapped pages",
59 };
60 
61 static void pci_fmb_show(struct seq_file *m, char *name[], int length,
62 			 u64 *data)
63 {
64 	int i;
65 
66 	for (i = 0; i < length; i++, data++)
67 		seq_printf(m, "%26s:\t%llu\n", name[i], *data);
68 }
69 
70 static void pci_sw_counter_show(struct seq_file *m)
71 {
72 	struct zpci_dev *zdev = m->private;
73 	atomic64_t *counter = &zdev->allocated_pages;
74 	int i;
75 
76 	for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++)
77 		seq_printf(m, "%26s:\t%lu\n", pci_sw_names[i],
78 			   atomic64_read(counter));
79 }
80 
81 static int pci_perf_show(struct seq_file *m, void *v)
82 {
83 	struct zpci_dev *zdev = m->private;
84 
85 	if (!zdev)
86 		return 0;
87 
88 	mutex_lock(&zdev->lock);
89 	if (!zdev->fmb) {
90 		mutex_unlock(&zdev->lock);
91 		seq_puts(m, "FMB statistics disabled\n");
92 		return 0;
93 	}
94 
95 	/* header */
96 	seq_printf(m, "FMB @ %p\n", zdev->fmb);
97 	seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
98 	seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
99 	seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
100 
101 	pci_fmb_show(m, pci_common_names, ARRAY_SIZE(pci_common_names),
102 		     &zdev->fmb->ld_ops);
103 
104 	switch (zdev->fmb->format) {
105 	case 0:
106 		if (!(zdev->fmb->fmt_ind & ZPCI_FMB_DMA_COUNTER_VALID))
107 			break;
108 		pci_fmb_show(m, pci_fmt0_names, ARRAY_SIZE(pci_fmt0_names),
109 			     &zdev->fmb->fmt0.dma_rbytes);
110 		break;
111 	case 1:
112 		pci_fmb_show(m, pci_fmt1_names, ARRAY_SIZE(pci_fmt1_names),
113 			     &zdev->fmb->fmt1.rx_bytes);
114 		break;
115 	case 2:
116 		pci_fmb_show(m, pci_fmt2_names, ARRAY_SIZE(pci_fmt2_names),
117 			     &zdev->fmb->fmt2.consumed_work_units);
118 		break;
119 	case 3:
120 		pci_fmb_show(m, pci_fmt3_names, ARRAY_SIZE(pci_fmt3_names),
121 			     &zdev->fmb->fmt3.tx_bytes);
122 		break;
123 	default:
124 		seq_puts(m, "Unknown format\n");
125 	}
126 
127 	pci_sw_counter_show(m);
128 	mutex_unlock(&zdev->lock);
129 	return 0;
130 }
131 
132 static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
133 				  size_t count, loff_t *off)
134 {
135 	struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
136 	unsigned long val;
137 	int rc;
138 
139 	if (!zdev)
140 		return 0;
141 
142 	rc = kstrtoul_from_user(ubuf, count, 10, &val);
143 	if (rc)
144 		return rc;
145 
146 	mutex_lock(&zdev->lock);
147 	switch (val) {
148 	case 0:
149 		rc = zpci_fmb_disable_device(zdev);
150 		break;
151 	case 1:
152 		rc = zpci_fmb_enable_device(zdev);
153 		break;
154 	}
155 	mutex_unlock(&zdev->lock);
156 	return rc ? rc : count;
157 }
158 
159 static int pci_perf_seq_open(struct inode *inode, struct file *filp)
160 {
161 	return single_open(filp, pci_perf_show,
162 			   file_inode(filp)->i_private);
163 }
164 
165 static const struct file_operations debugfs_pci_perf_fops = {
166 	.open	 = pci_perf_seq_open,
167 	.read	 = seq_read,
168 	.write	 = pci_perf_seq_write,
169 	.llseek  = seq_lseek,
170 	.release = single_release,
171 };
172 
173 void zpci_debug_init_device(struct zpci_dev *zdev, const char *name)
174 {
175 	zdev->debugfs_dev = debugfs_create_dir(name, debugfs_root);
176 	if (IS_ERR(zdev->debugfs_dev))
177 		zdev->debugfs_dev = NULL;
178 
179 	zdev->debugfs_perf = debugfs_create_file("statistics",
180 				S_IFREG | S_IRUGO | S_IWUSR,
181 				zdev->debugfs_dev, zdev,
182 				&debugfs_pci_perf_fops);
183 	if (IS_ERR(zdev->debugfs_perf))
184 		zdev->debugfs_perf = NULL;
185 }
186 
187 void zpci_debug_exit_device(struct zpci_dev *zdev)
188 {
189 	debugfs_remove(zdev->debugfs_perf);
190 	debugfs_remove(zdev->debugfs_dev);
191 }
192 
193 int __init zpci_debug_init(void)
194 {
195 	/* event trace buffer */
196 	pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
197 	if (!pci_debug_msg_id)
198 		return -EINVAL;
199 	debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
200 	debug_set_level(pci_debug_msg_id, 3);
201 
202 	/* error log */
203 	pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
204 	if (!pci_debug_err_id)
205 		return -EINVAL;
206 	debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
207 	debug_set_level(pci_debug_err_id, 6);
208 
209 	debugfs_root = debugfs_create_dir("pci", NULL);
210 	return 0;
211 }
212 
213 void zpci_debug_exit(void)
214 {
215 	debug_unregister(pci_debug_msg_id);
216 	debug_unregister(pci_debug_err_id);
217 	debugfs_remove(debugfs_root);
218 }
219