libnvdimm: namespace indices: read and validate
[firefly-linux-kernel-4.4.55.git] / drivers / nvdimm / dimm.c
index eb20fc2df32b4cb88ffdb7252963510b51c1a8eb..2df97c3c3b3492f4f496e6c14c27fc15cbc19195 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/nd.h>
+#include "label.h"
 #include "nd.h"
 
 static void free_data(struct nvdimm_drvdata *ndd)
@@ -42,6 +43,11 @@ static int nvdimm_probe(struct device *dev)
                return -ENOMEM;
 
        dev_set_drvdata(dev, ndd);
+       ndd->dpa.name = dev_name(dev);
+       ndd->ns_current = -1;
+       ndd->ns_next = -1;
+       ndd->dpa.start = 0;
+       ndd->dpa.end = -1;
        ndd->dev = dev;
 
        rc = nvdimm_init_nsarea(ndd);
@@ -54,6 +60,17 @@ static int nvdimm_probe(struct device *dev)
 
        dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
 
+       nvdimm_bus_lock(dev);
+       ndd->ns_current = nd_label_validate(ndd);
+       ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
+       nd_label_copy(ndd, to_next_namespace_index(ndd),
+                       to_current_namespace_index(ndd));
+       rc = nd_label_reserve_dpa(ndd);
+       nvdimm_bus_unlock(dev);
+
+       if (rc)
+               goto err;
+
        return 0;
 
  err:
@@ -64,7 +81,13 @@ static int nvdimm_probe(struct device *dev)
 static int nvdimm_remove(struct device *dev)
 {
        struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
+       struct resource *res, *_r;
 
+       nvdimm_bus_lock(dev);
+       dev_set_drvdata(dev, NULL);
+       for_each_dpa_resource_safe(ndd, res, _r)
+               nvdimm_free_dpa(ndd, res);
+       nvdimm_bus_unlock(dev);
        free_data(ndd);
 
        return 0;