xref: /linux/arch/s390/pci/pci_sysfs.c (revision fbf5df34a4dbcd09d433dd4f0916bf9b2ddb16de)
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 pr_fmt(fmt) "zpci: " fmt
10 
11 #include <linux/kernel.h>
12 #include <linux/stat.h>
13 #include <linux/pci.h>
14 
15 #include "../../../drivers/pci/pci.h"
16 
17 #include <asm/sclp.h>
18 
19 #define zpci_attr(name, fmt, member)					\
20 static ssize_t name##_show(struct device *dev,				\
21 			   struct device_attribute *attr, char *buf)	\
22 {									\
23 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));		\
24 									\
25 	return sysfs_emit(buf, fmt, zdev->member);				\
26 }									\
27 static DEVICE_ATTR_RO(name)
28 
29 zpci_attr(function_id, "0x%08x\n", fid);
30 zpci_attr(function_handle, "0x%08x\n", fh);
31 zpci_attr(pchid, "0x%04x\n", pchid);
32 zpci_attr(pfgid, "0x%02x\n", pfgid);
33 zpci_attr(vfn, "0x%04x\n", vfn);
34 zpci_attr(pft, "0x%02x\n", pft);
35 zpci_attr(port, "%d\n", port);
36 zpci_attr(fidparm, "0x%02x\n", fidparm);
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 #define ZPCI_FW_ATTR_RO(_name)						\
44 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
45 
46 static ssize_t mio_enabled_show(struct device *dev,
47 				struct device_attribute *attr, char *buf)
48 {
49 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
50 
51 	return sysfs_emit(buf, zpci_use_mio(zdev) ? "1\n" : "0\n");
52 }
53 static DEVICE_ATTR_RO(mio_enabled);
54 
55 static int _do_recover(struct pci_dev *pdev, struct zpci_dev *zdev)
56 {
57 	int ret;
58 
59 	pci_stop_and_remove_bus_device(pdev);
60 	if (zdev_enabled(zdev)) {
61 		ret = zpci_disable_device(zdev);
62 		/*
63 		 * Due to a z/VM vs LPAR inconsistency in the error
64 		 * state the FH may indicate an enabled device but
65 		 * disable says the device is already disabled don't
66 		 * treat it as an error here.
67 		 */
68 		if (ret == -EINVAL)
69 			ret = 0;
70 		if (ret)
71 			return ret;
72 	}
73 
74 	ret = zpci_reenable_device(zdev);
75 
76 	return ret;
77 }
78 
79 static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
80 			     const char *buf, size_t count)
81 {
82 	struct kernfs_node *kn;
83 	struct pci_dev *pdev = to_pci_dev(dev);
84 	struct zpci_dev *zdev = to_zpci(pdev);
85 	int ret = 0;
86 
87 	/* Can't use device_remove_self() here as that would lead us to lock
88 	 * the pci_rescan_remove_lock while holding the device' kernfs lock.
89 	 * This would create a possible deadlock with disable_slot() which is
90 	 * not directly protected by the device' kernfs lock but takes it
91 	 * during the device removal which happens under
92 	 * pci_rescan_remove_lock.
93 	 *
94 	 * This is analogous to sdev_store_delete() in
95 	 * drivers/scsi/scsi_sysfs.c
96 	 */
97 	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
98 	WARN_ON_ONCE(!kn);
99 
100 	/* Device needs to be configured and state must not change */
101 	mutex_lock(&zdev->state_lock);
102 	if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
103 		goto out;
104 
105 	/* device_remove_file() serializes concurrent calls ignoring all but
106 	 * the first
107 	 */
108 	device_remove_file(dev, attr);
109 
110 	/* A concurrent call to recover_store() may slip between
111 	 * sysfs_break_active_protection() and the sysfs file removal.
112 	 * Once it unblocks from pci_lock_rescan_remove() the original pdev
113 	 * will already be removed.
114 	 */
115 	pci_lock_rescan_remove();
116 	if (pci_dev_is_added(pdev)) {
117 		ret = _do_recover(pdev, zdev);
118 	}
119 	pci_rescan_bus(zdev->zbus->bus);
120 	pci_unlock_rescan_remove();
121 
122 out:
123 	mutex_unlock(&zdev->state_lock);
124 	if (kn)
125 		sysfs_unbreak_active_protection(kn);
126 	return ret ? ret : count;
127 }
128 static DEVICE_ATTR_WO(recover);
129 
130 static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
131 				const struct bin_attribute *attr, char *buf,
132 				loff_t off, size_t count)
133 {
134 	struct device *dev = kobj_to_dev(kobj);
135 	struct pci_dev *pdev = to_pci_dev(dev);
136 	struct zpci_dev *zdev = to_zpci(pdev);
137 
138 	return memory_read_from_buffer(buf, count, &off, zdev->util_str,
139 				       sizeof(zdev->util_str));
140 }
141 static const BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
142 
143 static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
144 				  const struct bin_attribute *attr, char *buf,
145 				  loff_t off, size_t count)
146 {
147 	struct zpci_report_error_header *report = (void *) buf;
148 	struct device *dev = kobj_to_dev(kobj);
149 	struct pci_dev *pdev = to_pci_dev(dev);
150 	struct zpci_dev *zdev = to_zpci(pdev);
151 	int ret;
152 
153 	if (off || (count < sizeof(*report)))
154 		return -EINVAL;
155 
156 	ret = sclp_pci_report(report, zdev->fh, zdev->fid);
157 
158 	return ret ? ret : count;
159 }
160 static const BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
161 
162 static ssize_t uid_is_unique_show(struct device *dev,
163 				  struct device_attribute *attr, char *buf)
164 {
165 	return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
166 }
167 static DEVICE_ATTR_RO(uid_is_unique);
168 
169 static ssize_t uid_checking_show(struct kobject *kobj,
170 				 struct kobj_attribute *attr, char *buf)
171 {
172 	return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
173 }
174 ZPCI_FW_ATTR_RO(uid_checking);
175 
176 /* analogous to smbios index */
177 static ssize_t index_show(struct device *dev,
178 			  struct device_attribute *attr, char *buf)
179 {
180 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
181 	u32 index = ~0;
182 
183 	if (zpci_unique_uid)
184 		index = zdev->uid;
185 
186 	return sysfs_emit(buf, "%u\n", index);
187 }
188 static DEVICE_ATTR_RO(index);
189 
190 static ssize_t zpci_uid_slot_show(struct pci_slot *slot, char *buf)
191 {
192 	struct zpci_dev *zdev = container_of(slot->hotplug, struct zpci_dev,
193 					     hotplug_slot);
194 
195 	return sysfs_emit(buf, "0x%x\n", zdev->uid);
196 }
197 
198 static struct pci_slot_attribute zpci_slot_attr_uid =
199 	__ATTR(uid, 0444, zpci_uid_slot_show, NULL);
200 
201 static umode_t zpci_index_is_visible(struct kobject *kobj,
202 				     struct attribute *attr, int n)
203 {
204 	return zpci_unique_uid ? attr->mode : 0;
205 }
206 
207 static struct attribute *zpci_ident_attrs[] = {
208 	&dev_attr_index.attr,
209 	NULL,
210 };
211 
212 const struct attribute_group zpci_ident_attr_group = {
213 	.attrs = zpci_ident_attrs,
214 	.is_visible = zpci_index_is_visible,
215 };
216 
217 static const struct bin_attribute *const zpci_bin_attrs[] = {
218 	&bin_attr_util_string,
219 	&bin_attr_report_error,
220 	NULL,
221 };
222 
223 static struct attribute *zpci_dev_attrs[] = {
224 	&dev_attr_function_id.attr,
225 	&dev_attr_function_handle.attr,
226 	&dev_attr_pchid.attr,
227 	&dev_attr_pfgid.attr,
228 	&dev_attr_pft.attr,
229 	&dev_attr_port.attr,
230 	&dev_attr_fidparm.attr,
231 	&dev_attr_vfn.attr,
232 	&dev_attr_uid.attr,
233 	&dev_attr_recover.attr,
234 	&dev_attr_mio_enabled.attr,
235 	&dev_attr_uid_is_unique.attr,
236 	NULL,
237 };
238 
239 const struct attribute_group zpci_attr_group = {
240 	.attrs = zpci_dev_attrs,
241 	.bin_attrs = zpci_bin_attrs,
242 };
243 
244 static struct attribute *pfip_attrs[] = {
245 	&dev_attr_segment0.attr,
246 	&dev_attr_segment1.attr,
247 	&dev_attr_segment2.attr,
248 	&dev_attr_segment3.attr,
249 	NULL,
250 };
251 
252 const struct attribute_group pfip_attr_group = {
253 	.name = "pfip",
254 	.attrs = pfip_attrs,
255 };
256 
257 static struct attribute *zpci_slot_attrs[] = {
258 	&zpci_slot_attr_uid.attr,
259 	NULL,
260 };
261 
262 const struct attribute_group zpci_slot_attr_group = {
263 	.attrs = zpci_slot_attrs,
264 };
265 
266 static struct attribute *clp_fw_attrs[] = {
267 	&uid_checking_attr.attr,
268 	NULL,
269 };
270 
271 static struct attribute_group clp_fw_attr_group = {
272 	.name = "clp",
273 	.attrs = clp_fw_attrs,
274 };
275 
276 int __init __zpci_fw_sysfs_init(void)
277 {
278 	return sysfs_create_group(firmware_kobj, &clp_fw_attr_group);
279 }
280