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