libnvdimm, dax: introduce device-dax infrastructure
[deliverable/linux.git] / drivers / nvdimm / pfn_devs.c
index e8693fe65e492d38150ebce328102234861eebf8..6ade2eb7615d92a76726e58f7a6a779465427417 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
+ * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of version 2 of the GNU General Public License as
@@ -54,10 +54,29 @@ struct nd_pfn *to_nd_pfn(struct device *dev)
 }
 EXPORT_SYMBOL(to_nd_pfn);
 
+static struct nd_pfn *to_nd_pfn_safe(struct device *dev)
+{
+       /*
+        * pfn device attributes are re-used by dax device instances, so we
+        * need to be careful to correct device-to-nd_pfn conversion.
+        */
+       if (is_nd_pfn(dev))
+               return to_nd_pfn(dev);
+
+       if (is_nd_dax(dev)) {
+               struct nd_dax *nd_dax = to_nd_dax(dev);
+
+               return &nd_dax->nd_pfn;
+       }
+
+       WARN_ON(1);
+       return NULL;
+}
+
 static ssize_t mode_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
 
        switch (nd_pfn->mode) {
        case PFN_MODE_RAM:
@@ -72,7 +91,7 @@ static ssize_t mode_show(struct device *dev,
 static ssize_t mode_store(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t len)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
        ssize_t rc = 0;
 
        device_lock(dev);
@@ -106,7 +125,7 @@ static DEVICE_ATTR_RW(mode);
 static ssize_t align_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
 
        return sprintf(buf, "%lx\n", nd_pfn->align);
 }
@@ -134,7 +153,7 @@ static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
 static ssize_t align_store(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t len)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
        ssize_t rc;
 
        device_lock(dev);
@@ -152,7 +171,7 @@ static DEVICE_ATTR_RW(align);
 static ssize_t uuid_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
 
        if (nd_pfn->uuid)
                return sprintf(buf, "%pUb\n", nd_pfn->uuid);
@@ -162,7 +181,7 @@ static ssize_t uuid_show(struct device *dev,
 static ssize_t uuid_store(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t len)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
        ssize_t rc;
 
        device_lock(dev);
@@ -178,7 +197,7 @@ static DEVICE_ATTR_RW(uuid);
 static ssize_t namespace_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
        ssize_t rc;
 
        nvdimm_bus_lock(dev);
@@ -191,7 +210,7 @@ static ssize_t namespace_show(struct device *dev,
 static ssize_t namespace_store(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t len)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
        ssize_t rc;
 
        device_lock(dev);
@@ -209,7 +228,7 @@ static DEVICE_ATTR_RW(namespace);
 static ssize_t resource_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
        ssize_t rc;
 
        device_lock(dev);
@@ -235,7 +254,7 @@ static DEVICE_ATTR_RO(resource);
 static ssize_t size_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
-       struct nd_pfn *nd_pfn = to_nd_pfn(dev);
+       struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
        ssize_t rc;
 
        device_lock(dev);
@@ -270,7 +289,7 @@ static struct attribute *nd_pfn_attributes[] = {
        NULL,
 };
 
-static struct attribute_group nd_pfn_attribute_group = {
+struct attribute_group nd_pfn_attribute_group = {
        .attrs = nd_pfn_attributes,
 };
 
@@ -281,15 +300,31 @@ static const struct attribute_group *nd_pfn_attribute_groups[] = {
        NULL,
 };
 
-static struct device *__nd_pfn_create(struct nd_region *nd_region,
+struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
                struct nd_namespace_common *ndns)
 {
-       struct nd_pfn *nd_pfn;
-       struct device *dev;
+       struct device *dev = &nd_pfn->dev;
 
-       /* we can only create pages for contiguous ranged of pmem */
-       if (!is_nd_pmem(&nd_region->dev))
+       if (!nd_pfn)
+               return NULL;
+
+       nd_pfn->mode = PFN_MODE_NONE;
+       nd_pfn->align = HPAGE_SIZE;
+       dev = &nd_pfn->dev;
+       device_initialize(&nd_pfn->dev);
+       if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
+               dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
+                               __func__, dev_name(ndns->claim));
+               put_device(dev);
                return NULL;
+       }
+       return dev;
+}
+
+static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
+{
+       struct nd_pfn *nd_pfn;
+       struct device *dev;
 
        nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
        if (!nd_pfn)
@@ -301,29 +336,27 @@ static struct device *__nd_pfn_create(struct nd_region *nd_region,
                return NULL;
        }
 
-       nd_pfn->mode = PFN_MODE_NONE;
-       nd_pfn->align = HPAGE_SIZE;
        dev = &nd_pfn->dev;
        dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
-       dev->parent = &nd_region->dev;
-       dev->type = &nd_pfn_device_type;
        dev->groups = nd_pfn_attribute_groups;
-       device_initialize(&nd_pfn->dev);
-       if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
-               dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
-                               __func__, dev_name(ndns->claim));
-               put_device(dev);
-               return NULL;
-       }
-       return dev;
+       dev->type = &nd_pfn_device_type;
+       dev->parent = &nd_region->dev;
+
+       return nd_pfn;
 }
 
 struct device *nd_pfn_create(struct nd_region *nd_region)
 {
-       struct device *dev = __nd_pfn_create(nd_region, NULL);
+       struct nd_pfn *nd_pfn;
+       struct device *dev;
+
+       if (!is_nd_pmem(&nd_region->dev))
+               return NULL;
+
+       nd_pfn = nd_pfn_alloc(nd_region);
+       dev = nd_pfn_devinit(nd_pfn, NULL);
 
-       if (dev)
-               __nd_device_register(dev);
+       __nd_device_register(dev);
        return dev;
 }
 
@@ -423,7 +456,8 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
                return -ENODEV;
 
        nvdimm_bus_lock(&ndns->dev);
-       pfn_dev = __nd_pfn_create(nd_region, ndns);
+       nd_pfn = nd_pfn_alloc(nd_region);
+       pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
        nvdimm_bus_unlock(&ndns->dev);
        if (!pfn_dev)
                return -ENOMEM;
This page took 0.027214 seconds and 5 git commands to generate.