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