0001
0002
0003
0004
0005 #include <linux/cpumask.h>
0006 #include <linux/module.h>
0007 #include <linux/device.h>
0008 #include <linux/nd.h>
0009 #include "nd-core.h"
0010 #include "nd.h"
0011
0012 static int nd_region_probe(struct device *dev)
0013 {
0014 int err, rc;
0015 static unsigned long once;
0016 struct nd_region_data *ndrd;
0017 struct nd_region *nd_region = to_nd_region(dev);
0018 struct range range = {
0019 .start = nd_region->ndr_start,
0020 .end = nd_region->ndr_start + nd_region->ndr_size - 1,
0021 };
0022
0023 if (nd_region->num_lanes > num_online_cpus()
0024 && nd_region->num_lanes < num_possible_cpus()
0025 && !test_and_set_bit(0, &once)) {
0026 dev_dbg(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n",
0027 num_online_cpus(), nd_region->num_lanes,
0028 num_possible_cpus());
0029 dev_dbg(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n",
0030 nd_region->num_lanes);
0031 }
0032
0033 rc = nd_region_activate(nd_region);
0034 if (rc)
0035 return rc;
0036
0037 if (devm_init_badblocks(dev, &nd_region->bb))
0038 return -ENODEV;
0039 nd_region->bb_state =
0040 sysfs_get_dirent(nd_region->dev.kobj.sd, "badblocks");
0041 if (!nd_region->bb_state)
0042 dev_warn(dev, "'badblocks' notification disabled\n");
0043 nvdimm_badblocks_populate(nd_region, &nd_region->bb, &range);
0044
0045 rc = nd_region_register_namespaces(nd_region, &err);
0046 if (rc < 0)
0047 return rc;
0048
0049 ndrd = dev_get_drvdata(dev);
0050 ndrd->ns_active = rc;
0051 ndrd->ns_count = rc + err;
0052
0053 if (rc && err && rc == err)
0054 return -ENODEV;
0055
0056 nd_region->btt_seed = nd_btt_create(nd_region);
0057 nd_region->pfn_seed = nd_pfn_create(nd_region);
0058 nd_region->dax_seed = nd_dax_create(nd_region);
0059 if (err == 0)
0060 return 0;
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 dev_err(dev, "failed to register %d namespace%s, continuing...\n",
0072 err, err == 1 ? "" : "s");
0073 return 0;
0074 }
0075
0076 static int child_unregister(struct device *dev, void *data)
0077 {
0078 nd_device_unregister(dev, ND_SYNC);
0079 return 0;
0080 }
0081
0082 static void nd_region_remove(struct device *dev)
0083 {
0084 struct nd_region *nd_region = to_nd_region(dev);
0085
0086 device_for_each_child(dev, NULL, child_unregister);
0087
0088
0089 nvdimm_bus_lock(dev);
0090 nd_region->ns_seed = NULL;
0091 nd_region->btt_seed = NULL;
0092 nd_region->pfn_seed = NULL;
0093 nd_region->dax_seed = NULL;
0094 dev_set_drvdata(dev, NULL);
0095 nvdimm_bus_unlock(dev);
0096
0097
0098
0099
0100
0101 sysfs_put(nd_region->bb_state);
0102 nd_region->bb_state = NULL;
0103 }
0104
0105 static int child_notify(struct device *dev, void *data)
0106 {
0107 nd_device_notify(dev, *(enum nvdimm_event *) data);
0108 return 0;
0109 }
0110
0111 static void nd_region_notify(struct device *dev, enum nvdimm_event event)
0112 {
0113 if (event == NVDIMM_REVALIDATE_POISON) {
0114 struct nd_region *nd_region = to_nd_region(dev);
0115
0116 if (is_memory(&nd_region->dev)) {
0117 struct range range = {
0118 .start = nd_region->ndr_start,
0119 .end = nd_region->ndr_start +
0120 nd_region->ndr_size - 1,
0121 };
0122
0123 nvdimm_badblocks_populate(nd_region,
0124 &nd_region->bb, &range);
0125 if (nd_region->bb_state)
0126 sysfs_notify_dirent(nd_region->bb_state);
0127 }
0128 }
0129 device_for_each_child(dev, &event, child_notify);
0130 }
0131
0132 static struct nd_device_driver nd_region_driver = {
0133 .probe = nd_region_probe,
0134 .remove = nd_region_remove,
0135 .notify = nd_region_notify,
0136 .drv = {
0137 .name = "nd_region",
0138 },
0139 .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
0140 };
0141
0142 int __init nd_region_init(void)
0143 {
0144 return nd_driver_register(&nd_region_driver);
0145 }
0146
0147 void nd_region_exit(void)
0148 {
0149 driver_unregister(&nd_region_driver.drv);
0150 }
0151
0152 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);