xref: /linux/arch/s390/pci/pci_sysfs.c (revision ae99fb8baafc881b35aa0b79d7ac0178a7c40c89)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2012
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/stat.h>
14 #include <linux/pci.h>
15 
16 #include "../../../drivers/pci/pci.h"
17 
18 #include <asm/sclp.h>
19 
20 #define zpci_attr(name, fmt, member)					\
21 static ssize_t name##_show(struct device *dev,				\
22 			   struct device_attribute *attr, char *buf)	\
23 {									\
24 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));		\
25 									\
26 	return sprintf(buf, fmt, zdev->member);				\
27 }									\
28 static DEVICE_ATTR_RO(name)
29 
30 zpci_attr(function_id, "0x%08x\n", fid);
31 zpci_attr(function_handle, "0x%08x\n", fh);
32 zpci_attr(pchid, "0x%04x\n", pchid);
33 zpci_attr(pfgid, "0x%02x\n", pfgid);
34 zpci_attr(vfn, "0x%04x\n", vfn);
35 zpci_attr(pft, "0x%02x\n", pft);
36 zpci_attr(uid, "0x%x\n", uid);
37 zpci_attr(segment0, "0x%02x\n", pfip[0]);
38 zpci_attr(segment1, "0x%02x\n", pfip[1]);
39 zpci_attr(segment2, "0x%02x\n", pfip[2]);
40 zpci_attr(segment3, "0x%02x\n", pfip[3]);
41 
42 static ssize_t mio_enabled_show(struct device *dev,
43 				struct device_attribute *attr, char *buf)
44 {
45 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
46 
47 	return sprintf(buf, zpci_use_mio(zdev) ? "1\n" : "0\n");
48 }
49 static DEVICE_ATTR_RO(mio_enabled);
50 
51 static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
52 			     const char *buf, size_t count)
53 {
54 	struct kernfs_node *kn;
55 	struct pci_dev *pdev = to_pci_dev(dev);
56 	struct zpci_dev *zdev = to_zpci(pdev);
57 	int ret = 0;
58 
59 	/* Can't use device_remove_self() here as that would lead us to lock
60 	 * the pci_rescan_remove_lock while holding the device' kernfs lock.
61 	 * This would create a possible deadlock with disable_slot() which is
62 	 * not directly protected by the device' kernfs lock but takes it
63 	 * during the device removal which happens under
64 	 * pci_rescan_remove_lock.
65 	 *
66 	 * This is analogous to sdev_store_delete() in
67 	 * drivers/scsi/scsi_sysfs.c
68 	 */
69 	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
70 	WARN_ON_ONCE(!kn);
71 	/* device_remove_file() serializes concurrent calls ignoring all but
72 	 * the first
73 	 */
74 	device_remove_file(dev, attr);
75 
76 	/* A concurrent call to recover_store() may slip between
77 	 * sysfs_break_active_protection() and the sysfs file removal.
78 	 * Once it unblocks from pci_lock_rescan_remove() the original pdev
79 	 * will already be removed.
80 	 */
81 	pci_lock_rescan_remove();
82 	if (pci_dev_is_added(pdev)) {
83 		pci_stop_and_remove_bus_device(pdev);
84 		ret = zpci_disable_device(zdev);
85 		if (ret)
86 			goto out;
87 
88 		ret = zpci_enable_device(zdev);
89 		if (ret)
90 			goto out;
91 		pci_rescan_bus(zdev->bus);
92 	}
93 out:
94 	pci_unlock_rescan_remove();
95 	if (kn)
96 		sysfs_unbreak_active_protection(kn);
97 	return ret ? ret : count;
98 }
99 static DEVICE_ATTR_WO(recover);
100 
101 static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
102 				struct bin_attribute *attr, char *buf,
103 				loff_t off, size_t count)
104 {
105 	struct device *dev = kobj_to_dev(kobj);
106 	struct pci_dev *pdev = to_pci_dev(dev);
107 	struct zpci_dev *zdev = to_zpci(pdev);
108 
109 	return memory_read_from_buffer(buf, count, &off, zdev->util_str,
110 				       sizeof(zdev->util_str));
111 }
112 static BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
113 
114 static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
115 				  struct bin_attribute *attr, char *buf,
116 				  loff_t off, size_t count)
117 {
118 	struct zpci_report_error_header *report = (void *) buf;
119 	struct device *dev = kobj_to_dev(kobj);
120 	struct pci_dev *pdev = to_pci_dev(dev);
121 	struct zpci_dev *zdev = to_zpci(pdev);
122 	int ret;
123 
124 	if (off || (count < sizeof(*report)))
125 		return -EINVAL;
126 
127 	ret = sclp_pci_report(report, zdev->fh, zdev->fid);
128 
129 	return ret ? ret : count;
130 }
131 static BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
132 
133 static struct bin_attribute *zpci_bin_attrs[] = {
134 	&bin_attr_util_string,
135 	&bin_attr_report_error,
136 	NULL,
137 };
138 
139 static struct attribute *zpci_dev_attrs[] = {
140 	&dev_attr_function_id.attr,
141 	&dev_attr_function_handle.attr,
142 	&dev_attr_pchid.attr,
143 	&dev_attr_pfgid.attr,
144 	&dev_attr_pft.attr,
145 	&dev_attr_vfn.attr,
146 	&dev_attr_uid.attr,
147 	&dev_attr_recover.attr,
148 	&dev_attr_mio_enabled.attr,
149 	NULL,
150 };
151 static struct attribute_group zpci_attr_group = {
152 	.attrs = zpci_dev_attrs,
153 	.bin_attrs = zpci_bin_attrs,
154 };
155 
156 static struct attribute *pfip_attrs[] = {
157 	&dev_attr_segment0.attr,
158 	&dev_attr_segment1.attr,
159 	&dev_attr_segment2.attr,
160 	&dev_attr_segment3.attr,
161 	NULL,
162 };
163 static struct attribute_group pfip_attr_group = {
164 	.name = "pfip",
165 	.attrs = pfip_attrs,
166 };
167 
168 const struct attribute_group *zpci_attr_groups[] = {
169 	&zpci_attr_group,
170 	&pfip_attr_group,
171 	NULL,
172 };
173