Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2014 IBM Corp.
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/device.h>
0008 #include <linux/sysfs.h>
0009 #include <linux/pci_regs.h>
0010 
0011 #include "cxl.h"
0012 
0013 #define to_afu_chardev_m(d) dev_get_drvdata(d)
0014 
0015 /*********  Adapter attributes  **********************************************/
0016 
0017 static ssize_t caia_version_show(struct device *device,
0018                  struct device_attribute *attr,
0019                  char *buf)
0020 {
0021     struct cxl *adapter = to_cxl_adapter(device);
0022 
0023     return scnprintf(buf, PAGE_SIZE, "%i.%i\n", adapter->caia_major,
0024              adapter->caia_minor);
0025 }
0026 
0027 static ssize_t psl_revision_show(struct device *device,
0028                  struct device_attribute *attr,
0029                  char *buf)
0030 {
0031     struct cxl *adapter = to_cxl_adapter(device);
0032 
0033     return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_rev);
0034 }
0035 
0036 static ssize_t base_image_show(struct device *device,
0037                    struct device_attribute *attr,
0038                    char *buf)
0039 {
0040     struct cxl *adapter = to_cxl_adapter(device);
0041 
0042     return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->base_image);
0043 }
0044 
0045 static ssize_t image_loaded_show(struct device *device,
0046                  struct device_attribute *attr,
0047                  char *buf)
0048 {
0049     struct cxl *adapter = to_cxl_adapter(device);
0050 
0051     if (adapter->user_image_loaded)
0052         return scnprintf(buf, PAGE_SIZE, "user\n");
0053     return scnprintf(buf, PAGE_SIZE, "factory\n");
0054 }
0055 
0056 static ssize_t psl_timebase_synced_show(struct device *device,
0057                     struct device_attribute *attr,
0058                     char *buf)
0059 {
0060     struct cxl *adapter = to_cxl_adapter(device);
0061     u64 psl_tb, delta;
0062 
0063     /* Recompute the status only in native mode */
0064     if (cpu_has_feature(CPU_FTR_HVMODE)) {
0065         psl_tb = adapter->native->sl_ops->timebase_read(adapter);
0066         delta = abs(mftb() - psl_tb);
0067 
0068         /* CORE TB and PSL TB difference <= 16usecs ? */
0069         adapter->psl_timebase_synced = (tb_to_ns(delta) < 16000) ? true : false;
0070         pr_devel("PSL timebase %s - delta: 0x%016llx\n",
0071              (tb_to_ns(delta) < 16000) ? "synchronized" :
0072              "not synchronized", tb_to_ns(delta));
0073     }
0074     return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_timebase_synced);
0075 }
0076 
0077 static ssize_t tunneled_ops_supported_show(struct device *device,
0078                     struct device_attribute *attr,
0079                     char *buf)
0080 {
0081     struct cxl *adapter = to_cxl_adapter(device);
0082 
0083     return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->tunneled_ops_supported);
0084 }
0085 
0086 static ssize_t reset_adapter_store(struct device *device,
0087                    struct device_attribute *attr,
0088                    const char *buf, size_t count)
0089 {
0090     struct cxl *adapter = to_cxl_adapter(device);
0091     int rc;
0092     int val;
0093 
0094     rc = sscanf(buf, "%i", &val);
0095     if ((rc != 1) || (val != 1 && val != -1))
0096         return -EINVAL;
0097 
0098     /*
0099      * See if we can lock the context mapping that's only allowed
0100      * when there are no contexts attached to the adapter. Once
0101      * taken this will also prevent any context from getting activated.
0102      */
0103     if (val == 1) {
0104         rc =  cxl_adapter_context_lock(adapter);
0105         if (rc)
0106             goto out;
0107 
0108         rc = cxl_ops->adapter_reset(adapter);
0109         /* In case reset failed release context lock */
0110         if (rc)
0111             cxl_adapter_context_unlock(adapter);
0112 
0113     } else if (val == -1) {
0114         /* Perform a forced adapter reset */
0115         rc = cxl_ops->adapter_reset(adapter);
0116     }
0117 
0118 out:
0119     return rc ? rc : count;
0120 }
0121 
0122 static ssize_t load_image_on_perst_show(struct device *device,
0123                  struct device_attribute *attr,
0124                  char *buf)
0125 {
0126     struct cxl *adapter = to_cxl_adapter(device);
0127 
0128     if (!adapter->perst_loads_image)
0129         return scnprintf(buf, PAGE_SIZE, "none\n");
0130 
0131     if (adapter->perst_select_user)
0132         return scnprintf(buf, PAGE_SIZE, "user\n");
0133     return scnprintf(buf, PAGE_SIZE, "factory\n");
0134 }
0135 
0136 static ssize_t load_image_on_perst_store(struct device *device,
0137                  struct device_attribute *attr,
0138                  const char *buf, size_t count)
0139 {
0140     struct cxl *adapter = to_cxl_adapter(device);
0141     int rc;
0142 
0143     if (!strncmp(buf, "none", 4))
0144         adapter->perst_loads_image = false;
0145     else if (!strncmp(buf, "user", 4)) {
0146         adapter->perst_select_user = true;
0147         adapter->perst_loads_image = true;
0148     } else if (!strncmp(buf, "factory", 7)) {
0149         adapter->perst_select_user = false;
0150         adapter->perst_loads_image = true;
0151     } else
0152         return -EINVAL;
0153 
0154     if ((rc = cxl_update_image_control(adapter)))
0155         return rc;
0156 
0157     return count;
0158 }
0159 
0160 static ssize_t perst_reloads_same_image_show(struct device *device,
0161                  struct device_attribute *attr,
0162                  char *buf)
0163 {
0164     struct cxl *adapter = to_cxl_adapter(device);
0165 
0166     return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->perst_same_image);
0167 }
0168 
0169 static ssize_t perst_reloads_same_image_store(struct device *device,
0170                  struct device_attribute *attr,
0171                  const char *buf, size_t count)
0172 {
0173     struct cxl *adapter = to_cxl_adapter(device);
0174     int rc;
0175     int val;
0176 
0177     rc = sscanf(buf, "%i", &val);
0178     if ((rc != 1) || !(val == 1 || val == 0))
0179         return -EINVAL;
0180 
0181     adapter->perst_same_image = (val == 1);
0182     return count;
0183 }
0184 
0185 static struct device_attribute adapter_attrs[] = {
0186     __ATTR_RO(caia_version),
0187     __ATTR_RO(psl_revision),
0188     __ATTR_RO(base_image),
0189     __ATTR_RO(image_loaded),
0190     __ATTR_RO(psl_timebase_synced),
0191     __ATTR_RO(tunneled_ops_supported),
0192     __ATTR_RW(load_image_on_perst),
0193     __ATTR_RW(perst_reloads_same_image),
0194     __ATTR(reset, S_IWUSR, NULL, reset_adapter_store),
0195 };
0196 
0197 
0198 /*********  AFU master specific attributes  **********************************/
0199 
0200 static ssize_t mmio_size_show_master(struct device *device,
0201                      struct device_attribute *attr,
0202                      char *buf)
0203 {
0204     struct cxl_afu *afu = to_afu_chardev_m(device);
0205 
0206     return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
0207 }
0208 
0209 static ssize_t pp_mmio_off_show(struct device *device,
0210                 struct device_attribute *attr,
0211                 char *buf)
0212 {
0213     struct cxl_afu *afu = to_afu_chardev_m(device);
0214 
0215     return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->native->pp_offset);
0216 }
0217 
0218 static ssize_t pp_mmio_len_show(struct device *device,
0219                 struct device_attribute *attr,
0220                 char *buf)
0221 {
0222     struct cxl_afu *afu = to_afu_chardev_m(device);
0223 
0224     return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
0225 }
0226 
0227 static struct device_attribute afu_master_attrs[] = {
0228     __ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
0229     __ATTR_RO(pp_mmio_off),
0230     __ATTR_RO(pp_mmio_len),
0231 };
0232 
0233 
0234 /*********  AFU attributes  **************************************************/
0235 
0236 static ssize_t mmio_size_show(struct device *device,
0237                   struct device_attribute *attr,
0238                   char *buf)
0239 {
0240     struct cxl_afu *afu = to_cxl_afu(device);
0241 
0242     if (afu->pp_size)
0243         return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
0244     return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
0245 }
0246 
0247 static ssize_t reset_store_afu(struct device *device,
0248                    struct device_attribute *attr,
0249                    const char *buf, size_t count)
0250 {
0251     struct cxl_afu *afu = to_cxl_afu(device);
0252     int rc;
0253 
0254     /* Not safe to reset if it is currently in use */
0255     mutex_lock(&afu->contexts_lock);
0256     if (!idr_is_empty(&afu->contexts_idr)) {
0257         rc = -EBUSY;
0258         goto err;
0259     }
0260 
0261     if ((rc = cxl_ops->afu_reset(afu)))
0262         goto err;
0263 
0264     rc = count;
0265 err:
0266     mutex_unlock(&afu->contexts_lock);
0267     return rc;
0268 }
0269 
0270 static ssize_t irqs_min_show(struct device *device,
0271                  struct device_attribute *attr,
0272                  char *buf)
0273 {
0274     struct cxl_afu *afu = to_cxl_afu(device);
0275 
0276     return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
0277 }
0278 
0279 static ssize_t irqs_max_show(struct device *device,
0280                   struct device_attribute *attr,
0281                   char *buf)
0282 {
0283     struct cxl_afu *afu = to_cxl_afu(device);
0284 
0285     return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
0286 }
0287 
0288 static ssize_t irqs_max_store(struct device *device,
0289                   struct device_attribute *attr,
0290                   const char *buf, size_t count)
0291 {
0292     struct cxl_afu *afu = to_cxl_afu(device);
0293     ssize_t ret;
0294     int irqs_max;
0295 
0296     ret = sscanf(buf, "%i", &irqs_max);
0297     if (ret != 1)
0298         return -EINVAL;
0299 
0300     if (irqs_max < afu->pp_irqs)
0301         return -EINVAL;
0302 
0303     if (cpu_has_feature(CPU_FTR_HVMODE)) {
0304         if (irqs_max > afu->adapter->user_irqs)
0305             return -EINVAL;
0306     } else {
0307         /* pHyp sets a per-AFU limit */
0308         if (irqs_max > afu->guest->max_ints)
0309             return -EINVAL;
0310     }
0311 
0312     afu->irqs_max = irqs_max;
0313     return count;
0314 }
0315 
0316 static ssize_t modes_supported_show(struct device *device,
0317                     struct device_attribute *attr, char *buf)
0318 {
0319     struct cxl_afu *afu = to_cxl_afu(device);
0320     char *p = buf, *end = buf + PAGE_SIZE;
0321 
0322     if (afu->modes_supported & CXL_MODE_DEDICATED)
0323         p += scnprintf(p, end - p, "dedicated_process\n");
0324     if (afu->modes_supported & CXL_MODE_DIRECTED)
0325         p += scnprintf(p, end - p, "afu_directed\n");
0326     return (p - buf);
0327 }
0328 
0329 static ssize_t prefault_mode_show(struct device *device,
0330                   struct device_attribute *attr,
0331                   char *buf)
0332 {
0333     struct cxl_afu *afu = to_cxl_afu(device);
0334 
0335     switch (afu->prefault_mode) {
0336     case CXL_PREFAULT_WED:
0337         return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
0338     case CXL_PREFAULT_ALL:
0339         return scnprintf(buf, PAGE_SIZE, "all\n");
0340     default:
0341         return scnprintf(buf, PAGE_SIZE, "none\n");
0342     }
0343 }
0344 
0345 static ssize_t prefault_mode_store(struct device *device,
0346               struct device_attribute *attr,
0347               const char *buf, size_t count)
0348 {
0349     struct cxl_afu *afu = to_cxl_afu(device);
0350     enum prefault_modes mode = -1;
0351 
0352     if (!strncmp(buf, "none", 4))
0353         mode = CXL_PREFAULT_NONE;
0354     else {
0355         if (!radix_enabled()) {
0356 
0357             /* only allowed when not in radix mode */
0358             if (!strncmp(buf, "work_element_descriptor", 23))
0359                 mode = CXL_PREFAULT_WED;
0360             if (!strncmp(buf, "all", 3))
0361                 mode = CXL_PREFAULT_ALL;
0362         } else {
0363             dev_err(device, "Cannot prefault with radix enabled\n");
0364         }
0365     }
0366 
0367     if (mode == -1)
0368         return -EINVAL;
0369 
0370     afu->prefault_mode = mode;
0371     return count;
0372 }
0373 
0374 static ssize_t mode_show(struct device *device,
0375              struct device_attribute *attr,
0376              char *buf)
0377 {
0378     struct cxl_afu *afu = to_cxl_afu(device);
0379 
0380     if (afu->current_mode == CXL_MODE_DEDICATED)
0381         return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
0382     if (afu->current_mode == CXL_MODE_DIRECTED)
0383         return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
0384     return scnprintf(buf, PAGE_SIZE, "none\n");
0385 }
0386 
0387 static ssize_t mode_store(struct device *device, struct device_attribute *attr,
0388               const char *buf, size_t count)
0389 {
0390     struct cxl_afu *afu = to_cxl_afu(device);
0391     int old_mode, mode = -1;
0392     int rc = -EBUSY;
0393 
0394     /* can't change this if we have a user */
0395     mutex_lock(&afu->contexts_lock);
0396     if (!idr_is_empty(&afu->contexts_idr))
0397         goto err;
0398 
0399     if (!strncmp(buf, "dedicated_process", 17))
0400         mode = CXL_MODE_DEDICATED;
0401     if (!strncmp(buf, "afu_directed", 12))
0402         mode = CXL_MODE_DIRECTED;
0403     if (!strncmp(buf, "none", 4))
0404         mode = 0;
0405 
0406     if (mode == -1) {
0407         rc = -EINVAL;
0408         goto err;
0409     }
0410 
0411     /*
0412      * afu_deactivate_mode needs to be done outside the lock, prevent
0413      * other contexts coming in before we are ready:
0414      */
0415     old_mode = afu->current_mode;
0416     afu->current_mode = 0;
0417     afu->num_procs = 0;
0418 
0419     mutex_unlock(&afu->contexts_lock);
0420 
0421     if ((rc = cxl_ops->afu_deactivate_mode(afu, old_mode)))
0422         return rc;
0423     if ((rc = cxl_ops->afu_activate_mode(afu, mode)))
0424         return rc;
0425 
0426     return count;
0427 err:
0428     mutex_unlock(&afu->contexts_lock);
0429     return rc;
0430 }
0431 
0432 static ssize_t api_version_show(struct device *device,
0433                 struct device_attribute *attr,
0434                 char *buf)
0435 {
0436     return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
0437 }
0438 
0439 static ssize_t api_version_compatible_show(struct device *device,
0440                        struct device_attribute *attr,
0441                        char *buf)
0442 {
0443     return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
0444 }
0445 
0446 static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
0447                    struct bin_attribute *bin_attr, char *buf,
0448                    loff_t off, size_t count)
0449 {
0450     struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj));
0451 
0452     return cxl_ops->afu_read_err_buffer(afu, buf, off, count);
0453 }
0454 
0455 static struct device_attribute afu_attrs[] = {
0456     __ATTR_RO(mmio_size),
0457     __ATTR_RO(irqs_min),
0458     __ATTR_RW(irqs_max),
0459     __ATTR_RO(modes_supported),
0460     __ATTR_RW(mode),
0461     __ATTR_RW(prefault_mode),
0462     __ATTR_RO(api_version),
0463     __ATTR_RO(api_version_compatible),
0464     __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
0465 };
0466 
0467 int cxl_sysfs_adapter_add(struct cxl *adapter)
0468 {
0469     struct device_attribute *dev_attr;
0470     int i, rc;
0471 
0472     for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
0473         dev_attr = &adapter_attrs[i];
0474         if (cxl_ops->support_attributes(dev_attr->attr.name,
0475                         CXL_ADAPTER_ATTRS)) {
0476             if ((rc = device_create_file(&adapter->dev, dev_attr)))
0477                 goto err;
0478         }
0479     }
0480     return 0;
0481 err:
0482     for (i--; i >= 0; i--) {
0483         dev_attr = &adapter_attrs[i];
0484         if (cxl_ops->support_attributes(dev_attr->attr.name,
0485                         CXL_ADAPTER_ATTRS))
0486             device_remove_file(&adapter->dev, dev_attr);
0487     }
0488     return rc;
0489 }
0490 
0491 void cxl_sysfs_adapter_remove(struct cxl *adapter)
0492 {
0493     struct device_attribute *dev_attr;
0494     int i;
0495 
0496     for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
0497         dev_attr = &adapter_attrs[i];
0498         if (cxl_ops->support_attributes(dev_attr->attr.name,
0499                         CXL_ADAPTER_ATTRS))
0500             device_remove_file(&adapter->dev, dev_attr);
0501     }
0502 }
0503 
0504 struct afu_config_record {
0505     struct kobject kobj;
0506     struct bin_attribute config_attr;
0507     struct list_head list;
0508     int cr;
0509     u16 device;
0510     u16 vendor;
0511     u32 class;
0512 };
0513 
0514 #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
0515 
0516 static ssize_t vendor_show(struct kobject *kobj,
0517                struct kobj_attribute *attr, char *buf)
0518 {
0519     struct afu_config_record *cr = to_cr(kobj);
0520 
0521     return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
0522 }
0523 
0524 static ssize_t device_show(struct kobject *kobj,
0525                struct kobj_attribute *attr, char *buf)
0526 {
0527     struct afu_config_record *cr = to_cr(kobj);
0528 
0529     return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
0530 }
0531 
0532 static ssize_t class_show(struct kobject *kobj,
0533               struct kobj_attribute *attr, char *buf)
0534 {
0535     struct afu_config_record *cr = to_cr(kobj);
0536 
0537     return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
0538 }
0539 
0540 static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
0541                    struct bin_attribute *bin_attr, char *buf,
0542                    loff_t off, size_t count)
0543 {
0544     struct afu_config_record *cr = to_cr(kobj);
0545     struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj->parent));
0546 
0547     u64 i, j, val, rc;
0548 
0549     for (i = 0; i < count;) {
0550         rc = cxl_ops->afu_cr_read64(afu, cr->cr, off & ~0x7, &val);
0551         if (rc)
0552             val = ~0ULL;
0553         for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
0554             buf[i] = (val >> (j * 8)) & 0xff;
0555     }
0556 
0557     return count;
0558 }
0559 
0560 static struct kobj_attribute vendor_attribute =
0561     __ATTR_RO(vendor);
0562 static struct kobj_attribute device_attribute =
0563     __ATTR_RO(device);
0564 static struct kobj_attribute class_attribute =
0565     __ATTR_RO(class);
0566 
0567 static struct attribute *afu_cr_attrs[] = {
0568     &vendor_attribute.attr,
0569     &device_attribute.attr,
0570     &class_attribute.attr,
0571     NULL,
0572 };
0573 ATTRIBUTE_GROUPS(afu_cr);
0574 
0575 static void release_afu_config_record(struct kobject *kobj)
0576 {
0577     struct afu_config_record *cr = to_cr(kobj);
0578 
0579     kfree(cr);
0580 }
0581 
0582 static struct kobj_type afu_config_record_type = {
0583     .sysfs_ops = &kobj_sysfs_ops,
0584     .release = release_afu_config_record,
0585     .default_groups = afu_cr_groups,
0586 };
0587 
0588 static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
0589 {
0590     struct afu_config_record *cr;
0591     int rc;
0592 
0593     cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
0594     if (!cr)
0595         return ERR_PTR(-ENOMEM);
0596 
0597     cr->cr = cr_idx;
0598 
0599     rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID, &cr->device);
0600     if (rc)
0601         goto err;
0602     rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID, &cr->vendor);
0603     if (rc)
0604         goto err;
0605     rc = cxl_ops->afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION, &cr->class);
0606     if (rc)
0607         goto err;
0608     cr->class >>= 8;
0609 
0610     /*
0611      * Export raw AFU PCIe like config record. For now this is read only by
0612      * root - we can expand that later to be readable by non-root and maybe
0613      * even writable provided we have a good use-case. Once we support
0614      * exposing AFUs through a virtual PHB they will get that for free from
0615      * Linux' PCI infrastructure, but until then it's not clear that we
0616      * need it for anything since the main use case is just identifying
0617      * AFUs, which can be done via the vendor, device and class attributes.
0618      */
0619     sysfs_bin_attr_init(&cr->config_attr);
0620     cr->config_attr.attr.name = "config";
0621     cr->config_attr.attr.mode = S_IRUSR;
0622     cr->config_attr.size = afu->crs_len;
0623     cr->config_attr.read = afu_read_config;
0624 
0625     rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
0626                   &afu->dev.kobj, "cr%i", cr->cr);
0627     if (rc)
0628         goto err1;
0629 
0630     rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
0631     if (rc)
0632         goto err1;
0633 
0634     rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
0635     if (rc)
0636         goto err2;
0637 
0638     return cr;
0639 err2:
0640     sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
0641 err1:
0642     kobject_put(&cr->kobj);
0643     return ERR_PTR(rc);
0644 err:
0645     kfree(cr);
0646     return ERR_PTR(rc);
0647 }
0648 
0649 void cxl_sysfs_afu_remove(struct cxl_afu *afu)
0650 {
0651     struct device_attribute *dev_attr;
0652     struct afu_config_record *cr, *tmp;
0653     int i;
0654 
0655     /* remove the err buffer bin attribute */
0656     if (afu->eb_len)
0657         device_remove_bin_file(&afu->dev, &afu->attr_eb);
0658 
0659     for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
0660         dev_attr = &afu_attrs[i];
0661         if (cxl_ops->support_attributes(dev_attr->attr.name,
0662                         CXL_AFU_ATTRS))
0663             device_remove_file(&afu->dev, &afu_attrs[i]);
0664     }
0665 
0666     list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
0667         sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
0668         kobject_put(&cr->kobj);
0669     }
0670 }
0671 
0672 int cxl_sysfs_afu_add(struct cxl_afu *afu)
0673 {
0674     struct device_attribute *dev_attr;
0675     struct afu_config_record *cr;
0676     int i, rc;
0677 
0678     INIT_LIST_HEAD(&afu->crs);
0679 
0680     for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
0681         dev_attr = &afu_attrs[i];
0682         if (cxl_ops->support_attributes(dev_attr->attr.name,
0683                         CXL_AFU_ATTRS)) {
0684             if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
0685                 goto err;
0686         }
0687     }
0688 
0689     /* conditionally create the add the binary file for error info buffer */
0690     if (afu->eb_len) {
0691         sysfs_attr_init(&afu->attr_eb.attr);
0692 
0693         afu->attr_eb.attr.name = "afu_err_buff";
0694         afu->attr_eb.attr.mode = S_IRUGO;
0695         afu->attr_eb.size = afu->eb_len;
0696         afu->attr_eb.read = afu_eb_read;
0697 
0698         rc = device_create_bin_file(&afu->dev, &afu->attr_eb);
0699         if (rc) {
0700             dev_err(&afu->dev,
0701                 "Unable to create eb attr for the afu. Err(%d)\n",
0702                 rc);
0703             goto err;
0704         }
0705     }
0706 
0707     for (i = 0; i < afu->crs_num; i++) {
0708         cr = cxl_sysfs_afu_new_cr(afu, i);
0709         if (IS_ERR(cr)) {
0710             rc = PTR_ERR(cr);
0711             goto err1;
0712         }
0713         list_add(&cr->list, &afu->crs);
0714     }
0715 
0716     return 0;
0717 
0718 err1:
0719     cxl_sysfs_afu_remove(afu);
0720     return rc;
0721 err:
0722     /* reset the eb_len as we havent created the bin attr */
0723     afu->eb_len = 0;
0724 
0725     for (i--; i >= 0; i--) {
0726         dev_attr = &afu_attrs[i];
0727         if (cxl_ops->support_attributes(dev_attr->attr.name,
0728                         CXL_AFU_ATTRS))
0729         device_remove_file(&afu->dev, &afu_attrs[i]);
0730     }
0731     return rc;
0732 }
0733 
0734 int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
0735 {
0736     struct device_attribute *dev_attr;
0737     int i, rc;
0738 
0739     for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
0740         dev_attr = &afu_master_attrs[i];
0741         if (cxl_ops->support_attributes(dev_attr->attr.name,
0742                         CXL_AFU_MASTER_ATTRS)) {
0743             if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
0744                 goto err;
0745         }
0746     }
0747 
0748     return 0;
0749 
0750 err:
0751     for (i--; i >= 0; i--) {
0752         dev_attr = &afu_master_attrs[i];
0753         if (cxl_ops->support_attributes(dev_attr->attr.name,
0754                         CXL_AFU_MASTER_ATTRS))
0755             device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
0756     }
0757     return rc;
0758 }
0759 
0760 void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
0761 {
0762     struct device_attribute *dev_attr;
0763     int i;
0764 
0765     for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
0766         dev_attr = &afu_master_attrs[i];
0767         if (cxl_ops->support_attributes(dev_attr->attr.name,
0768                         CXL_AFU_MASTER_ATTRS))
0769             device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
0770     }
0771 }