Merge tag 'cxl-for-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 9 Sep 2021 18:48:27 +0000 (11:48 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 9 Sep 2021 18:48:27 +0000 (11:48 -0700)
Pull CXL (Compute Express Link) updates from Dan Williams:

 - Fix detection of CXL host bridges to filter out disabled ACPI0016
   devices in the ACPI DSDT.

 - Fix kernel lockdown integration to disable raw commands when raw PCI
   access is disabled.

 - Fix a broken debug message.

 - Add support for "Get Partition Info". I.e. enumerate the split
   between volatile and persistent capacity on bi-modal CXL memory
   expanders.

 - Re-factor the core by subject area. This is a work in progress.

 - Prepare libnvdimm to understand CXL labels in addition to EFI labels.
   This is a work in progress.

* tag 'cxl-for-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl: (25 commits)
  cxl/registers: Fix Documentation warning
  cxl/pmem: Fix Documentation warning
  cxl/uapi: Fix defined but not used warnings
  cxl/pci: Fix debug message in cxl_probe_regs()
  cxl/pci: Fix lockdown level
  cxl/acpi: Do not add DSDT disabled ACPI0016 host bridge ports
  libnvdimm/labels: Add claim class helpers
  libnvdimm/labels: Add type-guid helpers
  libnvdimm/labels: Add blk special cases for nlabel and position helpers
  libnvdimm/labels: Add blk isetcookie set / validation helpers
  libnvdimm/labels: Add a checksum calculation helper
  libnvdimm/labels: Introduce label setter helpers
  libnvdimm/labels: Add isetcookie validation helper
  libnvdimm/labels: Introduce getters for namespace label fields
  cxl/mem: Adjust ram/pmem range to represent DPA ranges
  cxl/mem: Account for partitionable space in ram/pmem ranges
  cxl/pci: Store memory capacity values
  cxl/pci: Simplify register setup
  cxl/pci: Ignore unknown register block types
  cxl/core: Move memdev management to core
  ...

1  2 
drivers/cxl/core/bus.c
drivers/nvdimm/namespace_devs.c

diff --combined drivers/cxl/core/bus.c
index 0000000,37b87ad..267d804
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,661 +1,660 @@@
 -static int cxl_bus_remove(struct device *dev)
+ // SPDX-License-Identifier: GPL-2.0-only
+ /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
+ #include <linux/io-64-nonatomic-lo-hi.h>
+ #include <linux/device.h>
+ #include <linux/module.h>
+ #include <linux/pci.h>
+ #include <linux/slab.h>
+ #include <linux/idr.h>
+ #include <cxlmem.h>
+ #include <cxl.h>
+ #include "core.h"
+ /**
+  * DOC: cxl core
+  *
+  * The CXL core provides a set of interfaces that can be consumed by CXL aware
+  * drivers. The interfaces allow for creation, modification, and destruction of
+  * regions, memory devices, ports, and decoders. CXL aware drivers must register
+  * with the CXL core via these interfaces in order to be able to participate in
+  * cross-device interleave coordination. The CXL core also establishes and
+  * maintains the bridge to the nvdimm subsystem.
+  *
+  * CXL core introduces sysfs hierarchy to control the devices that are
+  * instantiated by the core.
+  */
+ static DEFINE_IDA(cxl_port_ida);
+ static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
+                           char *buf)
+ {
+       return sysfs_emit(buf, "%s\n", dev->type->name);
+ }
+ static DEVICE_ATTR_RO(devtype);
+ static struct attribute *cxl_base_attributes[] = {
+       &dev_attr_devtype.attr,
+       NULL,
+ };
+ struct attribute_group cxl_base_attribute_group = {
+       .attrs = cxl_base_attributes,
+ };
+ static ssize_t start_show(struct device *dev, struct device_attribute *attr,
+                         char *buf)
+ {
+       struct cxl_decoder *cxld = to_cxl_decoder(dev);
+       return sysfs_emit(buf, "%#llx\n", cxld->range.start);
+ }
+ static DEVICE_ATTR_RO(start);
+ static ssize_t size_show(struct device *dev, struct device_attribute *attr,
+                       char *buf)
+ {
+       struct cxl_decoder *cxld = to_cxl_decoder(dev);
+       return sysfs_emit(buf, "%#llx\n", range_len(&cxld->range));
+ }
+ static DEVICE_ATTR_RO(size);
+ #define CXL_DECODER_FLAG_ATTR(name, flag)                            \
+ static ssize_t name##_show(struct device *dev,                       \
+                          struct device_attribute *attr, char *buf) \
+ {                                                                    \
+       struct cxl_decoder *cxld = to_cxl_decoder(dev);              \
+                                                                      \
+       return sysfs_emit(buf, "%s\n",                               \
+                         (cxld->flags & (flag)) ? "1" : "0");       \
+ }                                                                    \
+ static DEVICE_ATTR_RO(name)
+ CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM);
+ CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM);
+ CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2);
+ CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3);
+ CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK);
+ static ssize_t target_type_show(struct device *dev,
+                               struct device_attribute *attr, char *buf)
+ {
+       struct cxl_decoder *cxld = to_cxl_decoder(dev);
+       switch (cxld->target_type) {
+       case CXL_DECODER_ACCELERATOR:
+               return sysfs_emit(buf, "accelerator\n");
+       case CXL_DECODER_EXPANDER:
+               return sysfs_emit(buf, "expander\n");
+       }
+       return -ENXIO;
+ }
+ static DEVICE_ATTR_RO(target_type);
+ static ssize_t target_list_show(struct device *dev,
+                              struct device_attribute *attr, char *buf)
+ {
+       struct cxl_decoder *cxld = to_cxl_decoder(dev);
+       ssize_t offset = 0;
+       int i, rc = 0;
+       device_lock(dev);
+       for (i = 0; i < cxld->interleave_ways; i++) {
+               struct cxl_dport *dport = cxld->target[i];
+               struct cxl_dport *next = NULL;
+               if (!dport)
+                       break;
+               if (i + 1 < cxld->interleave_ways)
+                       next = cxld->target[i + 1];
+               rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id,
+                                  next ? "," : "");
+               if (rc < 0)
+                       break;
+               offset += rc;
+       }
+       device_unlock(dev);
+       if (rc < 0)
+               return rc;
+       rc = sysfs_emit_at(buf, offset, "\n");
+       if (rc < 0)
+               return rc;
+       return offset + rc;
+ }
+ static DEVICE_ATTR_RO(target_list);
+ static struct attribute *cxl_decoder_base_attrs[] = {
+       &dev_attr_start.attr,
+       &dev_attr_size.attr,
+       &dev_attr_locked.attr,
+       &dev_attr_target_list.attr,
+       NULL,
+ };
+ static struct attribute_group cxl_decoder_base_attribute_group = {
+       .attrs = cxl_decoder_base_attrs,
+ };
+ static struct attribute *cxl_decoder_root_attrs[] = {
+       &dev_attr_cap_pmem.attr,
+       &dev_attr_cap_ram.attr,
+       &dev_attr_cap_type2.attr,
+       &dev_attr_cap_type3.attr,
+       NULL,
+ };
+ static struct attribute_group cxl_decoder_root_attribute_group = {
+       .attrs = cxl_decoder_root_attrs,
+ };
+ static const struct attribute_group *cxl_decoder_root_attribute_groups[] = {
+       &cxl_decoder_root_attribute_group,
+       &cxl_decoder_base_attribute_group,
+       &cxl_base_attribute_group,
+       NULL,
+ };
+ static struct attribute *cxl_decoder_switch_attrs[] = {
+       &dev_attr_target_type.attr,
+       NULL,
+ };
+ static struct attribute_group cxl_decoder_switch_attribute_group = {
+       .attrs = cxl_decoder_switch_attrs,
+ };
+ static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
+       &cxl_decoder_switch_attribute_group,
+       &cxl_decoder_base_attribute_group,
+       &cxl_base_attribute_group,
+       NULL,
+ };
+ static void cxl_decoder_release(struct device *dev)
+ {
+       struct cxl_decoder *cxld = to_cxl_decoder(dev);
+       struct cxl_port *port = to_cxl_port(dev->parent);
+       ida_free(&port->decoder_ida, cxld->id);
+       kfree(cxld);
+ }
+ static const struct device_type cxl_decoder_switch_type = {
+       .name = "cxl_decoder_switch",
+       .release = cxl_decoder_release,
+       .groups = cxl_decoder_switch_attribute_groups,
+ };
+ static const struct device_type cxl_decoder_root_type = {
+       .name = "cxl_decoder_root",
+       .release = cxl_decoder_release,
+       .groups = cxl_decoder_root_attribute_groups,
+ };
+ bool is_root_decoder(struct device *dev)
+ {
+       return dev->type == &cxl_decoder_root_type;
+ }
+ EXPORT_SYMBOL_GPL(is_root_decoder);
+ struct cxl_decoder *to_cxl_decoder(struct device *dev)
+ {
+       if (dev_WARN_ONCE(dev, dev->type->release != cxl_decoder_release,
+                         "not a cxl_decoder device\n"))
+               return NULL;
+       return container_of(dev, struct cxl_decoder, dev);
+ }
+ EXPORT_SYMBOL_GPL(to_cxl_decoder);
+ static void cxl_dport_release(struct cxl_dport *dport)
+ {
+       list_del(&dport->list);
+       put_device(dport->dport);
+       kfree(dport);
+ }
+ static void cxl_port_release(struct device *dev)
+ {
+       struct cxl_port *port = to_cxl_port(dev);
+       struct cxl_dport *dport, *_d;
+       device_lock(dev);
+       list_for_each_entry_safe(dport, _d, &port->dports, list)
+               cxl_dport_release(dport);
+       device_unlock(dev);
+       ida_free(&cxl_port_ida, port->id);
+       kfree(port);
+ }
+ static const struct attribute_group *cxl_port_attribute_groups[] = {
+       &cxl_base_attribute_group,
+       NULL,
+ };
+ static const struct device_type cxl_port_type = {
+       .name = "cxl_port",
+       .release = cxl_port_release,
+       .groups = cxl_port_attribute_groups,
+ };
+ struct cxl_port *to_cxl_port(struct device *dev)
+ {
+       if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type,
+                         "not a cxl_port device\n"))
+               return NULL;
+       return container_of(dev, struct cxl_port, dev);
+ }
+ static void unregister_port(void *_port)
+ {
+       struct cxl_port *port = _port;
+       struct cxl_dport *dport;
+       device_lock(&port->dev);
+       list_for_each_entry(dport, &port->dports, list) {
+               char link_name[CXL_TARGET_STRLEN];
+               if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d",
+                            dport->port_id) >= CXL_TARGET_STRLEN)
+                       continue;
+               sysfs_remove_link(&port->dev.kobj, link_name);
+       }
+       device_unlock(&port->dev);
+       device_unregister(&port->dev);
+ }
+ static void cxl_unlink_uport(void *_port)
+ {
+       struct cxl_port *port = _port;
+       sysfs_remove_link(&port->dev.kobj, "uport");
+ }
+ static int devm_cxl_link_uport(struct device *host, struct cxl_port *port)
+ {
+       int rc;
+       rc = sysfs_create_link(&port->dev.kobj, &port->uport->kobj, "uport");
+       if (rc)
+               return rc;
+       return devm_add_action_or_reset(host, cxl_unlink_uport, port);
+ }
+ static struct cxl_port *cxl_port_alloc(struct device *uport,
+                                      resource_size_t component_reg_phys,
+                                      struct cxl_port *parent_port)
+ {
+       struct cxl_port *port;
+       struct device *dev;
+       int rc;
+       port = kzalloc(sizeof(*port), GFP_KERNEL);
+       if (!port)
+               return ERR_PTR(-ENOMEM);
+       rc = ida_alloc(&cxl_port_ida, GFP_KERNEL);
+       if (rc < 0)
+               goto err;
+       port->id = rc;
+       /*
+        * The top-level cxl_port "cxl_root" does not have a cxl_port as
+        * its parent and it does not have any corresponding component
+        * registers as its decode is described by a fixed platform
+        * description.
+        */
+       dev = &port->dev;
+       if (parent_port)
+               dev->parent = &parent_port->dev;
+       else
+               dev->parent = uport;
+       port->uport = uport;
+       port->component_reg_phys = component_reg_phys;
+       ida_init(&port->decoder_ida);
+       INIT_LIST_HEAD(&port->dports);
+       device_initialize(dev);
+       device_set_pm_not_required(dev);
+       dev->bus = &cxl_bus_type;
+       dev->type = &cxl_port_type;
+       return port;
+ err:
+       kfree(port);
+       return ERR_PTR(rc);
+ }
+ /**
+  * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
+  * @host: host device for devm operations
+  * @uport: "physical" device implementing this upstream port
+  * @component_reg_phys: (optional) for configurable cxl_port instances
+  * @parent_port: next hop up in the CXL memory decode hierarchy
+  */
+ struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
+                                  resource_size_t component_reg_phys,
+                                  struct cxl_port *parent_port)
+ {
+       struct cxl_port *port;
+       struct device *dev;
+       int rc;
+       port = cxl_port_alloc(uport, component_reg_phys, parent_port);
+       if (IS_ERR(port))
+               return port;
+       dev = &port->dev;
+       if (parent_port)
+               rc = dev_set_name(dev, "port%d", port->id);
+       else
+               rc = dev_set_name(dev, "root%d", port->id);
+       if (rc)
+               goto err;
+       rc = device_add(dev);
+       if (rc)
+               goto err;
+       rc = devm_add_action_or_reset(host, unregister_port, port);
+       if (rc)
+               return ERR_PTR(rc);
+       rc = devm_cxl_link_uport(host, port);
+       if (rc)
+               return ERR_PTR(rc);
+       return port;
+ err:
+       put_device(dev);
+       return ERR_PTR(rc);
+ }
+ EXPORT_SYMBOL_GPL(devm_cxl_add_port);
+ static struct cxl_dport *find_dport(struct cxl_port *port, int id)
+ {
+       struct cxl_dport *dport;
+       device_lock_assert(&port->dev);
+       list_for_each_entry (dport, &port->dports, list)
+               if (dport->port_id == id)
+                       return dport;
+       return NULL;
+ }
+ static int add_dport(struct cxl_port *port, struct cxl_dport *new)
+ {
+       struct cxl_dport *dup;
+       device_lock(&port->dev);
+       dup = find_dport(port, new->port_id);
+       if (dup)
+               dev_err(&port->dev,
+                       "unable to add dport%d-%s non-unique port id (%s)\n",
+                       new->port_id, dev_name(new->dport),
+                       dev_name(dup->dport));
+       else
+               list_add_tail(&new->list, &port->dports);
+       device_unlock(&port->dev);
+       return dup ? -EEXIST : 0;
+ }
+ /**
+  * cxl_add_dport - append downstream port data to a cxl_port
+  * @port: the cxl_port that references this dport
+  * @dport_dev: firmware or PCI device representing the dport
+  * @port_id: identifier for this dport in a decoder's target list
+  * @component_reg_phys: optional location of CXL component registers
+  *
+  * Note that all allocations and links are undone by cxl_port deletion
+  * and release.
+  */
+ int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
+                 resource_size_t component_reg_phys)
+ {
+       char link_name[CXL_TARGET_STRLEN];
+       struct cxl_dport *dport;
+       int rc;
+       if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >=
+           CXL_TARGET_STRLEN)
+               return -EINVAL;
+       dport = kzalloc(sizeof(*dport), GFP_KERNEL);
+       if (!dport)
+               return -ENOMEM;
+       INIT_LIST_HEAD(&dport->list);
+       dport->dport = get_device(dport_dev);
+       dport->port_id = port_id;
+       dport->component_reg_phys = component_reg_phys;
+       dport->port = port;
+       rc = add_dport(port, dport);
+       if (rc)
+               goto err;
+       rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name);
+       if (rc)
+               goto err;
+       return 0;
+ err:
+       cxl_dport_release(dport);
+       return rc;
+ }
+ EXPORT_SYMBOL_GPL(cxl_add_dport);
+ static struct cxl_decoder *
+ cxl_decoder_alloc(struct cxl_port *port, int nr_targets, resource_size_t base,
+                 resource_size_t len, int interleave_ways,
+                 int interleave_granularity, enum cxl_decoder_type type,
+                 unsigned long flags)
+ {
+       struct cxl_decoder *cxld;
+       struct device *dev;
+       int rc = 0;
+       if (interleave_ways < 1)
+               return ERR_PTR(-EINVAL);
+       device_lock(&port->dev);
+       if (list_empty(&port->dports))
+               rc = -EINVAL;
+       device_unlock(&port->dev);
+       if (rc)
+               return ERR_PTR(rc);
+       cxld = kzalloc(struct_size(cxld, target, nr_targets), GFP_KERNEL);
+       if (!cxld)
+               return ERR_PTR(-ENOMEM);
+       rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
+       if (rc < 0)
+               goto err;
+       *cxld = (struct cxl_decoder) {
+               .id = rc,
+               .range = {
+                       .start = base,
+                       .end = base + len - 1,
+               },
+               .flags = flags,
+               .interleave_ways = interleave_ways,
+               .interleave_granularity = interleave_granularity,
+               .target_type = type,
+       };
+       /* handle implied target_list */
+       if (interleave_ways == 1)
+               cxld->target[0] =
+                       list_first_entry(&port->dports, struct cxl_dport, list);
+       dev = &cxld->dev;
+       device_initialize(dev);
+       device_set_pm_not_required(dev);
+       dev->parent = &port->dev;
+       dev->bus = &cxl_bus_type;
+       /* root ports do not have a cxl_port_type parent */
+       if (port->dev.parent->type == &cxl_port_type)
+               dev->type = &cxl_decoder_switch_type;
+       else
+               dev->type = &cxl_decoder_root_type;
+       return cxld;
+ err:
+       kfree(cxld);
+       return ERR_PTR(rc);
+ }
+ struct cxl_decoder *
+ devm_cxl_add_decoder(struct device *host, struct cxl_port *port, int nr_targets,
+                    resource_size_t base, resource_size_t len,
+                    int interleave_ways, int interleave_granularity,
+                    enum cxl_decoder_type type, unsigned long flags)
+ {
+       struct cxl_decoder *cxld;
+       struct device *dev;
+       int rc;
+       cxld = cxl_decoder_alloc(port, nr_targets, base, len, interleave_ways,
+                                interleave_granularity, type, flags);
+       if (IS_ERR(cxld))
+               return cxld;
+       dev = &cxld->dev;
+       rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
+       if (rc)
+               goto err;
+       rc = device_add(dev);
+       if (rc)
+               goto err;
+       rc = devm_add_action_or_reset(host, unregister_cxl_dev, dev);
+       if (rc)
+               return ERR_PTR(rc);
+       return cxld;
+ err:
+       put_device(dev);
+       return ERR_PTR(rc);
+ }
+ EXPORT_SYMBOL_GPL(devm_cxl_add_decoder);
+ /**
+  * __cxl_driver_register - register a driver for the cxl bus
+  * @cxl_drv: cxl driver structure to attach
+  * @owner: owning module/driver
+  * @modname: KBUILD_MODNAME for parent driver
+  */
+ int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
+                         const char *modname)
+ {
+       if (!cxl_drv->probe) {
+               pr_debug("%s ->probe() must be specified\n", modname);
+               return -EINVAL;
+       }
+       if (!cxl_drv->name) {
+               pr_debug("%s ->name must be specified\n", modname);
+               return -EINVAL;
+       }
+       if (!cxl_drv->id) {
+               pr_debug("%s ->id must be specified\n", modname);
+               return -EINVAL;
+       }
+       cxl_drv->drv.bus = &cxl_bus_type;
+       cxl_drv->drv.owner = owner;
+       cxl_drv->drv.mod_name = modname;
+       cxl_drv->drv.name = cxl_drv->name;
+       return driver_register(&cxl_drv->drv);
+ }
+ EXPORT_SYMBOL_GPL(__cxl_driver_register);
+ void cxl_driver_unregister(struct cxl_driver *cxl_drv)
+ {
+       driver_unregister(&cxl_drv->drv);
+ }
+ EXPORT_SYMBOL_GPL(cxl_driver_unregister);
+ static int cxl_device_id(struct device *dev)
+ {
+       if (dev->type == &cxl_nvdimm_bridge_type)
+               return CXL_DEVICE_NVDIMM_BRIDGE;
+       if (dev->type == &cxl_nvdimm_type)
+               return CXL_DEVICE_NVDIMM;
+       return 0;
+ }
+ static int cxl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
+ {
+       return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
+                             cxl_device_id(dev));
+ }
+ static int cxl_bus_match(struct device *dev, struct device_driver *drv)
+ {
+       return cxl_device_id(dev) == to_cxl_drv(drv)->id;
+ }
+ static int cxl_bus_probe(struct device *dev)
+ {
+       return to_cxl_drv(dev->driver)->probe(dev);
+ }
 -      return 0;
++static void cxl_bus_remove(struct device *dev)
+ {
+       struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
+       if (cxl_drv->remove)
+               cxl_drv->remove(dev);
+ }
+ struct bus_type cxl_bus_type = {
+       .name = "cxl",
+       .uevent = cxl_bus_uevent,
+       .match = cxl_bus_match,
+       .probe = cxl_bus_probe,
+       .remove = cxl_bus_remove,
+ };
+ EXPORT_SYMBOL_GPL(cxl_bus_type);
+ static __init int cxl_core_init(void)
+ {
+       int rc;
+       rc = cxl_memdev_init();
+       if (rc)
+               return rc;
+       rc = bus_register(&cxl_bus_type);
+       if (rc)
+               goto err;
+       return 0;
+ err:
+       cxl_memdev_exit();
+       return rc;
+ }
+ static void cxl_core_exit(void)
+ {
+       bus_unregister(&cxl_bus_type);
+       cxl_memdev_exit();
+ }
+ module_init(cxl_core_init);
+ module_exit(cxl_core_exit);
+ MODULE_LICENSE("GPL v2");
@@@ -1235,7 -1235,7 +1235,7 @@@ static int namespace_update_uuid(struc
                        if (!nd_label)
                                continue;
                        nd_label_gen_id(&label_id, nd_label->uuid,
-                                       __le32_to_cpu(nd_label->flags));
+                                       nsl_get_flags(ndd, nd_label));
                        if (strcmp(old_label_id.id, label_id.id) == 0)
                                set_bit(ND_LABEL_REAP, &label_ent->flags);
                }
@@@ -1847,28 -1847,21 +1847,21 @@@ static bool has_uuid_at_pos(struct nd_r
                list_for_each_entry(label_ent, &nd_mapping->labels, list) {
                        struct nd_namespace_label *nd_label = label_ent->label;
                        u16 position, nlabel;
-                       u64 isetcookie;
  
                        if (!nd_label)
                                continue;
-                       isetcookie = __le64_to_cpu(nd_label->isetcookie);
-                       position = __le16_to_cpu(nd_label->position);
-                       nlabel = __le16_to_cpu(nd_label->nlabel);
+                       position = nsl_get_position(ndd, nd_label);
+                       nlabel = nsl_get_nlabel(ndd, nd_label);
  
-                       if (isetcookie != cookie)
+                       if (!nsl_validate_isetcookie(ndd, nd_label, cookie))
                                continue;
  
                        if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
                                continue;
  
-                       if (namespace_label_has(ndd, type_guid)
-                                       && !guid_equal(&nd_set->type_guid,
-                                               &nd_label->type_guid)) {
-                               dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
-                                               &nd_set->type_guid,
-                                               &nd_label->type_guid);
+                       if (!nsl_validate_type_guid(ndd, nd_label,
+                                                   &nd_set->type_guid))
                                continue;
-                       }
  
                        if (found_uuid) {
                                dev_dbg(ndd->dev, "duplicate entry for uuid\n");
@@@ -1923,8 -1916,8 +1916,8 @@@ static int select_pmem_id(struct nd_reg
                 */
                hw_start = nd_mapping->start;
                hw_end = hw_start + nd_mapping->size;
-               pmem_start = __le64_to_cpu(nd_label->dpa);
-               pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
+               pmem_start = nsl_get_dpa(ndd, nd_label);
+               pmem_end = pmem_start + nsl_get_rawsize(ndd, nd_label);
                if (pmem_start >= hw_start && pmem_start < hw_end
                                && pmem_end <= hw_end && pmem_end > hw_start)
                        /* pass */;
   * @nd_label: target pmem namespace label to evaluate
   */
  static struct device *create_namespace_pmem(struct nd_region *nd_region,
-               struct nd_namespace_index *nsindex,
-               struct nd_namespace_label *nd_label)
+                                           struct nd_mapping *nd_mapping,
+                                           struct nd_namespace_label *nd_label)
  {
+       struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
+       struct nd_namespace_index *nsindex =
+               to_namespace_index(ndd, ndd->ns_current);
        u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
        u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
        struct nd_label_ent *label_ent;
        struct nd_namespace_pmem *nspm;
-       struct nd_mapping *nd_mapping;
        resource_size_t size = 0;
        struct resource *res;
        struct device *dev;
                return ERR_PTR(-ENXIO);
        }
  
-       if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
+       if (!nsl_validate_isetcookie(ndd, nd_label, cookie)) {
                dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
                                nd_label->uuid);
-               if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
+               if (!nsl_validate_isetcookie(ndd, nd_label, altcookie))
                        return ERR_PTR(-EAGAIN);
  
                dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
                        continue;
                }
  
-               size += __le64_to_cpu(label0->rawsize);
-               if (__le16_to_cpu(label0->position) != 0)
+               ndd = to_ndd(nd_mapping);
+               size += nsl_get_rawsize(ndd, label0);
+               if (nsl_get_position(ndd, label0) != 0)
                        continue;
                WARN_ON(nspm->alt_name || nspm->uuid);
-               nspm->alt_name = kmemdup((void __force *) label0->name,
-                               NSLABEL_NAME_LEN, GFP_KERNEL);
+               nspm->alt_name = kmemdup(nsl_ref_name(ndd, label0),
+                                        NSLABEL_NAME_LEN, GFP_KERNEL);
                nspm->uuid = kmemdup((void __force *) label0->uuid,
                                NSLABEL_UUID_LEN, GFP_KERNEL);
-               nspm->lbasize = __le64_to_cpu(label0->lbasize);
-               ndd = to_ndd(nd_mapping);
-               if (namespace_label_has(ndd, abstraction_guid))
-                       nspm->nsio.common.claim_class
-                               = to_nvdimm_cclass(&label0->abstraction_guid);
+               nspm->lbasize = nsl_get_lbasize(ndd, label0);
+               nspm->nsio.common.claim_class =
+                       nsl_get_claim_class(ndd, label0);
        }
  
        if (!nspm->alt_name || !nspm->uuid) {
@@@ -2237,7 -2230,7 +2230,7 @@@ static int add_namespace_resource(struc
                if (is_namespace_blk(devs[i])) {
                        res = nsblk_add_resource(nd_region, ndd,
                                        to_nd_namespace_blk(devs[i]),
-                                       __le64_to_cpu(nd_label->dpa));
+                                       nsl_get_dpa(ndd, nd_label));
                        if (!res)
                                return -ENXIO;
                        nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
@@@ -2265,21 -2258,10 +2258,10 @@@ static struct device *create_namespace_
        struct device *dev = NULL;
        struct resource *res;
  
-       if (namespace_label_has(ndd, type_guid)) {
-               if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
-                       dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
-                                       &nd_set->type_guid,
-                                       &nd_label->type_guid);
-                       return ERR_PTR(-EAGAIN);
-               }
-               if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
-                       dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
-                                       nd_set->cookie2,
-                                       __le64_to_cpu(nd_label->isetcookie));
-                       return ERR_PTR(-EAGAIN);
-               }
-       }
+       if (!nsl_validate_type_guid(ndd, nd_label, &nd_set->type_guid))
+               return ERR_PTR(-EAGAIN);
+       if (!nsl_validate_blk_isetcookie(ndd, nd_label, nd_set->cookie2))
+               return ERR_PTR(-EAGAIN);
  
        nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
        if (!nsblk)
        dev->type = &namespace_blk_device_type;
        dev->parent = &nd_region->dev;
        nsblk->id = -1;
-       nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
-       nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
-                       GFP_KERNEL);
-       if (namespace_label_has(ndd, abstraction_guid))
-               nsblk->common.claim_class
-                       = to_nvdimm_cclass(&nd_label->abstraction_guid);
+       nsblk->lbasize = nsl_get_lbasize(ndd, nd_label);
+       nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN, GFP_KERNEL);
+       nsblk->common.claim_class = nsl_get_claim_class(ndd, nd_label);
        if (!nsblk->uuid)
                goto blk_err;
-       memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
+       nsl_get_name(ndd, nd_label, name);
        if (name[0]) {
-               nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
-                               GFP_KERNEL);
+               nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, GFP_KERNEL);
                if (!nsblk->alt_name)
                        goto blk_err;
        }
        res = nsblk_add_resource(nd_region, ndd, nsblk,
-                       __le64_to_cpu(nd_label->dpa));
+                       nsl_get_dpa(ndd, nd_label));
        if (!res)
                goto blk_err;
        nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
@@@ -2345,6 -2323,7 +2323,7 @@@ static struct device **scan_labels(stru
        struct device *dev, **devs = NULL;
        struct nd_label_ent *label_ent, *e;
        struct nd_mapping *nd_mapping = &nd_region->mapping[0];
+       struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
        resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
  
        /* "safe" because create_namespace_pmem() might list_move() label_ent */
  
                if (!nd_label)
                        continue;
-               flags = __le32_to_cpu(nd_label->flags);
+               flags = nsl_get_flags(ndd, nd_label);
                if (is_nd_blk(&nd_region->dev)
                                == !!(flags & NSLABEL_FLAG_LOCAL))
                        /* pass, region matches label type */;
                        continue;
  
                /* skip labels that describe extents outside of the region */
-               if (__le64_to_cpu(nd_label->dpa) < nd_mapping->start ||
-                   __le64_to_cpu(nd_label->dpa) > map_end)
-                               continue;
+               if (nsl_get_dpa(ndd, nd_label) < nd_mapping->start ||
+                   nsl_get_dpa(ndd, nd_label) > map_end)
+                       continue;
  
                i = add_namespace_resource(nd_region, nd_label, devs, count);
                if (i < 0)
  
                if (is_nd_blk(&nd_region->dev))
                        dev = create_namespace_blk(nd_region, nd_label, count);
-               else {
-                       struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
-                       struct nd_namespace_index *nsindex;
-                       nsindex = to_namespace_index(ndd, ndd->ns_current);
-                       dev = create_namespace_pmem(nd_region, nsindex, nd_label);
-               }
+               else
+                       dev = create_namespace_pmem(nd_region, nd_mapping,
+                                                   nd_label);
  
                if (IS_ERR(dev)) {
                        switch (PTR_ERR(dev)) {
@@@ -2527,7 -2502,7 +2502,7 @@@ static void deactivate_labels(void *reg
  
  static int init_active_labels(struct nd_region *nd_region)
  {
 -      int i;
 +      int i, rc = 0;
  
        for (i = 0; i < nd_region->ndr_mappings; i++) {
                struct nd_mapping *nd_mapping = &nd_region->mapping[i];
                        else if (test_bit(NDD_LABELING, &nvdimm->flags))
                                /* fail, labels needed to disambiguate dpa */;
                        else
 -                              return 0;
 +                              continue;
  
                        dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
                                        dev_name(&nd_mapping->nvdimm->dev),
                                        test_bit(NDD_LOCKED, &nvdimm->flags)
                                        ? "locked" : "disabled");
 -                      return -ENXIO;
 +                      rc = -ENXIO;
 +                      goto out;
                }
                nd_mapping->ndd = ndd;
                atomic_inc(&nvdimm->busy);
                                break;
                        label = nd_label_active(ndd, j);
                        if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
-                               u32 flags = __le32_to_cpu(label->flags);
+                               u32 flags = nsl_get_flags(ndd, label);
  
                                flags &= ~NSLABEL_FLAG_LOCAL;
-                               label->flags = __cpu_to_le32(flags);
+                               nsl_set_flags(ndd, label, flags);
                        }
                        label_ent->label = label;
  
                        break;
        }
  
 -      if (i < nd_region->ndr_mappings) {
 +      if (i < nd_region->ndr_mappings)
 +              rc = -ENOMEM;
 +
 +out:
 +      if (rc) {
                deactivate_labels(nd_region);
 -              return -ENOMEM;
 +              return rc;
        }
  
        return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
 -                      nd_region);
 +                                      nd_region);
  }
  
  int nd_region_register_namespaces(struct nd_region *nd_region, int *err)