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