1bbbd7f11SThomas Huth // SPDX-License-Identifier: GPL-2.0-or-later
2317f06deSGavin Shan /*
3317f06deSGavin Shan * Sysfs entries for PCI Error Recovery for PAPR-compliant platform.
4317f06deSGavin Shan * Copyright IBM Corporation 2007
5317f06deSGavin Shan * Copyright Linas Vepstas <linas@austin.ibm.com> 2007
6317f06deSGavin Shan *
7317f06deSGavin Shan * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
8317f06deSGavin Shan */
9*e6f6390aSChristophe Leroy #include <linux/of.h>
10317f06deSGavin Shan #include <linux/pci.h>
11317f06deSGavin Shan #include <linux/stat.h>
12317f06deSGavin Shan #include <asm/ppc-pci.h>
13317f06deSGavin Shan #include <asm/pci-bridge.h>
14317f06deSGavin Shan
15317f06deSGavin Shan /**
16317f06deSGavin Shan * EEH_SHOW_ATTR -- Create sysfs entry for eeh statistic
17317f06deSGavin Shan * @_name: name of file in sysfs directory
1889f51839SOliver O'Halloran * @_memb: name of member in struct eeh_dev to access
19317f06deSGavin Shan * @_format: printf format for display
20317f06deSGavin Shan *
21317f06deSGavin Shan * All of the attributes look very similar, so just
22317f06deSGavin Shan * auto-gen a cut-n-paste routine to display them.
23317f06deSGavin Shan */
24317f06deSGavin Shan #define EEH_SHOW_ATTR(_name,_memb,_format) \
25317f06deSGavin Shan static ssize_t eeh_show_##_name(struct device *dev, \
26317f06deSGavin Shan struct device_attribute *attr, char *buf) \
27317f06deSGavin Shan { \
28317f06deSGavin Shan struct pci_dev *pdev = to_pci_dev(dev); \
29317f06deSGavin Shan struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev); \
30317f06deSGavin Shan \
31317f06deSGavin Shan if (!edev) \
32317f06deSGavin Shan return 0; \
33317f06deSGavin Shan \
34317f06deSGavin Shan return sprintf(buf, _format "\n", edev->_memb); \
35317f06deSGavin Shan } \
3657ad583fSRussell Currey static DEVICE_ATTR(_name, 0444, eeh_show_##_name, NULL);
37317f06deSGavin Shan
38317f06deSGavin Shan EEH_SHOW_ATTR(eeh_mode, mode, "0x%x");
39317f06deSGavin Shan EEH_SHOW_ATTR(eeh_pe_config_addr, pe_config_addr, "0x%x");
40317f06deSGavin Shan
eeh_pe_state_show(struct device * dev,struct device_attribute * attr,char * buf)41940376b3SGavin Shan static ssize_t eeh_pe_state_show(struct device *dev,
42940376b3SGavin Shan struct device_attribute *attr, char *buf)
43940376b3SGavin Shan {
44940376b3SGavin Shan struct pci_dev *pdev = to_pci_dev(dev);
45940376b3SGavin Shan struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
46940376b3SGavin Shan int state;
47940376b3SGavin Shan
48940376b3SGavin Shan if (!edev || !edev->pe)
49940376b3SGavin Shan return -ENODEV;
50940376b3SGavin Shan
51940376b3SGavin Shan state = eeh_ops->get_state(edev->pe, NULL);
527531473cSGavin Shan return sprintf(buf, "0x%08x 0x%08x\n",
53940376b3SGavin Shan state, edev->pe->state);
54940376b3SGavin Shan }
55940376b3SGavin Shan
eeh_pe_state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)56940376b3SGavin Shan static ssize_t eeh_pe_state_store(struct device *dev,
57940376b3SGavin Shan struct device_attribute *attr,
58940376b3SGavin Shan const char *buf, size_t count)
59940376b3SGavin Shan {
60940376b3SGavin Shan struct pci_dev *pdev = to_pci_dev(dev);
61940376b3SGavin Shan struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
62940376b3SGavin Shan
63940376b3SGavin Shan if (!edev || !edev->pe)
64940376b3SGavin Shan return -ENODEV;
65940376b3SGavin Shan
66940376b3SGavin Shan /* Nothing to do if it's not frozen */
67940376b3SGavin Shan if (!(edev->pe->state & EEH_PE_ISOLATED))
68940376b3SGavin Shan return count;
69940376b3SGavin Shan
70188fdea6SSam Bobroff if (eeh_unfreeze_pe(edev->pe))
71940376b3SGavin Shan return -EIO;
729ed5ca66SSam Bobroff eeh_pe_state_clear(edev->pe, EEH_PE_ISOLATED, true);
73940376b3SGavin Shan
74940376b3SGavin Shan return count;
75940376b3SGavin Shan }
76940376b3SGavin Shan
77940376b3SGavin Shan static DEVICE_ATTR_RW(eeh_pe_state);
78940376b3SGavin Shan
794107248cSOliver O'Halloran #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PPC_PSERIES)
eeh_notify_resume_show(struct device * dev,struct device_attribute * attr,char * buf)806ea3df69SBryant G. Ly static ssize_t eeh_notify_resume_show(struct device *dev,
816ea3df69SBryant G. Ly struct device_attribute *attr, char *buf)
826ea3df69SBryant G. Ly {
836ea3df69SBryant G. Ly struct pci_dev *pdev = to_pci_dev(dev);
846ea3df69SBryant G. Ly struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
856ea3df69SBryant G. Ly struct pci_dn *pdn = pci_get_pdn(pdev);
866ea3df69SBryant G. Ly
876ea3df69SBryant G. Ly if (!edev || !edev->pe)
886ea3df69SBryant G. Ly return -ENODEV;
896ea3df69SBryant G. Ly
906ea3df69SBryant G. Ly return sprintf(buf, "%d\n", pdn->last_allow_rc);
916ea3df69SBryant G. Ly }
926ea3df69SBryant G. Ly
eeh_notify_resume_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)936ea3df69SBryant G. Ly static ssize_t eeh_notify_resume_store(struct device *dev,
946ea3df69SBryant G. Ly struct device_attribute *attr,
956ea3df69SBryant G. Ly const char *buf, size_t count)
966ea3df69SBryant G. Ly {
976ea3df69SBryant G. Ly struct pci_dev *pdev = to_pci_dev(dev);
986ea3df69SBryant G. Ly struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
996ea3df69SBryant G. Ly
1006ea3df69SBryant G. Ly if (!edev || !edev->pe || !eeh_ops->notify_resume)
1016ea3df69SBryant G. Ly return -ENODEV;
1026ea3df69SBryant G. Ly
1038225d543SOliver O'Halloran if (eeh_ops->notify_resume(edev))
1046ea3df69SBryant G. Ly return -EIO;
1056ea3df69SBryant G. Ly
1066ea3df69SBryant G. Ly return count;
1076ea3df69SBryant G. Ly }
1086ea3df69SBryant G. Ly static DEVICE_ATTR_RW(eeh_notify_resume);
1096ea3df69SBryant G. Ly
eeh_notify_resume_add(struct pci_dev * pdev)1106ea3df69SBryant G. Ly static int eeh_notify_resume_add(struct pci_dev *pdev)
1116ea3df69SBryant G. Ly {
1126ea3df69SBryant G. Ly struct device_node *np;
1136ea3df69SBryant G. Ly int rc = 0;
1146ea3df69SBryant G. Ly
1156ea3df69SBryant G. Ly np = pci_device_to_OF_node(pdev->is_physfn ? pdev : pdev->physfn);
1166ea3df69SBryant G. Ly
1176ea3df69SBryant G. Ly if (of_property_read_bool(np, "ibm,is-open-sriov-pf"))
1186ea3df69SBryant G. Ly rc = device_create_file(&pdev->dev, &dev_attr_eeh_notify_resume);
1196ea3df69SBryant G. Ly
1206ea3df69SBryant G. Ly return rc;
1216ea3df69SBryant G. Ly }
1226ea3df69SBryant G. Ly
eeh_notify_resume_remove(struct pci_dev * pdev)1236ea3df69SBryant G. Ly static void eeh_notify_resume_remove(struct pci_dev *pdev)
1246ea3df69SBryant G. Ly {
1256ea3df69SBryant G. Ly struct device_node *np;
1266ea3df69SBryant G. Ly
1276ea3df69SBryant G. Ly np = pci_device_to_OF_node(pdev->is_physfn ? pdev : pdev->physfn);
1286ea3df69SBryant G. Ly
1296ea3df69SBryant G. Ly if (of_property_read_bool(np, "ibm,is-open-sriov-pf"))
1306ea3df69SBryant G. Ly device_remove_file(&pdev->dev, &dev_attr_eeh_notify_resume);
1316ea3df69SBryant G. Ly }
1326ea3df69SBryant G. Ly #else
eeh_notify_resume_add(struct pci_dev * pdev)1336ea3df69SBryant G. Ly static inline int eeh_notify_resume_add(struct pci_dev *pdev) { return 0; }
eeh_notify_resume_remove(struct pci_dev * pdev)1346ea3df69SBryant G. Ly static inline void eeh_notify_resume_remove(struct pci_dev *pdev) { }
1354107248cSOliver O'Halloran #endif /* CONFIG_PCI_IOV && CONFIG PPC_PSERIES*/
1366ea3df69SBryant G. Ly
eeh_sysfs_add_device(struct pci_dev * pdev)137317f06deSGavin Shan void eeh_sysfs_add_device(struct pci_dev *pdev)
138317f06deSGavin Shan {
139ab55d218SGavin Shan struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
140317f06deSGavin Shan int rc=0;
141317f06deSGavin Shan
1422213fb14SWei Yang if (!eeh_enabled())
1432213fb14SWei Yang return;
1442213fb14SWei Yang
145ab55d218SGavin Shan if (edev && (edev->mode & EEH_DEV_SYSFS))
146ab55d218SGavin Shan return;
147ab55d218SGavin Shan
148317f06deSGavin Shan rc += device_create_file(&pdev->dev, &dev_attr_eeh_mode);
149317f06deSGavin Shan rc += device_create_file(&pdev->dev, &dev_attr_eeh_pe_config_addr);
150940376b3SGavin Shan rc += device_create_file(&pdev->dev, &dev_attr_eeh_pe_state);
1516ea3df69SBryant G. Ly rc += eeh_notify_resume_add(pdev);
152317f06deSGavin Shan
153317f06deSGavin Shan if (rc)
154940376b3SGavin Shan pr_warn("EEH: Unable to create sysfs entries\n");
155ab55d218SGavin Shan else if (edev)
156ab55d218SGavin Shan edev->mode |= EEH_DEV_SYSFS;
157317f06deSGavin Shan }
158317f06deSGavin Shan
eeh_sysfs_remove_device(struct pci_dev * pdev)159317f06deSGavin Shan void eeh_sysfs_remove_device(struct pci_dev *pdev)
160317f06deSGavin Shan {
161ab55d218SGavin Shan struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
162ab55d218SGavin Shan
1633489cdc4SOliver O'Halloran if (!edev) {
1643489cdc4SOliver O'Halloran WARN_ON(eeh_enabled());
1653489cdc4SOliver O'Halloran return;
1663489cdc4SOliver O'Halloran }
1673489cdc4SOliver O'Halloran
1683489cdc4SOliver O'Halloran edev->mode &= ~EEH_DEV_SYSFS;
1693489cdc4SOliver O'Halloran
170f5c57710SGavin Shan /*
171f5c57710SGavin Shan * The parent directory might have been removed. We needn't
172f5c57710SGavin Shan * continue for that case.
173f5c57710SGavin Shan */
1743489cdc4SOliver O'Halloran if (!pdev->dev.kobj.sd)
175f5c57710SGavin Shan return;
176f5c57710SGavin Shan
177317f06deSGavin Shan device_remove_file(&pdev->dev, &dev_attr_eeh_mode);
178317f06deSGavin Shan device_remove_file(&pdev->dev, &dev_attr_eeh_pe_config_addr);
179940376b3SGavin Shan device_remove_file(&pdev->dev, &dev_attr_eeh_pe_state);
180ab55d218SGavin Shan
1816ea3df69SBryant G. Ly eeh_notify_resume_remove(pdev);
182317f06deSGavin Shan }
183