]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/nvdimm/dimm.c
libnvdimm: namespace indices: read and validate
[mirror_ubuntu-eoan-kernel.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
24static void free_data(struct nvdimm_drvdata *ndd)
25{
26 if (!ndd)
27 return;
28
29 if (ndd->data && is_vmalloc_addr(ndd->data))
30 vfree(ndd->data);
31 else
32 kfree(ndd->data);
33 kfree(ndd);
34}
35
36static int nvdimm_probe(struct device *dev)
37{
38 struct nvdimm_drvdata *ndd;
39 int rc;
40
41 ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
42 if (!ndd)
43 return -ENOMEM;
44
45 dev_set_drvdata(dev, ndd);
4a826c83
DW
46 ndd->dpa.name = dev_name(dev);
47 ndd->ns_current = -1;
48 ndd->ns_next = -1;
49 ndd->dpa.start = 0;
50 ndd->dpa.end = -1;
4d88a97a
DW
51 ndd->dev = dev;
52
53 rc = nvdimm_init_nsarea(ndd);
54 if (rc)
55 goto err;
56
57 rc = nvdimm_init_config_data(ndd);
58 if (rc)
59 goto err;
60
61 dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
62
4a826c83
DW
63 nvdimm_bus_lock(dev);
64 ndd->ns_current = nd_label_validate(ndd);
65 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
66 nd_label_copy(ndd, to_next_namespace_index(ndd),
67 to_current_namespace_index(ndd));
68 rc = nd_label_reserve_dpa(ndd);
69 nvdimm_bus_unlock(dev);
70
71 if (rc)
72 goto err;
73
4d88a97a
DW
74 return 0;
75
76 err:
77 free_data(ndd);
78 return rc;
79}
80
81static int nvdimm_remove(struct device *dev)
82{
83 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
4a826c83 84 struct resource *res, *_r;
4d88a97a 85
4a826c83
DW
86 nvdimm_bus_lock(dev);
87 dev_set_drvdata(dev, NULL);
88 for_each_dpa_resource_safe(ndd, res, _r)
89 nvdimm_free_dpa(ndd, res);
90 nvdimm_bus_unlock(dev);
4d88a97a
DW
91 free_data(ndd);
92
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
3d88002e 110void nvdimm_exit(void)
4d88a97a
DW
111{
112 driver_unregister(&nvdimm_driver.drv);
113}
114
115MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);