blob: f28f78ccff190ceb92c86684245c0364c3dde6ed [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 */
Vishal Verma5212e112015-06-25 04:20:32 -040013#include <linux/cpumask.h>
Dan Williams3d880022015-05-31 15:02:11 -040014#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/nd.h>
17#include "nd.h"
18
19static int nd_region_probe(struct device *dev)
20{
Ross Zwisler047fc8a2015-06-25 04:21:02 -040021 int err, rc;
Vishal Verma5212e112015-06-25 04:20:32 -040022 static unsigned long once;
Dan Williams3d880022015-05-31 15:02:11 -040023 struct nd_region_namespaces *num_ns;
24 struct nd_region *nd_region = to_nd_region(dev);
Dan Williams3d880022015-05-31 15:02:11 -040025
Vishal Verma5212e112015-06-25 04:20:32 -040026 if (nd_region->num_lanes > num_online_cpus()
27 && nd_region->num_lanes < num_possible_cpus()
28 && !test_and_set_bit(0, &once)) {
29 dev_info(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n",
30 num_online_cpus(), nd_region->num_lanes,
31 num_possible_cpus());
32 dev_info(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n",
33 nd_region->num_lanes);
34 }
35
Ross Zwisler047fc8a2015-06-25 04:21:02 -040036 rc = nd_blk_region_init(nd_region);
37 if (rc)
38 return rc;
39
40 rc = nd_region_register_namespaces(nd_region, &err);
Dan Williams3d880022015-05-31 15:02:11 -040041 num_ns = devm_kzalloc(dev, sizeof(*num_ns), GFP_KERNEL);
42 if (!num_ns)
43 return -ENOMEM;
44
45 if (rc < 0)
46 return rc;
47
48 num_ns->active = rc;
49 num_ns->count = rc + err;
50 dev_set_drvdata(dev, num_ns);
51
Dan Williams8c2f7e82015-06-25 04:20:04 -040052 if (rc && err && rc == err)
53 return -ENODEV;
54
55 nd_region->btt_seed = nd_btt_create(nd_region);
Dan Williams3d880022015-05-31 15:02:11 -040056 if (err == 0)
57 return 0;
58
Dan Williams3d880022015-05-31 15:02:11 -040059 /*
60 * Given multiple namespaces per region, we do not want to
61 * disable all the successfully registered peer namespaces upon
62 * a single registration failure. If userspace is missing a
63 * namespace that it expects it can disable/re-enable the region
64 * to retry discovery after correcting the failure.
65 * <regionX>/namespaces returns the current
66 * "<async-registered>/<total>" namespace count.
67 */
68 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
69 err, err == 1 ? "" : "s");
70 return 0;
71}
72
73static int child_unregister(struct device *dev, void *data)
74{
75 nd_device_unregister(dev, ND_SYNC);
76 return 0;
77}
78
79static int nd_region_remove(struct device *dev)
80{
Dan Williamsbf9bccc2015-06-17 17:14:46 -040081 struct nd_region *nd_region = to_nd_region(dev);
82
Dan Williams3d880022015-05-31 15:02:11 -040083 /* flush attribute readers and disable */
84 nvdimm_bus_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040085 nd_region->ns_seed = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -040086 nd_region->btt_seed = NULL;
Dan Williams3d880022015-05-31 15:02:11 -040087 dev_set_drvdata(dev, NULL);
88 nvdimm_bus_unlock(dev);
89
90 device_for_each_child(dev, NULL, child_unregister);
91 return 0;
92}
93
94static struct nd_device_driver nd_region_driver = {
95 .probe = nd_region_probe,
96 .remove = nd_region_remove,
97 .drv = {
98 .name = "nd_region",
99 },
100 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
101};
102
103int __init nd_region_init(void)
104{
105 return nd_driver_register(&nd_region_driver);
106}
107
108void nd_region_exit(void)
109{
110 driver_unregister(&nd_region_driver.drv);
111}
112
113MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
114MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);