Merge remote-tracking branch 'nvdimm/libnvdimm-for-next'
[deliverable/linux.git] / drivers / nvdimm / dimm.c
CommitLineData
4d88a97a
DW
1/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/vmalloc.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/sizes.h>
17#include <linux/ndctl.h>
18#include <linux/slab.h>
19#include <linux/mm.h>
20#include <linux/nd.h>
4a826c83 21#include "label.h"
4d88a97a
DW
22#include "nd.h"
23
4d88a97a
DW
24static int nvdimm_probe(struct device *dev)
25{
26 struct nvdimm_drvdata *ndd;
27 int rc;
28
aee65987
TK
29 rc = nvdimm_check_config_data(dev);
30 if (rc) {
31 /* not required for non-aliased nvdimm, ex. NVDIMM-N */
32 if (rc == -ENOTTY)
33 rc = 0;
34 return rc;
35 }
36
4d88a97a
DW
37 ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
38 if (!ndd)
39 return -ENOMEM;
40
41 dev_set_drvdata(dev, ndd);
4a826c83
DW
42 ndd->dpa.name = dev_name(dev);
43 ndd->ns_current = -1;
44 ndd->ns_next = -1;
45 ndd->dpa.start = 0;
46 ndd->dpa.end = -1;
4d88a97a 47 ndd->dev = dev;
bf9bccc1
DW
48 get_device(dev);
49 kref_init(&ndd->kref);
4d88a97a
DW
50
51 rc = nvdimm_init_nsarea(ndd);
52 if (rc)
53 goto err;
54
55 rc = nvdimm_init_config_data(ndd);
56 if (rc)
57 goto err;
58
59 dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
60
4a826c83
DW
61 nvdimm_bus_lock(dev);
62 ndd->ns_current = nd_label_validate(ndd);
63 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
64 nd_label_copy(ndd, to_next_namespace_index(ndd),
65 to_current_namespace_index(ndd));
66 rc = nd_label_reserve_dpa(ndd);
67 nvdimm_bus_unlock(dev);
68
69 if (rc)
70 goto err;
71
4d88a97a
DW
72 return 0;
73
74 err:
bf9bccc1 75 put_ndd(ndd);
4d88a97a
DW
76 return rc;
77}
78
79static int nvdimm_remove(struct device *dev)
80{
81 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
82
aee65987
TK
83 if (!ndd)
84 return 0;
85
4a826c83
DW
86 nvdimm_bus_lock(dev);
87 dev_set_drvdata(dev, NULL);
4a826c83 88 nvdimm_bus_unlock(dev);
bf9bccc1 89 put_ndd(ndd);
4d88a97a
DW
90
91 return 0;
92}
93
94static struct nd_device_driver nvdimm_driver = {
95 .probe = nvdimm_probe,
96 .remove = nvdimm_remove,
97 .drv = {
98 .name = "nvdimm",
99 },
100 .type = ND_DRIVER_DIMM,
101};
102
103int __init nvdimm_init(void)
104{
105 return nd_driver_register(&nvdimm_driver);
106}
107
3d88002e 108void nvdimm_exit(void)
4d88a97a
DW
109{
110 driver_unregister(&nvdimm_driver.drv);
111}
112
113MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);
This page took 0.089431 seconds and 5 git commands to generate.