1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2012
6 * Jan Glauber <jang@linux.vnet.ibm.com>
9 #define KMSG_COMPONENT "zpci"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/stat.h>
14 #include <linux/pci.h>
16 #include "../../../drivers/pci/pci.h"
20 #define zpci_attr(name, fmt, member) \
21 static ssize_t name##_show(struct device *dev, \
22 struct device_attribute *attr, char *buf) \
24 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); \
26 return sprintf(buf, fmt, zdev->member); \
28 static DEVICE_ATTR_RO(name)
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(uid, "0x%x\n", uid);
37 zpci_attr(segment0, "0x%02x\n", pfip[0]);
38 zpci_attr(segment1, "0x%02x\n", pfip[1]);
39 zpci_attr(segment2, "0x%02x\n", pfip[2]);
40 zpci_attr(segment3, "0x%02x\n", pfip[3]);
42 static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
43 const char *buf, size_t count)
45 struct kernfs_node *kn;
46 struct pci_dev *pdev = to_pci_dev(dev);
47 struct zpci_dev *zdev = to_zpci(pdev);
50 /* Can't use device_remove_self() here as that would lead us to lock
51 * the pci_rescan_remove_lock while holding the device' kernfs lock.
52 * This would create a possible deadlock with disable_slot() which is
53 * not directly protected by the device' kernfs lock but takes it
54 * during the device removal which happens under
55 * pci_rescan_remove_lock.
57 * This is analogous to sdev_store_delete() in
58 * drivers/scsi/scsi_sysfs.c
60 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
62 /* device_remove_file() serializes concurrent calls ignoring all but
65 device_remove_file(dev, attr);
67 /* A concurrent call to recover_store() may slip between
68 * sysfs_break_active_protection() and the sysfs file removal.
69 * Once it unblocks from pci_lock_rescan_remove() the original pdev
70 * will already be removed.
72 pci_lock_rescan_remove();
73 if (pci_dev_is_added(pdev)) {
74 pci_stop_and_remove_bus_device(pdev);
75 ret = zpci_disable_device(zdev);
79 ret = zpci_enable_device(zdev);
82 pci_rescan_bus(zdev->bus);
85 pci_unlock_rescan_remove();
87 sysfs_unbreak_active_protection(kn);
88 return ret ? ret : count;
90 static DEVICE_ATTR_WO(recover);
92 static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
93 struct bin_attribute *attr, char *buf,
94 loff_t off, size_t count)
96 struct device *dev = kobj_to_dev(kobj);
97 struct pci_dev *pdev = to_pci_dev(dev);
98 struct zpci_dev *zdev = to_zpci(pdev);
100 return memory_read_from_buffer(buf, count, &off, zdev->util_str,
101 sizeof(zdev->util_str));
103 static BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
105 static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
106 struct bin_attribute *attr, char *buf,
107 loff_t off, size_t count)
109 struct zpci_report_error_header *report = (void *) buf;
110 struct device *dev = kobj_to_dev(kobj);
111 struct pci_dev *pdev = to_pci_dev(dev);
112 struct zpci_dev *zdev = to_zpci(pdev);
115 if (off || (count < sizeof(*report)))
118 ret = sclp_pci_report(report, zdev->fh, zdev->fid);
120 return ret ? ret : count;
122 static BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
124 static struct bin_attribute *zpci_bin_attrs[] = {
125 &bin_attr_util_string,
126 &bin_attr_report_error,
130 static struct attribute *zpci_dev_attrs[] = {
131 &dev_attr_function_id.attr,
132 &dev_attr_function_handle.attr,
133 &dev_attr_pchid.attr,
134 &dev_attr_pfgid.attr,
138 &dev_attr_recover.attr,
141 static struct attribute_group zpci_attr_group = {
142 .attrs = zpci_dev_attrs,
143 .bin_attrs = zpci_bin_attrs,
146 static struct attribute *pfip_attrs[] = {
147 &dev_attr_segment0.attr,
148 &dev_attr_segment1.attr,
149 &dev_attr_segment2.attr,
150 &dev_attr_segment3.attr,
153 static struct attribute_group pfip_attr_group = {
158 const struct attribute_group *zpci_attr_groups[] = {