blob: ef46cc3a71aefef4b7927dabdb44a4759a5dd98a [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams3d880022015-05-31 15:02:11 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williams3d880022015-05-31 15:02:11 -04004 */
Vishal Verma5212e112015-06-25 04:20:32 -04005#include <linux/cpumask.h>
Dan Williams3d880022015-05-31 15:02:11 -04006#include <linux/module.h>
7#include <linux/device.h>
8#include <linux/nd.h>
Dave Jiang6a6bef92017-04-07 15:33:20 -07009#include "nd-core.h"
Dan Williams3d880022015-05-31 15:02:11 -040010#include "nd.h"
11
12static int nd_region_probe(struct device *dev)
13{
Ross Zwisler047fc8a2015-06-25 04:21:02 -040014 int err, rc;
Vishal Verma5212e112015-06-25 04:20:32 -040015 static unsigned long once;
Dan Williamse5ae3b22016-06-07 17:00:04 -070016 struct nd_region_data *ndrd;
Dan Williams3d880022015-05-31 15:02:11 -040017 struct nd_region *nd_region = to_nd_region(dev);
Dan Williams3d880022015-05-31 15:02:11 -040018
Vishal Verma5212e112015-06-25 04:20:32 -040019 if (nd_region->num_lanes > num_online_cpus()
20 && nd_region->num_lanes < num_possible_cpus()
21 && !test_and_set_bit(0, &once)) {
Dan Williams60ce0f92018-04-07 07:47:10 -070022 dev_dbg(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n",
Vishal Verma5212e112015-06-25 04:20:32 -040023 num_online_cpus(), nd_region->num_lanes,
24 num_possible_cpus());
Dan Williams60ce0f92018-04-07 07:47:10 -070025 dev_dbg(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n",
Vishal Verma5212e112015-06-25 04:20:32 -040026 nd_region->num_lanes);
27 }
28
Dan Williamse5ae3b22016-06-07 17:00:04 -070029 rc = nd_region_activate(nd_region);
30 if (rc)
31 return rc;
32
Ross Zwisler047fc8a2015-06-25 04:21:02 -040033 rc = nd_blk_region_init(nd_region);
34 if (rc)
35 return rc;
36
37 rc = nd_region_register_namespaces(nd_region, &err);
Dan Williams3d880022015-05-31 15:02:11 -040038 if (rc < 0)
39 return rc;
40
Dan Williamse5ae3b22016-06-07 17:00:04 -070041 ndrd = dev_get_drvdata(dev);
42 ndrd->ns_active = rc;
43 ndrd->ns_count = rc + err;
Dan Williams3d880022015-05-31 15:02:11 -040044
Dan Williams8c2f7e82015-06-25 04:20:04 -040045 if (rc && err && rc == err)
46 return -ENODEV;
47
Dave Jiang6a6bef92017-04-07 15:33:20 -070048 if (is_nd_pmem(&nd_region->dev)) {
49 struct resource ndr_res;
50
51 if (devm_init_badblocks(dev, &nd_region->bb))
52 return -ENODEV;
Toshi Kani975750a2017-06-12 16:25:11 -060053 nd_region->bb_state = sysfs_get_dirent(nd_region->dev.kobj.sd,
54 "badblocks");
Dan Williams6aa734a2017-06-30 18:56:03 -070055 if (!nd_region->bb_state)
Toshi Kani975750a2017-06-12 16:25:11 -060056 dev_warn(&nd_region->dev,
Dan Williams6aa734a2017-06-30 18:56:03 -070057 "'badblocks' notification disabled\n");
Dave Jiang6a6bef92017-04-07 15:33:20 -070058 ndr_res.start = nd_region->ndr_start;
59 ndr_res.end = nd_region->ndr_start + nd_region->ndr_size - 1;
Toshi Kani975750a2017-06-12 16:25:11 -060060 nvdimm_badblocks_populate(nd_region, &nd_region->bb, &ndr_res);
Dave Jiang6a6bef92017-04-07 15:33:20 -070061 }
62
Dan Williams8c2f7e82015-06-25 04:20:04 -040063 nd_region->btt_seed = nd_btt_create(nd_region);
Dan Williamse1455742015-07-30 17:57:47 -040064 nd_region->pfn_seed = nd_pfn_create(nd_region);
Dan Williamscd034122016-03-11 10:15:36 -080065 nd_region->dax_seed = nd_dax_create(nd_region);
Dan Williams3d880022015-05-31 15:02:11 -040066 if (err == 0)
67 return 0;
68
Dan Williams3d880022015-05-31 15:02:11 -040069 /*
70 * Given multiple namespaces per region, we do not want to
71 * disable all the successfully registered peer namespaces upon
72 * a single registration failure. If userspace is missing a
73 * namespace that it expects it can disable/re-enable the region
74 * to retry discovery after correcting the failure.
75 * <regionX>/namespaces returns the current
76 * "<async-registered>/<total>" namespace count.
77 */
78 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
79 err, err == 1 ? "" : "s");
80 return 0;
81}
82
83static int child_unregister(struct device *dev, void *data)
84{
85 nd_device_unregister(dev, ND_SYNC);
86 return 0;
87}
88
89static int nd_region_remove(struct device *dev)
90{
Dan Williamsbf9bccc2015-06-17 17:14:46 -040091 struct nd_region *nd_region = to_nd_region(dev);
92
Dan Williamsa8f72022016-07-09 16:46:29 -070093 device_for_each_child(dev, NULL, child_unregister);
94
Dan Williams3d880022015-05-31 15:02:11 -040095 /* flush attribute readers and disable */
96 nvdimm_bus_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040097 nd_region->ns_seed = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -040098 nd_region->btt_seed = NULL;
Dan Williamse1455742015-07-30 17:57:47 -040099 nd_region->pfn_seed = NULL;
Dan Williamscd034122016-03-11 10:15:36 -0800100 nd_region->dax_seed = NULL;
Dan Williams3d880022015-05-31 15:02:11 -0400101 dev_set_drvdata(dev, NULL);
102 nvdimm_bus_unlock(dev);
103
Dan Williams6aa734a2017-06-30 18:56:03 -0700104 /*
105 * Note, this assumes device_lock() context to not race
106 * nd_region_notify()
107 */
108 sysfs_put(nd_region->bb_state);
109 nd_region->bb_state = NULL;
110
Dan Williams3d880022015-05-31 15:02:11 -0400111 return 0;
112}
113
Dan Williams71999462016-02-18 10:29:49 -0800114static int child_notify(struct device *dev, void *data)
115{
116 nd_device_notify(dev, *(enum nvdimm_event *) data);
117 return 0;
118}
119
120static void nd_region_notify(struct device *dev, enum nvdimm_event event)
121{
Dave Jiang6a6bef92017-04-07 15:33:20 -0700122 if (event == NVDIMM_REVALIDATE_POISON) {
123 struct nd_region *nd_region = to_nd_region(dev);
124 struct resource res;
125
126 if (is_nd_pmem(&nd_region->dev)) {
127 res.start = nd_region->ndr_start;
128 res.end = nd_region->ndr_start +
129 nd_region->ndr_size - 1;
130 nvdimm_badblocks_populate(nd_region,
131 &nd_region->bb, &res);
Toshi Kani975750a2017-06-12 16:25:11 -0600132 if (nd_region->bb_state)
133 sysfs_notify_dirent(nd_region->bb_state);
Dave Jiang6a6bef92017-04-07 15:33:20 -0700134 }
135 }
Dan Williams71999462016-02-18 10:29:49 -0800136 device_for_each_child(dev, &event, child_notify);
137}
138
Dan Williams3d880022015-05-31 15:02:11 -0400139static struct nd_device_driver nd_region_driver = {
140 .probe = nd_region_probe,
141 .remove = nd_region_remove,
Dan Williams71999462016-02-18 10:29:49 -0800142 .notify = nd_region_notify,
Dan Williams3d880022015-05-31 15:02:11 -0400143 .drv = {
144 .name = "nd_region",
145 },
146 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
147};
148
149int __init nd_region_init(void)
150{
151 return nd_driver_register(&nd_region_driver);
152}
153
154void nd_region_exit(void)
155{
156 driver_unregister(&nd_region_driver.drv);
157}
158
159MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
160MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);