blob: 869a886c292ebf94f963abb9f747b0c2802fb471 [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>
Dave Jiang6a6bef92017-04-07 15:33:20 -070017#include "nd-core.h"
Dan Williams3d880022015-05-31 15:02:11 -040018#include "nd.h"
19
20static int nd_region_probe(struct device *dev)
21{
Ross Zwisler047fc8a2015-06-25 04:21:02 -040022 int err, rc;
Vishal Verma5212e112015-06-25 04:20:32 -040023 static unsigned long once;
Dan Williamse5ae3b22016-06-07 17:00:04 -070024 struct nd_region_data *ndrd;
Dan Williams3d880022015-05-31 15:02:11 -040025 struct nd_region *nd_region = to_nd_region(dev);
Dan Williams3d880022015-05-31 15:02:11 -040026
Vishal Verma5212e112015-06-25 04:20:32 -040027 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
Dan Williamse5ae3b22016-06-07 17:00:04 -070037 rc = nd_region_activate(nd_region);
38 if (rc)
39 return rc;
40
Ross Zwisler047fc8a2015-06-25 04:21:02 -040041 rc = nd_blk_region_init(nd_region);
42 if (rc)
43 return rc;
44
45 rc = nd_region_register_namespaces(nd_region, &err);
Dan Williams3d880022015-05-31 15:02:11 -040046 if (rc < 0)
47 return rc;
48
Dan Williamse5ae3b22016-06-07 17:00:04 -070049 ndrd = dev_get_drvdata(dev);
50 ndrd->ns_active = rc;
51 ndrd->ns_count = rc + err;
Dan Williams3d880022015-05-31 15:02:11 -040052
Dan Williams8c2f7e82015-06-25 04:20:04 -040053 if (rc && err && rc == err)
54 return -ENODEV;
55
Dave Jiang6a6bef92017-04-07 15:33:20 -070056 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;
61 ndr_res.start = nd_region->ndr_start;
62 ndr_res.end = nd_region->ndr_start + nd_region->ndr_size - 1;
63 nvdimm_badblocks_populate(nd_region,
64 &nd_region->bb, &ndr_res);
65 }
66
Dan Williams8c2f7e82015-06-25 04:20:04 -040067 nd_region->btt_seed = nd_btt_create(nd_region);
Dan Williamse1455742015-07-30 17:57:47 -040068 nd_region->pfn_seed = nd_pfn_create(nd_region);
Dan Williamscd034122016-03-11 10:15:36 -080069 nd_region->dax_seed = nd_dax_create(nd_region);
Dan Williams3d880022015-05-31 15:02:11 -040070 if (err == 0)
71 return 0;
72
Dan Williams3d880022015-05-31 15:02:11 -040073 /*
74 * Given multiple namespaces per region, we do not want to
75 * disable all the successfully registered peer namespaces upon
76 * a single registration failure. If userspace is missing a
77 * namespace that it expects it can disable/re-enable the region
78 * to retry discovery after correcting the failure.
79 * <regionX>/namespaces returns the current
80 * "<async-registered>/<total>" namespace count.
81 */
82 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
83 err, err == 1 ? "" : "s");
84 return 0;
85}
86
87static int child_unregister(struct device *dev, void *data)
88{
89 nd_device_unregister(dev, ND_SYNC);
90 return 0;
91}
92
93static int nd_region_remove(struct device *dev)
94{
Dan Williamsbf9bccc2015-06-17 17:14:46 -040095 struct nd_region *nd_region = to_nd_region(dev);
96
Dan Williamsa8f72022016-07-09 16:46:29 -070097 device_for_each_child(dev, NULL, child_unregister);
98
Dan Williams3d880022015-05-31 15:02:11 -040099 /* flush attribute readers and disable */
100 nvdimm_bus_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400101 nd_region->ns_seed = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400102 nd_region->btt_seed = NULL;
Dan Williamse1455742015-07-30 17:57:47 -0400103 nd_region->pfn_seed = NULL;
Dan Williamscd034122016-03-11 10:15:36 -0800104 nd_region->dax_seed = NULL;
Dan Williams3d880022015-05-31 15:02:11 -0400105 dev_set_drvdata(dev, NULL);
106 nvdimm_bus_unlock(dev);
107
Dan Williams3d880022015-05-31 15:02:11 -0400108 return 0;
109}
110
Dan Williams71999462016-02-18 10:29:49 -0800111static int child_notify(struct device *dev, void *data)
112{
113 nd_device_notify(dev, *(enum nvdimm_event *) data);
114 return 0;
115}
116
117static void nd_region_notify(struct device *dev, enum nvdimm_event event)
118{
Dave Jiang6a6bef92017-04-07 15:33:20 -0700119 if (event == NVDIMM_REVALIDATE_POISON) {
120 struct nd_region *nd_region = to_nd_region(dev);
121 struct resource res;
122
123 if (is_nd_pmem(&nd_region->dev)) {
124 res.start = nd_region->ndr_start;
125 res.end = nd_region->ndr_start +
126 nd_region->ndr_size - 1;
127 nvdimm_badblocks_populate(nd_region,
128 &nd_region->bb, &res);
129 }
130 }
Dan Williams71999462016-02-18 10:29:49 -0800131 device_for_each_child(dev, &event, child_notify);
132}
133
Dan Williams3d880022015-05-31 15:02:11 -0400134static struct nd_device_driver nd_region_driver = {
135 .probe = nd_region_probe,
136 .remove = nd_region_remove,
Dan Williams71999462016-02-18 10:29:49 -0800137 .notify = nd_region_notify,
Dan Williams3d880022015-05-31 15:02:11 -0400138 .drv = {
139 .name = "nd_region",
140 },
141 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
142};
143
144int __init nd_region_init(void)
145{
146 return nd_driver_register(&nd_region_driver);
147}
148
149void nd_region_exit(void)
150{
151 driver_unregister(&nd_region_driver.drv);
152}
153
154MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
155MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);