blob: 2a5f3f53d79de1c576f9984cd1c4f784f6c07b44 [file] [log] [blame]
Dan Williams3d880022015-05-31 15:02:11 -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/module.h>
14#include <linux/device.h>
15#include <linux/nd.h>
16#include "nd.h"
17
18static int nd_region_probe(struct device *dev)
19{
20 int err;
21 struct nd_region_namespaces *num_ns;
22 struct nd_region *nd_region = to_nd_region(dev);
23 int rc = nd_region_register_namespaces(nd_region, &err);
24
25 num_ns = devm_kzalloc(dev, sizeof(*num_ns), GFP_KERNEL);
26 if (!num_ns)
27 return -ENOMEM;
28
29 if (rc < 0)
30 return rc;
31
32 num_ns->active = rc;
33 num_ns->count = rc + err;
34 dev_set_drvdata(dev, num_ns);
35
Dan Williams8c2f7e82015-06-25 04:20:04 -040036 if (rc && err && rc == err)
37 return -ENODEV;
38
39 nd_region->btt_seed = nd_btt_create(nd_region);
Dan Williams3d880022015-05-31 15:02:11 -040040 if (err == 0)
41 return 0;
42
Dan Williams3d880022015-05-31 15:02:11 -040043 /*
44 * Given multiple namespaces per region, we do not want to
45 * disable all the successfully registered peer namespaces upon
46 * a single registration failure. If userspace is missing a
47 * namespace that it expects it can disable/re-enable the region
48 * to retry discovery after correcting the failure.
49 * <regionX>/namespaces returns the current
50 * "<async-registered>/<total>" namespace count.
51 */
52 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
53 err, err == 1 ? "" : "s");
54 return 0;
55}
56
57static int child_unregister(struct device *dev, void *data)
58{
59 nd_device_unregister(dev, ND_SYNC);
60 return 0;
61}
62
63static int nd_region_remove(struct device *dev)
64{
Dan Williamsbf9bccc2015-06-17 17:14:46 -040065 struct nd_region *nd_region = to_nd_region(dev);
66
Dan Williams3d880022015-05-31 15:02:11 -040067 /* flush attribute readers and disable */
68 nvdimm_bus_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040069 nd_region->ns_seed = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -040070 nd_region->btt_seed = NULL;
Dan Williams3d880022015-05-31 15:02:11 -040071 dev_set_drvdata(dev, NULL);
72 nvdimm_bus_unlock(dev);
73
74 device_for_each_child(dev, NULL, child_unregister);
75 return 0;
76}
77
78static struct nd_device_driver nd_region_driver = {
79 .probe = nd_region_probe,
80 .remove = nd_region_remove,
81 .drv = {
82 .name = "nd_region",
83 },
84 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
85};
86
87int __init nd_region_init(void)
88{
89 return nd_driver_register(&nd_region_driver);
90}
91
92void nd_region_exit(void)
93{
94 driver_unregister(&nd_region_driver.drv);
95}
96
97MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
98MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);