| .. | .. |
|---|
| 3 | 3 | #include <linux/sysfs.h> |
|---|
| 4 | 4 | #include "ocxl_internal.h" |
|---|
| 5 | 5 | |
|---|
| 6 | +static inline struct ocxl_afu *to_afu(struct device *device) |
|---|
| 7 | +{ |
|---|
| 8 | + struct ocxl_file_info *info = container_of(device, struct ocxl_file_info, dev); |
|---|
| 9 | + |
|---|
| 10 | + return info->afu; |
|---|
| 11 | +} |
|---|
| 12 | + |
|---|
| 6 | 13 | static ssize_t global_mmio_size_show(struct device *device, |
|---|
| 7 | 14 | struct device_attribute *attr, |
|---|
| 8 | 15 | char *buf) |
|---|
| 9 | 16 | { |
|---|
| 10 | | - struct ocxl_afu *afu = to_ocxl_afu(device); |
|---|
| 17 | + struct ocxl_afu *afu = to_afu(device); |
|---|
| 11 | 18 | |
|---|
| 12 | 19 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
|---|
| 13 | 20 | afu->config.global_mmio_size); |
|---|
| .. | .. |
|---|
| 17 | 24 | struct device_attribute *attr, |
|---|
| 18 | 25 | char *buf) |
|---|
| 19 | 26 | { |
|---|
| 20 | | - struct ocxl_afu *afu = to_ocxl_afu(device); |
|---|
| 27 | + struct ocxl_afu *afu = to_afu(device); |
|---|
| 21 | 28 | |
|---|
| 22 | 29 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
|---|
| 23 | 30 | afu->config.pp_mmio_stride); |
|---|
| .. | .. |
|---|
| 27 | 34 | struct device_attribute *attr, |
|---|
| 28 | 35 | char *buf) |
|---|
| 29 | 36 | { |
|---|
| 30 | | - struct ocxl_afu *afu = to_ocxl_afu(device); |
|---|
| 37 | + struct ocxl_afu *afu = to_afu(device); |
|---|
| 31 | 38 | |
|---|
| 32 | 39 | return scnprintf(buf, PAGE_SIZE, "%hhu:%hhu\n", |
|---|
| 33 | 40 | afu->config.version_major, |
|---|
| .. | .. |
|---|
| 38 | 45 | struct device_attribute *attr, |
|---|
| 39 | 46 | char *buf) |
|---|
| 40 | 47 | { |
|---|
| 41 | | - struct ocxl_afu *afu = to_ocxl_afu(device); |
|---|
| 48 | + struct ocxl_afu *afu = to_afu(device); |
|---|
| 42 | 49 | |
|---|
| 43 | 50 | return scnprintf(buf, PAGE_SIZE, "%d/%d\n", |
|---|
| 44 | 51 | afu->pasid_count, afu->pasid_max); |
|---|
| 52 | +} |
|---|
| 53 | + |
|---|
| 54 | +static ssize_t reload_on_reset_show(struct device *device, |
|---|
| 55 | + struct device_attribute *attr, |
|---|
| 56 | + char *buf) |
|---|
| 57 | +{ |
|---|
| 58 | + struct ocxl_afu *afu = to_afu(device); |
|---|
| 59 | + struct ocxl_fn *fn = afu->fn; |
|---|
| 60 | + struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent); |
|---|
| 61 | + int val; |
|---|
| 62 | + |
|---|
| 63 | + if (ocxl_config_get_reset_reload(pci_dev, &val)) |
|---|
| 64 | + return scnprintf(buf, PAGE_SIZE, "unavailable\n"); |
|---|
| 65 | + |
|---|
| 66 | + return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
|---|
| 67 | +} |
|---|
| 68 | + |
|---|
| 69 | +static ssize_t reload_on_reset_store(struct device *device, |
|---|
| 70 | + struct device_attribute *attr, |
|---|
| 71 | + const char *buf, size_t count) |
|---|
| 72 | +{ |
|---|
| 73 | + struct ocxl_afu *afu = to_afu(device); |
|---|
| 74 | + struct ocxl_fn *fn = afu->fn; |
|---|
| 75 | + struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent); |
|---|
| 76 | + int rc, val; |
|---|
| 77 | + |
|---|
| 78 | + rc = kstrtoint(buf, 0, &val); |
|---|
| 79 | + if (rc || (val != 0 && val != 1)) |
|---|
| 80 | + return -EINVAL; |
|---|
| 81 | + |
|---|
| 82 | + if (ocxl_config_set_reset_reload(pci_dev, val)) |
|---|
| 83 | + return -ENODEV; |
|---|
| 84 | + |
|---|
| 85 | + return count; |
|---|
| 45 | 86 | } |
|---|
| 46 | 87 | |
|---|
| 47 | 88 | static struct device_attribute afu_attrs[] = { |
|---|
| .. | .. |
|---|
| 49 | 90 | __ATTR_RO(pp_mmio_size), |
|---|
| 50 | 91 | __ATTR_RO(afu_version), |
|---|
| 51 | 92 | __ATTR_RO(contexts), |
|---|
| 93 | + __ATTR_RW(reload_on_reset), |
|---|
| 52 | 94 | }; |
|---|
| 53 | 95 | |
|---|
| 54 | 96 | static ssize_t global_mmio_read(struct file *filp, struct kobject *kobj, |
|---|
| 55 | 97 | struct bin_attribute *bin_attr, char *buf, |
|---|
| 56 | 98 | loff_t off, size_t count) |
|---|
| 57 | 99 | { |
|---|
| 58 | | - struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj)); |
|---|
| 100 | + struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj)); |
|---|
| 59 | 101 | |
|---|
| 60 | 102 | if (count == 0 || off < 0 || |
|---|
| 61 | 103 | off >= afu->config.global_mmio_size) |
|---|
| .. | .. |
|---|
| 86 | 128 | struct bin_attribute *bin_attr, |
|---|
| 87 | 129 | struct vm_area_struct *vma) |
|---|
| 88 | 130 | { |
|---|
| 89 | | - struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj)); |
|---|
| 131 | + struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj)); |
|---|
| 90 | 132 | |
|---|
| 91 | 133 | if ((vma_pages(vma) + vma->vm_pgoff) > |
|---|
| 92 | 134 | (afu->config.global_mmio_size >> PAGE_SHIFT)) |
|---|
| .. | .. |
|---|
| 99 | 141 | return 0; |
|---|
| 100 | 142 | } |
|---|
| 101 | 143 | |
|---|
| 102 | | -int ocxl_sysfs_add_afu(struct ocxl_afu *afu) |
|---|
| 144 | +int ocxl_sysfs_register_afu(struct ocxl_file_info *info) |
|---|
| 103 | 145 | { |
|---|
| 104 | 146 | int i, rc; |
|---|
| 105 | 147 | |
|---|
| 106 | 148 | for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) { |
|---|
| 107 | | - rc = device_create_file(&afu->dev, &afu_attrs[i]); |
|---|
| 149 | + rc = device_create_file(&info->dev, &afu_attrs[i]); |
|---|
| 108 | 150 | if (rc) |
|---|
| 109 | 151 | goto err; |
|---|
| 110 | 152 | } |
|---|
| 111 | 153 | |
|---|
| 112 | | - sysfs_attr_init(&afu->attr_global_mmio.attr); |
|---|
| 113 | | - afu->attr_global_mmio.attr.name = "global_mmio_area"; |
|---|
| 114 | | - afu->attr_global_mmio.attr.mode = 0600; |
|---|
| 115 | | - afu->attr_global_mmio.size = afu->config.global_mmio_size; |
|---|
| 116 | | - afu->attr_global_mmio.read = global_mmio_read; |
|---|
| 117 | | - afu->attr_global_mmio.mmap = global_mmio_mmap; |
|---|
| 118 | | - rc = device_create_bin_file(&afu->dev, &afu->attr_global_mmio); |
|---|
| 154 | + sysfs_attr_init(&info->attr_global_mmio.attr); |
|---|
| 155 | + info->attr_global_mmio.attr.name = "global_mmio_area"; |
|---|
| 156 | + info->attr_global_mmio.attr.mode = 0600; |
|---|
| 157 | + info->attr_global_mmio.size = info->afu->config.global_mmio_size; |
|---|
| 158 | + info->attr_global_mmio.read = global_mmio_read; |
|---|
| 159 | + info->attr_global_mmio.mmap = global_mmio_mmap; |
|---|
| 160 | + rc = device_create_bin_file(&info->dev, &info->attr_global_mmio); |
|---|
| 119 | 161 | if (rc) { |
|---|
| 120 | | - dev_err(&afu->dev, |
|---|
| 121 | | - "Unable to create global mmio attr for afu: %d\n", |
|---|
| 122 | | - rc); |
|---|
| 162 | + dev_err(&info->dev, "Unable to create global mmio attr for afu: %d\n", rc); |
|---|
| 123 | 163 | goto err; |
|---|
| 124 | 164 | } |
|---|
| 125 | 165 | |
|---|
| .. | .. |
|---|
| 127 | 167 | |
|---|
| 128 | 168 | err: |
|---|
| 129 | 169 | for (i--; i >= 0; i--) |
|---|
| 130 | | - device_remove_file(&afu->dev, &afu_attrs[i]); |
|---|
| 170 | + device_remove_file(&info->dev, &afu_attrs[i]); |
|---|
| 171 | + |
|---|
| 131 | 172 | return rc; |
|---|
| 132 | 173 | } |
|---|
| 133 | 174 | |
|---|
| 134 | | -void ocxl_sysfs_remove_afu(struct ocxl_afu *afu) |
|---|
| 175 | +void ocxl_sysfs_unregister_afu(struct ocxl_file_info *info) |
|---|
| 135 | 176 | { |
|---|
| 136 | 177 | int i; |
|---|
| 137 | 178 | |
|---|
| 179 | + /* |
|---|
| 180 | + * device_remove_bin_file is safe to call if the file is not added as |
|---|
| 181 | + * the files are removed by name, and early exit if not found |
|---|
| 182 | + */ |
|---|
| 138 | 183 | for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) |
|---|
| 139 | | - device_remove_file(&afu->dev, &afu_attrs[i]); |
|---|
| 140 | | - device_remove_bin_file(&afu->dev, &afu->attr_global_mmio); |
|---|
| 184 | + device_remove_file(&info->dev, &afu_attrs[i]); |
|---|
| 185 | + device_remove_bin_file(&info->dev, &info->attr_global_mmio); |
|---|
| 141 | 186 | } |
|---|