blob: ee0b412827bfb43fc739d5e03e873f12254517fa [file] [log] [blame]
Dan Williams4d88a972015-05-31 14:41:48 -04001/*
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>
Dan Williams4a826c82015-06-09 16:09:36 -040021#include "label.h"
Dan Williams4d88a972015-05-31 14:41:48 -040022#include "nd.h"
23
Dan Williams4d88a972015-05-31 14:41:48 -040024static int nvdimm_probe(struct device *dev)
25{
26 struct nvdimm_drvdata *ndd;
27 int rc;
28
Toshi Kaniaee65982016-08-16 13:08:40 -060029 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
Dan Williams4d88a972015-05-31 14:41:48 -040037 ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
38 if (!ndd)
39 return -ENOMEM;
40
41 dev_set_drvdata(dev, ndd);
Dan Williams4a826c82015-06-09 16:09:36 -040042 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;
Dan Williams4d88a972015-05-31 14:41:48 -040047 ndd->dev = dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -040048 get_device(dev);
49 kref_init(&ndd->kref);
Dan Williams4d88a972015-05-31 14:41:48 -040050
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
Dan Williams4a826c82015-06-09 16:09:36 -040061 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);
Dan Williams42237e32016-10-15 15:33:52 -070067 if (ndd->ns_current >= 0)
68 nvdimm_set_aliasing(dev);
Dan Williams4a826c82015-06-09 16:09:36 -040069 nvdimm_bus_unlock(dev);
70
71 if (rc)
72 goto err;
73
Dan Williams4d88a972015-05-31 14:41:48 -040074 return 0;
75
76 err:
Dan Williamsbf9bccc2015-06-17 17:14:46 -040077 put_ndd(ndd);
Dan Williams4d88a972015-05-31 14:41:48 -040078 return rc;
79}
80
81static int nvdimm_remove(struct device *dev)
82{
83 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
84
Toshi Kaniaee65982016-08-16 13:08:40 -060085 if (!ndd)
86 return 0;
87
Dan Williams4a826c82015-06-09 16:09:36 -040088 nvdimm_bus_lock(dev);
89 dev_set_drvdata(dev, NULL);
Dan Williams4a826c82015-06-09 16:09:36 -040090 nvdimm_bus_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040091 put_ndd(ndd);
Dan Williams4d88a972015-05-31 14:41:48 -040092
93 return 0;
94}
95
96static struct nd_device_driver nvdimm_driver = {
97 .probe = nvdimm_probe,
98 .remove = nvdimm_remove,
99 .drv = {
100 .name = "nvdimm",
101 },
102 .type = ND_DRIVER_DIMM,
103};
104
105int __init nvdimm_init(void)
106{
107 return nd_driver_register(&nvdimm_driver);
108}
109
Dan Williams3d880022015-05-31 15:02:11 -0400110void nvdimm_exit(void)
Dan Williams4d88a972015-05-31 14:41:48 -0400111{
112 driver_unregister(&nvdimm_driver.drv);
113}
114
115MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);