]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/nvdimm/region.c
libnvdimm: fix the clear-error check in nsio_rw_bytes
[thirdparty/linux.git] / drivers / nvdimm / region.c
CommitLineData
3d88002e
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 */
5212e11f 13#include <linux/cpumask.h>
3d88002e
DW
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/nd.h>
6a6bef90 17#include "nd-core.h"
3d88002e
DW
18#include "nd.h"
19
20static int nd_region_probe(struct device *dev)
21{
047fc8a1 22 int err, rc;
5212e11f 23 static unsigned long once;
e5ae3b25 24 struct nd_region_data *ndrd;
3d88002e 25 struct nd_region *nd_region = to_nd_region(dev);
3d88002e 26
5212e11f
VV
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
e5ae3b25
DW
37 rc = nd_region_activate(nd_region);
38 if (rc)
39 return rc;
40
047fc8a1
RZ
41 rc = nd_blk_region_init(nd_region);
42 if (rc)
43 return rc;
44
45 rc = nd_region_register_namespaces(nd_region, &err);
3d88002e
DW
46 if (rc < 0)
47 return rc;
48
e5ae3b25
DW
49 ndrd = dev_get_drvdata(dev);
50 ndrd->ns_active = rc;
51 ndrd->ns_count = rc + err;
3d88002e 52
8c2f7e86
DW
53 if (rc && err && rc == err)
54 return -ENODEV;
55
6a6bef90
DJ
56 if (is_nd_pmem(&nd_region->dev)) {
57 struct resource ndr_res;
58
59 if (devm_init_badblocks(dev, &nd_region->bb))
60 return -ENODEV;
975750a9
TK
61 nd_region->bb_state = sysfs_get_dirent(nd_region->dev.kobj.sd,
62 "badblocks");
63 if (nd_region->bb_state)
64 sysfs_put(nd_region->bb_state);
65 else
66 dev_warn(&nd_region->dev,
67 "sysfs_get_dirent 'badblocks' failed\n");
6a6bef90
DJ
68 ndr_res.start = nd_region->ndr_start;
69 ndr_res.end = nd_region->ndr_start + nd_region->ndr_size - 1;
975750a9 70 nvdimm_badblocks_populate(nd_region, &nd_region->bb, &ndr_res);
6a6bef90
DJ
71 }
72
8c2f7e86 73 nd_region->btt_seed = nd_btt_create(nd_region);
e1455744 74 nd_region->pfn_seed = nd_pfn_create(nd_region);
cd03412a 75 nd_region->dax_seed = nd_dax_create(nd_region);
3d88002e
DW
76 if (err == 0)
77 return 0;
78
3d88002e
DW
79 /*
80 * Given multiple namespaces per region, we do not want to
81 * disable all the successfully registered peer namespaces upon
82 * a single registration failure. If userspace is missing a
83 * namespace that it expects it can disable/re-enable the region
84 * to retry discovery after correcting the failure.
85 * <regionX>/namespaces returns the current
86 * "<async-registered>/<total>" namespace count.
87 */
88 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
89 err, err == 1 ? "" : "s");
90 return 0;
91}
92
93static int child_unregister(struct device *dev, void *data)
94{
95 nd_device_unregister(dev, ND_SYNC);
96 return 0;
97}
98
99static int nd_region_remove(struct device *dev)
100{
bf9bccc1
DW
101 struct nd_region *nd_region = to_nd_region(dev);
102
a8f72022
DW
103 device_for_each_child(dev, NULL, child_unregister);
104
3d88002e
DW
105 /* flush attribute readers and disable */
106 nvdimm_bus_lock(dev);
bf9bccc1 107 nd_region->ns_seed = NULL;
8c2f7e86 108 nd_region->btt_seed = NULL;
e1455744 109 nd_region->pfn_seed = NULL;
cd03412a 110 nd_region->dax_seed = NULL;
3d88002e
DW
111 dev_set_drvdata(dev, NULL);
112 nvdimm_bus_unlock(dev);
113
3d88002e
DW
114 return 0;
115}
116
71999466
DW
117static int child_notify(struct device *dev, void *data)
118{
119 nd_device_notify(dev, *(enum nvdimm_event *) data);
120 return 0;
121}
122
123static void nd_region_notify(struct device *dev, enum nvdimm_event event)
124{
6a6bef90
DJ
125 if (event == NVDIMM_REVALIDATE_POISON) {
126 struct nd_region *nd_region = to_nd_region(dev);
127 struct resource res;
128
129 if (is_nd_pmem(&nd_region->dev)) {
130 res.start = nd_region->ndr_start;
131 res.end = nd_region->ndr_start +
132 nd_region->ndr_size - 1;
133 nvdimm_badblocks_populate(nd_region,
134 &nd_region->bb, &res);
975750a9
TK
135 if (nd_region->bb_state)
136 sysfs_notify_dirent(nd_region->bb_state);
6a6bef90
DJ
137 }
138 }
71999466
DW
139 device_for_each_child(dev, &event, child_notify);
140}
141
3d88002e
DW
142static struct nd_device_driver nd_region_driver = {
143 .probe = nd_region_probe,
144 .remove = nd_region_remove,
71999466 145 .notify = nd_region_notify,
3d88002e
DW
146 .drv = {
147 .name = "nd_region",
148 },
149 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
150};
151
152int __init nd_region_init(void)
153{
154 return nd_driver_register(&nd_region_driver);
155}
156
157void nd_region_exit(void)
158{
159 driver_unregister(&nd_region_driver.drv);
160}
161
162MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
163MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);