xref: /linux/arch/s390/pci/pci_debug.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 	"Mapped pages",
57d0b08853SJan Glauber 	"Unmapped pages",
58c76c067eSNiklas Schnelle 	"Global RPCITs",
59c76c067eSNiklas Schnelle 	"Sync Map RPCITs",
60c76c067eSNiklas Schnelle 	"Sync RPCITs",
61d0b08853SJan Glauber };
62d0b08853SJan Glauber 
pci_fmb_show(struct seq_file * m,char * name[],int length,u64 * data)63f9dc447eSSebastian Ott static void pci_fmb_show(struct seq_file *m, char *name[], int length,
64f9dc447eSSebastian Ott 			 u64 *data)
65f9dc447eSSebastian Ott {
66f9dc447eSSebastian Ott 	int i;
67f9dc447eSSebastian Ott 
68f9dc447eSSebastian Ott 	for (i = 0; i < length; i++, data++)
69f9dc447eSSebastian Ott 		seq_printf(m, "%26s:\t%llu\n", name[i], *data);
70f9dc447eSSebastian Ott }
71f9dc447eSSebastian Ott 
pci_sw_counter_show(struct seq_file * m)726001018aSSebastian Ott static void pci_sw_counter_show(struct seq_file *m)
736001018aSSebastian Ott {
74c76c067eSNiklas Schnelle 	struct zpci_iommu_ctrs  *ctrs = zpci_get_iommu_ctrs(m->private);
75c76c067eSNiklas Schnelle 	atomic64_t *counter;
766001018aSSebastian Ott 	int i;
776001018aSSebastian Ott 
78c76c067eSNiklas Schnelle 	if (!ctrs)
79c76c067eSNiklas Schnelle 		return;
80c76c067eSNiklas Schnelle 
81c76c067eSNiklas Schnelle 	counter = &ctrs->mapped_pages;
826001018aSSebastian Ott 	for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++)
83982164d6SMark Rutland 		seq_printf(m, "%26s:\t%llu\n", pci_sw_names[i],
846a6a9d5fSMark Rutland 			   atomic64_read(counter));
856001018aSSebastian Ott }
866001018aSSebastian Ott 
pci_perf_show(struct seq_file * m,void * v)87d0b08853SJan Glauber static int pci_perf_show(struct seq_file *m, void *v)
88d0b08853SJan Glauber {
89d0b08853SJan Glauber 	struct zpci_dev *zdev = m->private;
90d0b08853SJan Glauber 
91d0b08853SJan Glauber 	if (!zdev)
92d0b08853SJan Glauber 		return 0;
9380ed156aSSebastian Ott 
94*0d48566dSGerd Bayer 	mutex_lock(&zdev->fmb_lock);
95c2f0b61dSJoe Perches 	if (!zdev->fmb) {
96*0d48566dSGerd Bayer 		mutex_unlock(&zdev->fmb_lock);
97c2f0b61dSJoe Perches 		seq_puts(m, "FMB statistics disabled\n");
98c2f0b61dSJoe Perches 		return 0;
99c2f0b61dSJoe Perches 	}
100d0b08853SJan Glauber 
101d0b08853SJan Glauber 	/* header */
102d0b08853SJan Glauber 	seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
103d0b08853SJan Glauber 	seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
104d0b08853SJan Glauber 	seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
105d0b08853SJan Glauber 
106f9dc447eSSebastian Ott 	pci_fmb_show(m, pci_common_names, ARRAY_SIZE(pci_common_names),
107f9dc447eSSebastian Ott 		     &zdev->fmb->ld_ops);
108f9dc447eSSebastian Ott 
109f9dc447eSSebastian Ott 	switch (zdev->fmb->format) {
110f9dc447eSSebastian Ott 	case 0:
111f9dc447eSSebastian Ott 		if (!(zdev->fmb->fmt_ind & ZPCI_FMB_DMA_COUNTER_VALID))
112f9dc447eSSebastian Ott 			break;
113f9dc447eSSebastian Ott 		pci_fmb_show(m, pci_fmt0_names, ARRAY_SIZE(pci_fmt0_names),
114f9dc447eSSebastian Ott 			     &zdev->fmb->fmt0.dma_rbytes);
115f9dc447eSSebastian Ott 		break;
116f9dc447eSSebastian Ott 	case 1:
117f9dc447eSSebastian Ott 		pci_fmb_show(m, pci_fmt1_names, ARRAY_SIZE(pci_fmt1_names),
118f9dc447eSSebastian Ott 			     &zdev->fmb->fmt1.rx_bytes);
119f9dc447eSSebastian Ott 		break;
120f9dc447eSSebastian Ott 	case 2:
121f9dc447eSSebastian Ott 		pci_fmb_show(m, pci_fmt2_names, ARRAY_SIZE(pci_fmt2_names),
122f9dc447eSSebastian Ott 			     &zdev->fmb->fmt2.consumed_work_units);
123f9dc447eSSebastian Ott 		break;
1240ac94282SSebastian Ott 	case 3:
1250ac94282SSebastian Ott 		pci_fmb_show(m, pci_fmt3_names, ARRAY_SIZE(pci_fmt3_names),
1260ac94282SSebastian Ott 			     &zdev->fmb->fmt3.tx_bytes);
1270ac94282SSebastian Ott 		break;
128f9dc447eSSebastian Ott 	default:
129f9dc447eSSebastian Ott 		seq_puts(m, "Unknown format\n");
130f9dc447eSSebastian Ott 	}
131d0b08853SJan Glauber 
1326001018aSSebastian Ott 	pci_sw_counter_show(m);
133*0d48566dSGerd Bayer 	mutex_unlock(&zdev->fmb_lock);
134d0b08853SJan Glauber 	return 0;
135d0b08853SJan Glauber }
136d0b08853SJan Glauber 
pci_perf_seq_write(struct file * file,const char __user * ubuf,size_t count,loff_t * off)137d0b08853SJan Glauber static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
138d0b08853SJan Glauber 				  size_t count, loff_t *off)
139d0b08853SJan Glauber {
140d0b08853SJan Glauber 	struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
141d0b08853SJan Glauber 	unsigned long val;
142d0b08853SJan Glauber 	int rc;
143d0b08853SJan Glauber 
144d0b08853SJan Glauber 	if (!zdev)
145d0b08853SJan Glauber 		return 0;
146d0b08853SJan Glauber 
147d0b08853SJan Glauber 	rc = kstrtoul_from_user(ubuf, count, 10, &val);
148d0b08853SJan Glauber 	if (rc)
149d0b08853SJan Glauber 		return rc;
150d0b08853SJan Glauber 
151*0d48566dSGerd Bayer 	mutex_lock(&zdev->fmb_lock);
152d0b08853SJan Glauber 	switch (val) {
153d0b08853SJan Glauber 	case 0:
154d0b08853SJan Glauber 		rc = zpci_fmb_disable_device(zdev);
155d0b08853SJan Glauber 		break;
156d0b08853SJan Glauber 	case 1:
157d0b08853SJan Glauber 		rc = zpci_fmb_enable_device(zdev);
158d0b08853SJan Glauber 		break;
159d0b08853SJan Glauber 	}
160*0d48566dSGerd Bayer 	mutex_unlock(&zdev->fmb_lock);
16180ed156aSSebastian Ott 	return rc ? rc : count;
162d0b08853SJan Glauber }
163d0b08853SJan Glauber 
pci_perf_seq_open(struct inode * inode,struct file * filp)164d0b08853SJan Glauber static int pci_perf_seq_open(struct inode *inode, struct file *filp)
165d0b08853SJan Glauber {
166d0b08853SJan Glauber 	return single_open(filp, pci_perf_show,
167496ad9aaSAl Viro 			   file_inode(filp)->i_private);
168d0b08853SJan Glauber }
169d0b08853SJan Glauber 
170d0b08853SJan Glauber static const struct file_operations debugfs_pci_perf_fops = {
171d0b08853SJan Glauber 	.open	 = pci_perf_seq_open,
172d0b08853SJan Glauber 	.read	 = seq_read,
173d0b08853SJan Glauber 	.write	 = pci_perf_seq_write,
174d0b08853SJan Glauber 	.llseek  = seq_lseek,
175d0b08853SJan Glauber 	.release = single_release,
176d0b08853SJan Glauber };
177d0b08853SJan Glauber 
zpci_debug_init_device(struct zpci_dev * zdev,const char * name)1789a99649fSSebastian Ott void zpci_debug_init_device(struct zpci_dev *zdev, const char *name)
179d0b08853SJan Glauber {
1809a99649fSSebastian Ott 	zdev->debugfs_dev = debugfs_create_dir(name, debugfs_root);
181d0b08853SJan Glauber 
182d7f2f7c7SGreg Kroah-Hartman 	debugfs_create_file("statistics", S_IFREG | S_IRUGO | S_IWUSR,
183d7f2f7c7SGreg Kroah-Hartman 			    zdev->debugfs_dev, zdev, &debugfs_pci_perf_fops);
184d0b08853SJan Glauber }
185d0b08853SJan Glauber 
zpci_debug_exit_device(struct zpci_dev * zdev)186d0b08853SJan Glauber void zpci_debug_exit_device(struct zpci_dev *zdev)
187d0b08853SJan Glauber {
188d7f2f7c7SGreg Kroah-Hartman 	debugfs_remove_recursive(zdev->debugfs_dev);
189d0b08853SJan Glauber }
190d0b08853SJan Glauber 
zpci_debug_init(void)191d0b08853SJan Glauber int __init zpci_debug_init(void)
192d0b08853SJan Glauber {
193d0b08853SJan Glauber 	/* event trace buffer */
194f7e1e65dSSebastian Ott 	pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
195d0b08853SJan Glauber 	if (!pci_debug_msg_id)
196d0b08853SJan Glauber 		return -EINVAL;
197d0b08853SJan Glauber 	debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
198d0b08853SJan Glauber 	debug_set_level(pci_debug_msg_id, 3);
199d0b08853SJan Glauber 
200d0b08853SJan Glauber 	/* error log */
201d0b08853SJan Glauber 	pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
202d0b08853SJan Glauber 	if (!pci_debug_err_id)
203d0b08853SJan Glauber 		return -EINVAL;
204d0b08853SJan Glauber 	debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
20552c79e63SNiklas Schnelle 	debug_set_level(pci_debug_err_id, 3);
206d0b08853SJan Glauber 
207d0b08853SJan Glauber 	debugfs_root = debugfs_create_dir("pci", NULL);
208d0b08853SJan Glauber 	return 0;
209d0b08853SJan Glauber }
210d0b08853SJan Glauber 
zpci_debug_exit(void)211d0b08853SJan Glauber void zpci_debug_exit(void)
212d0b08853SJan Glauber {
213d0b08853SJan Glauber 	debug_unregister(pci_debug_msg_id);
214d0b08853SJan Glauber 	debug_unregister(pci_debug_err_id);
215d0b08853SJan Glauber 	debugfs_remove(debugfs_root);
216d0b08853SJan Glauber }
217