blob: e0c34120df379ebff539073db7b49d3a708c0ee9 [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
Aneesh Kumar K.Vc42adf82019-09-19 14:03:55 +053037 if (is_memory(&nd_region->dev)) {
Dan Williamsa4574f62020-10-13 16:50:29 -070038 struct range range = {
39 .start = nd_region->ndr_start,
40 .end = nd_region->ndr_start + nd_region->ndr_size - 1,
41 };
Dave Jiang6a6bef92017-04-07 15:33:20 -070042
43 if (devm_init_badblocks(dev, &nd_region->bb))
44 return -ENODEV;
Toshi Kani975750a2017-06-12 16:25:11 -060045 nd_region->bb_state = sysfs_get_dirent(nd_region->dev.kobj.sd,
46 "badblocks");
Dan Williams6aa734a2017-06-30 18:56:03 -070047 if (!nd_region->bb_state)
Toshi Kani975750a2017-06-12 16:25:11 -060048 dev_warn(&nd_region->dev,
Dan Williams6aa734a2017-06-30 18:56:03 -070049 "'badblocks' notification disabled\n");
Dan Williamsa4574f62020-10-13 16:50:29 -070050 nvdimm_badblocks_populate(nd_region, &nd_region->bb, &range);
Dave Jiang6a6bef92017-04-07 15:33:20 -070051 }
52
Dan Williams700cd032019-07-17 18:08:03 -070053 rc = nd_region_register_namespaces(nd_region, &err);
54 if (rc < 0)
55 return rc;
56
57 ndrd = dev_get_drvdata(dev);
58 ndrd->ns_active = rc;
59 ndrd->ns_count = rc + err;
60
61 if (rc && err && rc == err)
62 return -ENODEV;
63
Dan Williams8c2f7e82015-06-25 04:20:04 -040064 nd_region->btt_seed = nd_btt_create(nd_region);
Dan Williamse1455742015-07-30 17:57:47 -040065 nd_region->pfn_seed = nd_pfn_create(nd_region);
Dan Williamscd034122016-03-11 10:15:36 -080066 nd_region->dax_seed = nd_dax_create(nd_region);
Dan Williams3d880022015-05-31 15:02:11 -040067 if (err == 0)
68 return 0;
69
Dan Williams3d880022015-05-31 15:02:11 -040070 /*
71 * Given multiple namespaces per region, we do not want to
72 * disable all the successfully registered peer namespaces upon
73 * a single registration failure. If userspace is missing a
74 * namespace that it expects it can disable/re-enable the region
75 * to retry discovery after correcting the failure.
76 * <regionX>/namespaces returns the current
77 * "<async-registered>/<total>" namespace count.
78 */
79 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
80 err, err == 1 ? "" : "s");
81 return 0;
82}
83
84static int child_unregister(struct device *dev, void *data)
85{
86 nd_device_unregister(dev, ND_SYNC);
87 return 0;
88}
89
Uwe Kleine-König1f975072021-02-12 18:10:43 +010090static void nd_region_remove(struct device *dev)
Dan Williams3d880022015-05-31 15:02:11 -040091{
Dan Williamsbf9bccc2015-06-17 17:14:46 -040092 struct nd_region *nd_region = to_nd_region(dev);
93
Dan Williamsa8f72022016-07-09 16:46:29 -070094 device_for_each_child(dev, NULL, child_unregister);
95
Dan Williams3d880022015-05-31 15:02:11 -040096 /* flush attribute readers and disable */
97 nvdimm_bus_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040098 nd_region->ns_seed = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -040099 nd_region->btt_seed = NULL;
Dan Williamse1455742015-07-30 17:57:47 -0400100 nd_region->pfn_seed = NULL;
Dan Williamscd034122016-03-11 10:15:36 -0800101 nd_region->dax_seed = NULL;
Dan Williams3d880022015-05-31 15:02:11 -0400102 dev_set_drvdata(dev, NULL);
103 nvdimm_bus_unlock(dev);
104
Dan Williams6aa734a2017-06-30 18:56:03 -0700105 /*
Dan Williams87a30e12019-07-17 18:08:26 -0700106 * Note, this assumes nd_device_lock() context to not race
Dan Williams6aa734a2017-06-30 18:56:03 -0700107 * nd_region_notify()
108 */
109 sysfs_put(nd_region->bb_state);
110 nd_region->bb_state = NULL;
Dan Williams3d880022015-05-31 15:02:11 -0400111}
112
Dan Williams71999462016-02-18 10:29:49 -0800113static int child_notify(struct device *dev, void *data)
114{
115 nd_device_notify(dev, *(enum nvdimm_event *) data);
116 return 0;
117}
118
119static void nd_region_notify(struct device *dev, enum nvdimm_event event)
120{
Dave Jiang6a6bef92017-04-07 15:33:20 -0700121 if (event == NVDIMM_REVALIDATE_POISON) {
122 struct nd_region *nd_region = to_nd_region(dev);
Dave Jiang6a6bef92017-04-07 15:33:20 -0700123
Aneesh Kumar K.Vc42adf82019-09-19 14:03:55 +0530124 if (is_memory(&nd_region->dev)) {
Dan Williamsa4574f62020-10-13 16:50:29 -0700125 struct range range = {
126 .start = nd_region->ndr_start,
127 .end = nd_region->ndr_start +
128 nd_region->ndr_size - 1,
129 };
130
Dave Jiang6a6bef92017-04-07 15:33:20 -0700131 nvdimm_badblocks_populate(nd_region,
Dan Williamsa4574f62020-10-13 16:50:29 -0700132 &nd_region->bb, &range);
Toshi Kani975750a2017-06-12 16:25:11 -0600133 if (nd_region->bb_state)
134 sysfs_notify_dirent(nd_region->bb_state);
Dave Jiang6a6bef92017-04-07 15:33:20 -0700135 }
136 }
Dan Williams71999462016-02-18 10:29:49 -0800137 device_for_each_child(dev, &event, child_notify);
138}
139
Dan Williams3d880022015-05-31 15:02:11 -0400140static struct nd_device_driver nd_region_driver = {
141 .probe = nd_region_probe,
142 .remove = nd_region_remove,
Dan Williams71999462016-02-18 10:29:49 -0800143 .notify = nd_region_notify,
Dan Williams3d880022015-05-31 15:02:11 -0400144 .drv = {
145 .name = "nd_region",
146 },
147 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
148};
149
150int __init nd_region_init(void)
151{
152 return nd_driver_register(&nd_region_driver);
153}
154
155void nd_region_exit(void)
156{
157 driver_unregister(&nd_region_driver.drv);
158}
159
160MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
161MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);