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