xref: /linux/arch/s390/pci/pci_sysfs.c (revision aaa44952bbd1d4db14a4d676bf9595bb5db7e7b0)
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(port, "%d\n", port);
37 zpci_attr(uid, "0x%x\n", uid);
38 zpci_attr(segment0, "0x%02x\n", pfip[0]);
39 zpci_attr(segment1, "0x%02x\n", pfip[1]);
40 zpci_attr(segment2, "0x%02x\n", pfip[2]);
41 zpci_attr(segment3, "0x%02x\n", pfip[3]);
42 
43 static ssize_t mio_enabled_show(struct device *dev,
44 				struct device_attribute *attr, char *buf)
45 {
46 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
47 
48 	return sprintf(buf, zpci_use_mio(zdev) ? "1\n" : "0\n");
49 }
50 static DEVICE_ATTR_RO(mio_enabled);
51 
52 static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
53 			     const char *buf, size_t count)
54 {
55 	struct kernfs_node *kn;
56 	struct pci_dev *pdev = to_pci_dev(dev);
57 	struct zpci_dev *zdev = to_zpci(pdev);
58 	int ret = 0;
59 
60 	/* Can't use device_remove_self() here as that would lead us to lock
61 	 * the pci_rescan_remove_lock while holding the device' kernfs lock.
62 	 * This would create a possible deadlock with disable_slot() which is
63 	 * not directly protected by the device' kernfs lock but takes it
64 	 * during the device removal which happens under
65 	 * pci_rescan_remove_lock.
66 	 *
67 	 * This is analogous to sdev_store_delete() in
68 	 * drivers/scsi/scsi_sysfs.c
69 	 */
70 	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
71 	WARN_ON_ONCE(!kn);
72 	/* device_remove_file() serializes concurrent calls ignoring all but
73 	 * the first
74 	 */
75 	device_remove_file(dev, attr);
76 
77 	/* A concurrent call to recover_store() may slip between
78 	 * sysfs_break_active_protection() and the sysfs file removal.
79 	 * Once it unblocks from pci_lock_rescan_remove() the original pdev
80 	 * will already be removed.
81 	 */
82 	pci_lock_rescan_remove();
83 	if (pci_dev_is_added(pdev)) {
84 		pci_stop_and_remove_bus_device(pdev);
85 		ret = zpci_disable_device(zdev);
86 		if (ret)
87 			goto out;
88 
89 		ret = zpci_enable_device(zdev);
90 		if (ret)
91 			goto out;
92 		pci_rescan_bus(zdev->zbus->bus);
93 	}
94 out:
95 	pci_unlock_rescan_remove();
96 	if (kn)
97 		sysfs_unbreak_active_protection(kn);
98 	return ret ? ret : count;
99 }
100 static DEVICE_ATTR_WO(recover);
101 
102 static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
103 				struct bin_attribute *attr, char *buf,
104 				loff_t off, size_t count)
105 {
106 	struct device *dev = kobj_to_dev(kobj);
107 	struct pci_dev *pdev = to_pci_dev(dev);
108 	struct zpci_dev *zdev = to_zpci(pdev);
109 
110 	return memory_read_from_buffer(buf, count, &off, zdev->util_str,
111 				       sizeof(zdev->util_str));
112 }
113 static BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
114 
115 static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
116 				  struct bin_attribute *attr, char *buf,
117 				  loff_t off, size_t count)
118 {
119 	struct zpci_report_error_header *report = (void *) buf;
120 	struct device *dev = kobj_to_dev(kobj);
121 	struct pci_dev *pdev = to_pci_dev(dev);
122 	struct zpci_dev *zdev = to_zpci(pdev);
123 	int ret;
124 
125 	if (off || (count < sizeof(*report)))
126 		return -EINVAL;
127 
128 	ret = sclp_pci_report(report, zdev->fh, zdev->fid);
129 
130 	return ret ? ret : count;
131 }
132 static BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
133 
134 static ssize_t uid_is_unique_show(struct device *dev,
135 				  struct device_attribute *attr, char *buf)
136 {
137 	return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
138 }
139 static DEVICE_ATTR_RO(uid_is_unique);
140 
141 #ifndef CONFIG_DMI
142 /* analogous to smbios index */
143 static ssize_t index_show(struct device *dev,
144 			  struct device_attribute *attr, char *buf)
145 {
146 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
147 	u32 index = ~0;
148 
149 	if (zpci_unique_uid)
150 		index = zdev->uid;
151 
152 	return sysfs_emit(buf, "%u\n", index);
153 }
154 static DEVICE_ATTR_RO(index);
155 
156 static umode_t zpci_index_is_visible(struct kobject *kobj,
157 				     struct attribute *attr, int n)
158 {
159 	return zpci_unique_uid ? attr->mode : 0;
160 }
161 
162 static struct attribute *zpci_ident_attrs[] = {
163 	&dev_attr_index.attr,
164 	NULL,
165 };
166 
167 static struct attribute_group zpci_ident_attr_group = {
168 	.attrs = zpci_ident_attrs,
169 	.is_visible = zpci_index_is_visible,
170 };
171 #endif
172 
173 static struct bin_attribute *zpci_bin_attrs[] = {
174 	&bin_attr_util_string,
175 	&bin_attr_report_error,
176 	NULL,
177 };
178 
179 static struct attribute *zpci_dev_attrs[] = {
180 	&dev_attr_function_id.attr,
181 	&dev_attr_function_handle.attr,
182 	&dev_attr_pchid.attr,
183 	&dev_attr_pfgid.attr,
184 	&dev_attr_pft.attr,
185 	&dev_attr_port.attr,
186 	&dev_attr_vfn.attr,
187 	&dev_attr_uid.attr,
188 	&dev_attr_recover.attr,
189 	&dev_attr_mio_enabled.attr,
190 	&dev_attr_uid_is_unique.attr,
191 	NULL,
192 };
193 
194 static struct attribute_group zpci_attr_group = {
195 	.attrs = zpci_dev_attrs,
196 	.bin_attrs = zpci_bin_attrs,
197 };
198 
199 static struct attribute *pfip_attrs[] = {
200 	&dev_attr_segment0.attr,
201 	&dev_attr_segment1.attr,
202 	&dev_attr_segment2.attr,
203 	&dev_attr_segment3.attr,
204 	NULL,
205 };
206 static struct attribute_group pfip_attr_group = {
207 	.name = "pfip",
208 	.attrs = pfip_attrs,
209 };
210 
211 const struct attribute_group *zpci_attr_groups[] = {
212 	&zpci_attr_group,
213 	&pfip_attr_group,
214 #ifndef CONFIG_DMI
215 	&zpci_ident_attr_group,
216 #endif
217 	NULL,
218 };
219