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