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
_do_recover(struct pci_dev * pdev,struct zpci_dev * zdev)52*bcb5d6c7SGerd Bayer static int _do_recover(struct pci_dev *pdev, struct zpci_dev *zdev)
53*bcb5d6c7SGerd Bayer {
54*bcb5d6c7SGerd Bayer u8 status;
55*bcb5d6c7SGerd Bayer int ret;
56*bcb5d6c7SGerd Bayer
57*bcb5d6c7SGerd Bayer pci_stop_and_remove_bus_device(pdev);
58*bcb5d6c7SGerd Bayer if (zdev_enabled(zdev)) {
59*bcb5d6c7SGerd Bayer ret = zpci_disable_device(zdev);
60*bcb5d6c7SGerd Bayer /*
61*bcb5d6c7SGerd Bayer * Due to a z/VM vs LPAR inconsistency in the error
62*bcb5d6c7SGerd Bayer * state the FH may indicate an enabled device but
63*bcb5d6c7SGerd Bayer * disable says the device is already disabled don't
64*bcb5d6c7SGerd Bayer * treat it as an error here.
65*bcb5d6c7SGerd Bayer */
66*bcb5d6c7SGerd Bayer if (ret == -EINVAL)
67*bcb5d6c7SGerd Bayer ret = 0;
68*bcb5d6c7SGerd Bayer if (ret)
69*bcb5d6c7SGerd Bayer return ret;
70*bcb5d6c7SGerd Bayer }
71*bcb5d6c7SGerd Bayer
72*bcb5d6c7SGerd Bayer ret = zpci_enable_device(zdev);
73*bcb5d6c7SGerd Bayer if (ret)
74*bcb5d6c7SGerd Bayer return ret;
75*bcb5d6c7SGerd Bayer
76*bcb5d6c7SGerd Bayer if (zdev->dma_table) {
77*bcb5d6c7SGerd Bayer ret = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
78*bcb5d6c7SGerd Bayer virt_to_phys(zdev->dma_table), &status);
79*bcb5d6c7SGerd Bayer if (ret)
80*bcb5d6c7SGerd Bayer zpci_disable_device(zdev);
81*bcb5d6c7SGerd Bayer }
82*bcb5d6c7SGerd Bayer return ret;
83*bcb5d6c7SGerd Bayer }
84*bcb5d6c7SGerd Bayer
recover_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)85b346953dSSebastian Ott static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
860b60f9eaSTejun Heo const char *buf, size_t count)
870ff70ec8SSebastian Ott {
88576c75e3SNiklas Schnelle struct kernfs_node *kn;
890ff70ec8SSebastian Ott struct pci_dev *pdev = to_pci_dev(dev);
90198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev);
91576c75e3SNiklas Schnelle int ret = 0;
920ff70ec8SSebastian Ott
93576c75e3SNiklas Schnelle /* Can't use device_remove_self() here as that would lead us to lock
94576c75e3SNiklas Schnelle * the pci_rescan_remove_lock while holding the device' kernfs lock.
95576c75e3SNiklas Schnelle * This would create a possible deadlock with disable_slot() which is
96576c75e3SNiklas Schnelle * not directly protected by the device' kernfs lock but takes it
97576c75e3SNiklas Schnelle * during the device removal which happens under
98576c75e3SNiklas Schnelle * pci_rescan_remove_lock.
99576c75e3SNiklas Schnelle *
100576c75e3SNiklas Schnelle * This is analogous to sdev_store_delete() in
101576c75e3SNiklas Schnelle * drivers/scsi/scsi_sysfs.c
102576c75e3SNiklas Schnelle */
103576c75e3SNiklas Schnelle kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
104576c75e3SNiklas Schnelle WARN_ON_ONCE(!kn);
105*bcb5d6c7SGerd Bayer
106*bcb5d6c7SGerd Bayer /* Device needs to be configured and state must not change */
107*bcb5d6c7SGerd Bayer mutex_lock(&zdev->state_lock);
108*bcb5d6c7SGerd Bayer if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
109*bcb5d6c7SGerd Bayer goto out;
110*bcb5d6c7SGerd Bayer
111576c75e3SNiklas Schnelle /* device_remove_file() serializes concurrent calls ignoring all but
112576c75e3SNiklas Schnelle * the first
113576c75e3SNiklas Schnelle */
114576c75e3SNiklas Schnelle device_remove_file(dev, attr);
1150b60f9eaSTejun Heo
116576c75e3SNiklas Schnelle /* A concurrent call to recover_store() may slip between
117576c75e3SNiklas Schnelle * sysfs_break_active_protection() and the sysfs file removal.
118576c75e3SNiklas Schnelle * Once it unblocks from pci_lock_rescan_remove() the original pdev
119576c75e3SNiklas Schnelle * will already be removed.
120576c75e3SNiklas Schnelle */
1212a01bd1bSSebastian Ott pci_lock_rescan_remove();
122576c75e3SNiklas Schnelle if (pci_dev_is_added(pdev)) {
123*bcb5d6c7SGerd Bayer ret = _do_recover(pdev, zdev);
1241f3f7681SNiklas Schnelle }
125c76c067eSNiklas Schnelle pci_rescan_bus(zdev->zbus->bus);
1262a01bd1bSSebastian Ott pci_unlock_rescan_remove();
127*bcb5d6c7SGerd Bayer
128*bcb5d6c7SGerd Bayer out:
129*bcb5d6c7SGerd Bayer mutex_unlock(&zdev->state_lock);
130576c75e3SNiklas Schnelle if (kn)
131576c75e3SNiklas Schnelle sysfs_unbreak_active_protection(kn);
132576c75e3SNiklas Schnelle return ret ? ret : count;
1330ff70ec8SSebastian Ott }
134b346953dSSebastian Ott static DEVICE_ATTR_WO(recover);
1350ff70ec8SSebastian Ott
util_string_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)136ac4995b9SSebastian Ott static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
137ac4995b9SSebastian Ott struct bin_attribute *attr, char *buf,
138ac4995b9SSebastian Ott loff_t off, size_t count)
139ac4995b9SSebastian Ott {
140ac4995b9SSebastian Ott struct device *dev = kobj_to_dev(kobj);
141ac4995b9SSebastian Ott struct pci_dev *pdev = to_pci_dev(dev);
142198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev);
143ac4995b9SSebastian Ott
144ac4995b9SSebastian Ott return memory_read_from_buffer(buf, count, &off, zdev->util_str,
145ac4995b9SSebastian Ott sizeof(zdev->util_str));
146ac4995b9SSebastian Ott }
147ac4995b9SSebastian Ott static BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
148368704a6SSebastian Ott
report_error_write(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)149368704a6SSebastian Ott static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
150368704a6SSebastian Ott struct bin_attribute *attr, char *buf,
151368704a6SSebastian Ott loff_t off, size_t count)
152368704a6SSebastian Ott {
153368704a6SSebastian Ott struct zpci_report_error_header *report = (void *) buf;
154368704a6SSebastian Ott struct device *dev = kobj_to_dev(kobj);
155368704a6SSebastian Ott struct pci_dev *pdev = to_pci_dev(dev);
156368704a6SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev);
157368704a6SSebastian Ott int ret;
158368704a6SSebastian Ott
159368704a6SSebastian Ott if (off || (count < sizeof(*report)))
160368704a6SSebastian Ott return -EINVAL;
161368704a6SSebastian Ott
162368704a6SSebastian Ott ret = sclp_pci_report(report, zdev->fh, zdev->fid);
163368704a6SSebastian Ott
164368704a6SSebastian Ott return ret ? ret : count;
165368704a6SSebastian Ott }
166368704a6SSebastian Ott static BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
167368704a6SSebastian Ott
uid_is_unique_show(struct device * dev,struct device_attribute * attr,char * buf)168408f2c9cSNiklas Schnelle static ssize_t uid_is_unique_show(struct device *dev,
169408f2c9cSNiklas Schnelle struct device_attribute *attr, char *buf)
170408f2c9cSNiklas Schnelle {
171408f2c9cSNiklas Schnelle return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
172408f2c9cSNiklas Schnelle }
173408f2c9cSNiklas Schnelle static DEVICE_ATTR_RO(uid_is_unique);
174408f2c9cSNiklas Schnelle
17581bbf039SNiklas Schnelle /* analogous to smbios index */
index_show(struct device * dev,struct device_attribute * attr,char * buf)17681bbf039SNiklas Schnelle static ssize_t index_show(struct device *dev,
17781bbf039SNiklas Schnelle struct device_attribute *attr, char *buf)
17881bbf039SNiklas Schnelle {
17981bbf039SNiklas Schnelle struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
18081bbf039SNiklas Schnelle u32 index = ~0;
18181bbf039SNiklas Schnelle
18281bbf039SNiklas Schnelle if (zpci_unique_uid)
18381bbf039SNiklas Schnelle index = zdev->uid;
18481bbf039SNiklas Schnelle
18581bbf039SNiklas Schnelle return sysfs_emit(buf, "%u\n", index);
18681bbf039SNiklas Schnelle }
18781bbf039SNiklas Schnelle static DEVICE_ATTR_RO(index);
18881bbf039SNiklas Schnelle
zpci_index_is_visible(struct kobject * kobj,struct attribute * attr,int n)18981bbf039SNiklas Schnelle static umode_t zpci_index_is_visible(struct kobject *kobj,
19081bbf039SNiklas Schnelle struct attribute *attr, int n)
19181bbf039SNiklas Schnelle {
19281bbf039SNiklas Schnelle return zpci_unique_uid ? attr->mode : 0;
19381bbf039SNiklas Schnelle }
19481bbf039SNiklas Schnelle
19581bbf039SNiklas Schnelle static struct attribute *zpci_ident_attrs[] = {
19681bbf039SNiklas Schnelle &dev_attr_index.attr,
19781bbf039SNiklas Schnelle NULL,
19881bbf039SNiklas Schnelle };
19981bbf039SNiklas Schnelle
20081bbf039SNiklas Schnelle static struct attribute_group zpci_ident_attr_group = {
20181bbf039SNiklas Schnelle .attrs = zpci_ident_attrs,
20281bbf039SNiklas Schnelle .is_visible = zpci_index_is_visible,
20381bbf039SNiklas Schnelle };
20481bbf039SNiklas Schnelle
205ac4995b9SSebastian Ott static struct bin_attribute *zpci_bin_attrs[] = {
206ac4995b9SSebastian Ott &bin_attr_util_string,
207368704a6SSebastian Ott &bin_attr_report_error,
208ac4995b9SSebastian Ott NULL,
209ac4995b9SSebastian Ott };
210ac4995b9SSebastian Ott
21193065d04SSebastian Ott static struct attribute *zpci_dev_attrs[] = {
21293065d04SSebastian Ott &dev_attr_function_id.attr,
21393065d04SSebastian Ott &dev_attr_function_handle.attr,
21493065d04SSebastian Ott &dev_attr_pchid.attr,
21593065d04SSebastian Ott &dev_attr_pfgid.attr,
216ac4995b9SSebastian Ott &dev_attr_pft.attr,
217e6ab7490SAlexander Schmidt &dev_attr_port.attr,
218ac4995b9SSebastian Ott &dev_attr_vfn.attr,
219ac4995b9SSebastian Ott &dev_attr_uid.attr,
22093065d04SSebastian Ott &dev_attr_recover.attr,
2218e4708b3SSebastian Ott &dev_attr_mio_enabled.attr,
222408f2c9cSNiklas Schnelle &dev_attr_uid_is_unique.attr,
2231e8da956SJan Glauber NULL,
2241e8da956SJan Glauber };
225408f2c9cSNiklas Schnelle
22693065d04SSebastian Ott static struct attribute_group zpci_attr_group = {
22793065d04SSebastian Ott .attrs = zpci_dev_attrs,
228ac4995b9SSebastian Ott .bin_attrs = zpci_bin_attrs,
22993065d04SSebastian Ott };
230ac4995b9SSebastian Ott
231ac4995b9SSebastian Ott static struct attribute *pfip_attrs[] = {
232ac4995b9SSebastian Ott &dev_attr_segment0.attr,
233ac4995b9SSebastian Ott &dev_attr_segment1.attr,
234ac4995b9SSebastian Ott &dev_attr_segment2.attr,
235ac4995b9SSebastian Ott &dev_attr_segment3.attr,
236ac4995b9SSebastian Ott NULL,
237ac4995b9SSebastian Ott };
238ac4995b9SSebastian Ott static struct attribute_group pfip_attr_group = {
239ac4995b9SSebastian Ott .name = "pfip",
240ac4995b9SSebastian Ott .attrs = pfip_attrs,
241ac4995b9SSebastian Ott };
242ac4995b9SSebastian Ott
24393065d04SSebastian Ott const struct attribute_group *zpci_attr_groups[] = {
24493065d04SSebastian Ott &zpci_attr_group,
245ac4995b9SSebastian Ott &pfip_attr_group,
24681bbf039SNiklas Schnelle &zpci_ident_attr_group,
24793065d04SSebastian Ott NULL,
24893065d04SSebastian Ott };
249