xref: /linux/arch/s390/pci/pci_sysfs.c (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
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 
mio_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)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 
_do_recover(struct pci_dev * pdev,struct zpci_dev * zdev)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 
recover_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 
util_string_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t off,size_t count)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 
report_error_write(struct file * filp,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t off,size_t count)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 	if (count < (report->length + sizeof(*report)))
157 		return -EINVAL;
158 
159 	ret = sclp_pci_report(report, zdev->fh, zdev->fid);
160 
161 	return ret ? ret : count;
162 }
163 static const BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
164 
uid_is_unique_show(struct device * dev,struct device_attribute * attr,char * buf)165 static ssize_t uid_is_unique_show(struct device *dev,
166 				  struct device_attribute *attr, char *buf)
167 {
168 	return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
169 }
170 static DEVICE_ATTR_RO(uid_is_unique);
171 
uid_checking_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)172 static ssize_t uid_checking_show(struct kobject *kobj,
173 				 struct kobj_attribute *attr, char *buf)
174 {
175 	return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
176 }
177 ZPCI_FW_ATTR_RO(uid_checking);
178 
179 /* analogous to smbios index */
index_show(struct device * dev,struct device_attribute * attr,char * buf)180 static ssize_t index_show(struct device *dev,
181 			  struct device_attribute *attr, char *buf)
182 {
183 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
184 	u32 index = ~0;
185 
186 	if (zpci_unique_uid)
187 		index = zdev->uid;
188 
189 	return sysfs_emit(buf, "%u\n", index);
190 }
191 static DEVICE_ATTR_RO(index);
192 
zpci_uid_slot_show(struct pci_slot * slot,char * buf)193 static ssize_t zpci_uid_slot_show(struct pci_slot *slot, char *buf)
194 {
195 	struct zpci_dev *zdev = container_of(slot->hotplug, struct zpci_dev,
196 					     hotplug_slot);
197 
198 	return sysfs_emit(buf, "0x%x\n", zdev->uid);
199 }
200 
201 static struct pci_slot_attribute zpci_slot_attr_uid =
202 	__ATTR(uid, 0444, zpci_uid_slot_show, NULL);
203 
zpci_index_is_visible(struct kobject * kobj,struct attribute * attr,int n)204 static umode_t zpci_index_is_visible(struct kobject *kobj,
205 				     struct attribute *attr, int n)
206 {
207 	return zpci_unique_uid ? attr->mode : 0;
208 }
209 
210 static struct attribute *zpci_ident_attrs[] = {
211 	&dev_attr_index.attr,
212 	NULL,
213 };
214 
215 const struct attribute_group zpci_ident_attr_group = {
216 	.attrs = zpci_ident_attrs,
217 	.is_visible = zpci_index_is_visible,
218 };
219 
220 static const struct bin_attribute *const zpci_bin_attrs[] = {
221 	&bin_attr_util_string,
222 	&bin_attr_report_error,
223 	NULL,
224 };
225 
226 static struct attribute *zpci_dev_attrs[] = {
227 	&dev_attr_function_id.attr,
228 	&dev_attr_function_handle.attr,
229 	&dev_attr_pchid.attr,
230 	&dev_attr_pfgid.attr,
231 	&dev_attr_pft.attr,
232 	&dev_attr_port.attr,
233 	&dev_attr_fidparm.attr,
234 	&dev_attr_vfn.attr,
235 	&dev_attr_uid.attr,
236 	&dev_attr_recover.attr,
237 	&dev_attr_mio_enabled.attr,
238 	&dev_attr_uid_is_unique.attr,
239 	NULL,
240 };
241 
242 const struct attribute_group zpci_attr_group = {
243 	.attrs = zpci_dev_attrs,
244 	.bin_attrs = zpci_bin_attrs,
245 };
246 
247 static struct attribute *pfip_attrs[] = {
248 	&dev_attr_segment0.attr,
249 	&dev_attr_segment1.attr,
250 	&dev_attr_segment2.attr,
251 	&dev_attr_segment3.attr,
252 	NULL,
253 };
254 
255 const struct attribute_group pfip_attr_group = {
256 	.name = "pfip",
257 	.attrs = pfip_attrs,
258 };
259 
260 static struct attribute *zpci_slot_attrs[] = {
261 	&zpci_slot_attr_uid.attr,
262 	NULL,
263 };
264 
265 const struct attribute_group zpci_slot_attr_group = {
266 	.attrs = zpci_slot_attrs,
267 };
268 
269 static struct attribute *clp_fw_attrs[] = {
270 	&uid_checking_attr.attr,
271 	NULL,
272 };
273 
274 static struct attribute_group clp_fw_attr_group = {
275 	.name = "clp",
276 	.attrs = clp_fw_attrs,
277 };
278 
__zpci_fw_sysfs_init(void)279 int __init __zpci_fw_sysfs_init(void)
280 {
281 	return sysfs_create_group(firmware_kobj, &clp_fw_attr_group);
282 }
283