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