xref: /openbmc/linux/arch/s390/pci/pci_sysfs.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21e8da956SJan Glauber /*
31e8da956SJan Glauber  * Copyright IBM Corp. 2012
41e8da956SJan Glauber  *
51e8da956SJan Glauber  * Author(s):
61e8da956SJan Glauber  *   Jan Glauber <jang@linux.vnet.ibm.com>
71e8da956SJan Glauber  */
81e8da956SJan Glauber 
9896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci"
10896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
111e8da956SJan Glauber 
121e8da956SJan Glauber #include <linux/kernel.h>
131e8da956SJan Glauber #include <linux/stat.h>
141e8da956SJan Glauber #include <linux/pci.h>
151e8da956SJan Glauber 
16576c75e3SNiklas Schnelle #include "../../../drivers/pci/pci.h"
17576c75e3SNiklas Schnelle 
18368704a6SSebastian Ott #include <asm/sclp.h>
19368704a6SSebastian Ott 
20b346953dSSebastian Ott #define zpci_attr(name, fmt, member)					\
21b346953dSSebastian Ott static ssize_t name##_show(struct device *dev,				\
22b346953dSSebastian Ott 			   struct device_attribute *attr, char *buf)	\
23b346953dSSebastian Ott {									\
24198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));		\
25b346953dSSebastian Ott 									\
26b346953dSSebastian Ott 	return sprintf(buf, fmt, zdev->member);				\
27b346953dSSebastian Ott }									\
28b346953dSSebastian Ott static DEVICE_ATTR_RO(name)
291e8da956SJan Glauber 
30b346953dSSebastian Ott zpci_attr(function_id, "0x%08x\n", fid);
31b346953dSSebastian Ott zpci_attr(function_handle, "0x%08x\n", fh);
32b346953dSSebastian Ott zpci_attr(pchid, "0x%04x\n", pchid);
33b346953dSSebastian Ott zpci_attr(pfgid, "0x%02x\n", pfgid);
34ac4995b9SSebastian Ott zpci_attr(vfn, "0x%04x\n", vfn);
35ac4995b9SSebastian Ott zpci_attr(pft, "0x%02x\n", pft);
36e6ab7490SAlexander Schmidt zpci_attr(port, "%d\n", port);
37ac4995b9SSebastian Ott zpci_attr(uid, "0x%x\n", uid);
38ac4995b9SSebastian Ott zpci_attr(segment0, "0x%02x\n", pfip[0]);
39ac4995b9SSebastian Ott zpci_attr(segment1, "0x%02x\n", pfip[1]);
40ac4995b9SSebastian Ott zpci_attr(segment2, "0x%02x\n", pfip[2]);
41ac4995b9SSebastian Ott zpci_attr(segment3, "0x%02x\n", pfip[3]);
421e8da956SJan Glauber 
mio_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)438e4708b3SSebastian Ott static ssize_t mio_enabled_show(struct device *dev,
448e4708b3SSebastian Ott 				struct device_attribute *attr, char *buf)
458e4708b3SSebastian Ott {
468e4708b3SSebastian Ott 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
478e4708b3SSebastian Ott 
488e4708b3SSebastian Ott 	return sprintf(buf, zpci_use_mio(zdev) ? "1\n" : "0\n");
498e4708b3SSebastian Ott }
508e4708b3SSebastian Ott static DEVICE_ATTR_RO(mio_enabled);
518e4708b3SSebastian Ott 
recover_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)52b346953dSSebastian Ott static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
530b60f9eaSTejun Heo 			     const char *buf, size_t count)
540ff70ec8SSebastian Ott {
55576c75e3SNiklas Schnelle 	struct kernfs_node *kn;
560ff70ec8SSebastian Ott 	struct pci_dev *pdev = to_pci_dev(dev);
57198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
58576c75e3SNiklas Schnelle 	int ret = 0;
590ff70ec8SSebastian Ott 
60576c75e3SNiklas Schnelle 	/* Can't use device_remove_self() here as that would lead us to lock
61576c75e3SNiklas Schnelle 	 * the pci_rescan_remove_lock while holding the device' kernfs lock.
62576c75e3SNiklas Schnelle 	 * This would create a possible deadlock with disable_slot() which is
63576c75e3SNiklas Schnelle 	 * not directly protected by the device' kernfs lock but takes it
64576c75e3SNiklas Schnelle 	 * during the device removal which happens under
65576c75e3SNiklas Schnelle 	 * pci_rescan_remove_lock.
66576c75e3SNiklas Schnelle 	 *
67576c75e3SNiklas Schnelle 	 * This is analogous to sdev_store_delete() in
68576c75e3SNiklas Schnelle 	 * drivers/scsi/scsi_sysfs.c
69576c75e3SNiklas Schnelle 	 */
70576c75e3SNiklas Schnelle 	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
71576c75e3SNiklas Schnelle 	WARN_ON_ONCE(!kn);
72576c75e3SNiklas Schnelle 	/* device_remove_file() serializes concurrent calls ignoring all but
73576c75e3SNiklas Schnelle 	 * the first
74576c75e3SNiklas Schnelle 	 */
75576c75e3SNiklas Schnelle 	device_remove_file(dev, attr);
760b60f9eaSTejun Heo 
77576c75e3SNiklas Schnelle 	/* A concurrent call to recover_store() may slip between
78576c75e3SNiklas Schnelle 	 * sysfs_break_active_protection() and the sysfs file removal.
79576c75e3SNiklas Schnelle 	 * Once it unblocks from pci_lock_rescan_remove() the original pdev
80576c75e3SNiklas Schnelle 	 * will already be removed.
81576c75e3SNiklas Schnelle 	 */
822a01bd1bSSebastian Ott 	pci_lock_rescan_remove();
83576c75e3SNiklas Schnelle 	if (pci_dev_is_added(pdev)) {
840ff70ec8SSebastian Ott 		pci_stop_and_remove_bus_device(pdev);
851f3f7681SNiklas Schnelle 		if (zdev->dma_table) {
861f3f7681SNiklas Schnelle 			ret = zpci_dma_exit_device(zdev);
871f3f7681SNiklas Schnelle 			if (ret)
881f3f7681SNiklas Schnelle 				goto out;
891f3f7681SNiklas Schnelle 		}
901f3f7681SNiklas Schnelle 
911f3f7681SNiklas Schnelle 		if (zdev_enabled(zdev)) {
920ff70ec8SSebastian Ott 			ret = zpci_disable_device(zdev);
93*1c8174fdSNiklas Schnelle 			/*
94*1c8174fdSNiklas Schnelle 			 * Due to a z/VM vs LPAR inconsistency in the error
95*1c8174fdSNiklas Schnelle 			 * state the FH may indicate an enabled device but
96*1c8174fdSNiklas Schnelle 			 * disable says the device is already disabled don't
97*1c8174fdSNiklas Schnelle 			 * treat it as an error here.
98*1c8174fdSNiklas Schnelle 			 */
99*1c8174fdSNiklas Schnelle 			if (ret == -EINVAL)
100*1c8174fdSNiklas Schnelle 				ret = 0;
1010ff70ec8SSebastian Ott 			if (ret)
102576c75e3SNiklas Schnelle 				goto out;
1031f3f7681SNiklas Schnelle 		}
1040ff70ec8SSebastian Ott 
1050ff70ec8SSebastian Ott 		ret = zpci_enable_device(zdev);
1060ff70ec8SSebastian Ott 		if (ret)
107576c75e3SNiklas Schnelle 			goto out;
1081f3f7681SNiklas Schnelle 		ret = zpci_dma_init_device(zdev);
1091f3f7681SNiklas Schnelle 		if (ret) {
1101f3f7681SNiklas Schnelle 			zpci_disable_device(zdev);
1111f3f7681SNiklas Schnelle 			goto out;
1121f3f7681SNiklas Schnelle 		}
11305bc1be6SPierre Morel 		pci_rescan_bus(zdev->zbus->bus);
114576c75e3SNiklas Schnelle 	}
115576c75e3SNiklas Schnelle out:
1162a01bd1bSSebastian Ott 	pci_unlock_rescan_remove();
117576c75e3SNiklas Schnelle 	if (kn)
118576c75e3SNiklas Schnelle 		sysfs_unbreak_active_protection(kn);
119576c75e3SNiklas Schnelle 	return ret ? ret : count;
1200ff70ec8SSebastian Ott }
121b346953dSSebastian Ott static DEVICE_ATTR_WO(recover);
1220ff70ec8SSebastian Ott 
util_string_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)123ac4995b9SSebastian Ott static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
124ac4995b9SSebastian Ott 				struct bin_attribute *attr, char *buf,
125ac4995b9SSebastian Ott 				loff_t off, size_t count)
126ac4995b9SSebastian Ott {
127ac4995b9SSebastian Ott 	struct device *dev = kobj_to_dev(kobj);
128ac4995b9SSebastian Ott 	struct pci_dev *pdev = to_pci_dev(dev);
129198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
130ac4995b9SSebastian Ott 
131ac4995b9SSebastian Ott 	return memory_read_from_buffer(buf, count, &off, zdev->util_str,
132ac4995b9SSebastian Ott 				       sizeof(zdev->util_str));
133ac4995b9SSebastian Ott }
134ac4995b9SSebastian Ott static BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
135368704a6SSebastian Ott 
report_error_write(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)136368704a6SSebastian Ott static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
137368704a6SSebastian Ott 				  struct bin_attribute *attr, char *buf,
138368704a6SSebastian Ott 				  loff_t off, size_t count)
139368704a6SSebastian Ott {
140368704a6SSebastian Ott 	struct zpci_report_error_header *report = (void *) buf;
141368704a6SSebastian Ott 	struct device *dev = kobj_to_dev(kobj);
142368704a6SSebastian Ott 	struct pci_dev *pdev = to_pci_dev(dev);
143368704a6SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
144368704a6SSebastian Ott 	int ret;
145368704a6SSebastian Ott 
146368704a6SSebastian Ott 	if (off || (count < sizeof(*report)))
147368704a6SSebastian Ott 		return -EINVAL;
148368704a6SSebastian Ott 
149368704a6SSebastian Ott 	ret = sclp_pci_report(report, zdev->fh, zdev->fid);
150368704a6SSebastian Ott 
151368704a6SSebastian Ott 	return ret ? ret : count;
152368704a6SSebastian Ott }
153368704a6SSebastian Ott static BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
154368704a6SSebastian Ott 
uid_is_unique_show(struct device * dev,struct device_attribute * attr,char * buf)155408f2c9cSNiklas Schnelle static ssize_t uid_is_unique_show(struct device *dev,
156408f2c9cSNiklas Schnelle 				  struct device_attribute *attr, char *buf)
157408f2c9cSNiklas Schnelle {
158408f2c9cSNiklas Schnelle 	return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
159408f2c9cSNiklas Schnelle }
160408f2c9cSNiklas Schnelle static DEVICE_ATTR_RO(uid_is_unique);
161408f2c9cSNiklas Schnelle 
16281bbf039SNiklas Schnelle #ifndef CONFIG_DMI
16381bbf039SNiklas Schnelle /* analogous to smbios index */
index_show(struct device * dev,struct device_attribute * attr,char * buf)16481bbf039SNiklas Schnelle static ssize_t index_show(struct device *dev,
16581bbf039SNiklas Schnelle 			  struct device_attribute *attr, char *buf)
16681bbf039SNiklas Schnelle {
16781bbf039SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
16881bbf039SNiklas Schnelle 	u32 index = ~0;
16981bbf039SNiklas Schnelle 
17081bbf039SNiklas Schnelle 	if (zpci_unique_uid)
17181bbf039SNiklas Schnelle 		index = zdev->uid;
17281bbf039SNiklas Schnelle 
17381bbf039SNiklas Schnelle 	return sysfs_emit(buf, "%u\n", index);
17481bbf039SNiklas Schnelle }
17581bbf039SNiklas Schnelle static DEVICE_ATTR_RO(index);
17681bbf039SNiklas Schnelle 
zpci_index_is_visible(struct kobject * kobj,struct attribute * attr,int n)17781bbf039SNiklas Schnelle static umode_t zpci_index_is_visible(struct kobject *kobj,
17881bbf039SNiklas Schnelle 				     struct attribute *attr, int n)
17981bbf039SNiklas Schnelle {
18081bbf039SNiklas Schnelle 	return zpci_unique_uid ? attr->mode : 0;
18181bbf039SNiklas Schnelle }
18281bbf039SNiklas Schnelle 
18381bbf039SNiklas Schnelle static struct attribute *zpci_ident_attrs[] = {
18481bbf039SNiklas Schnelle 	&dev_attr_index.attr,
18581bbf039SNiklas Schnelle 	NULL,
18681bbf039SNiklas Schnelle };
18781bbf039SNiklas Schnelle 
18881bbf039SNiklas Schnelle static struct attribute_group zpci_ident_attr_group = {
18981bbf039SNiklas Schnelle 	.attrs = zpci_ident_attrs,
19081bbf039SNiklas Schnelle 	.is_visible = zpci_index_is_visible,
19181bbf039SNiklas Schnelle };
19281bbf039SNiklas Schnelle #endif
19381bbf039SNiklas Schnelle 
194ac4995b9SSebastian Ott static struct bin_attribute *zpci_bin_attrs[] = {
195ac4995b9SSebastian Ott 	&bin_attr_util_string,
196368704a6SSebastian Ott 	&bin_attr_report_error,
197ac4995b9SSebastian Ott 	NULL,
198ac4995b9SSebastian Ott };
199ac4995b9SSebastian Ott 
20093065d04SSebastian Ott static struct attribute *zpci_dev_attrs[] = {
20193065d04SSebastian Ott 	&dev_attr_function_id.attr,
20293065d04SSebastian Ott 	&dev_attr_function_handle.attr,
20393065d04SSebastian Ott 	&dev_attr_pchid.attr,
20493065d04SSebastian Ott 	&dev_attr_pfgid.attr,
205ac4995b9SSebastian Ott 	&dev_attr_pft.attr,
206e6ab7490SAlexander Schmidt 	&dev_attr_port.attr,
207ac4995b9SSebastian Ott 	&dev_attr_vfn.attr,
208ac4995b9SSebastian Ott 	&dev_attr_uid.attr,
20993065d04SSebastian Ott 	&dev_attr_recover.attr,
2108e4708b3SSebastian Ott 	&dev_attr_mio_enabled.attr,
211408f2c9cSNiklas Schnelle 	&dev_attr_uid_is_unique.attr,
2121e8da956SJan Glauber 	NULL,
2131e8da956SJan Glauber };
214408f2c9cSNiklas Schnelle 
21593065d04SSebastian Ott static struct attribute_group zpci_attr_group = {
21693065d04SSebastian Ott 	.attrs = zpci_dev_attrs,
217ac4995b9SSebastian Ott 	.bin_attrs = zpci_bin_attrs,
21893065d04SSebastian Ott };
219ac4995b9SSebastian Ott 
220ac4995b9SSebastian Ott static struct attribute *pfip_attrs[] = {
221ac4995b9SSebastian Ott 	&dev_attr_segment0.attr,
222ac4995b9SSebastian Ott 	&dev_attr_segment1.attr,
223ac4995b9SSebastian Ott 	&dev_attr_segment2.attr,
224ac4995b9SSebastian Ott 	&dev_attr_segment3.attr,
225ac4995b9SSebastian Ott 	NULL,
226ac4995b9SSebastian Ott };
227ac4995b9SSebastian Ott static struct attribute_group pfip_attr_group = {
228ac4995b9SSebastian Ott 	.name = "pfip",
229ac4995b9SSebastian Ott 	.attrs = pfip_attrs,
230ac4995b9SSebastian Ott };
231ac4995b9SSebastian Ott 
23293065d04SSebastian Ott const struct attribute_group *zpci_attr_groups[] = {
23393065d04SSebastian Ott 	&zpci_attr_group,
234ac4995b9SSebastian Ott 	&pfip_attr_group,
23581bbf039SNiklas Schnelle #ifndef CONFIG_DMI
23681bbf039SNiklas Schnelle 	&zpci_ident_attr_group,
23781bbf039SNiklas Schnelle #endif
23893065d04SSebastian Ott 	NULL,
23993065d04SSebastian Ott };
240