Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 4 | */ |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 5 | #include <linux/cpumask.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 6 | #include <linux/module.h> |
| 7 | #include <linux/device.h> |
| 8 | #include <linux/nd.h> |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 9 | #include "nd-core.h" |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 10 | #include "nd.h" |
| 11 | |
| 12 | static int nd_region_probe(struct device *dev) |
| 13 | { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 14 | int err, rc; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 15 | static unsigned long once; |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 16 | struct nd_region_data *ndrd; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 17 | struct nd_region *nd_region = to_nd_region(dev); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 18 | |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 19 | if (nd_region->num_lanes > num_online_cpus() |
| 20 | && nd_region->num_lanes < num_possible_cpus() |
| 21 | && !test_and_set_bit(0, &once)) { |
Dan Williams | 60ce0f9 | 2018-04-07 07:47:10 -0700 | [diff] [blame] | 22 | dev_dbg(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n", |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 23 | num_online_cpus(), nd_region->num_lanes, |
| 24 | num_possible_cpus()); |
Dan Williams | 60ce0f9 | 2018-04-07 07:47:10 -0700 | [diff] [blame] | 25 | dev_dbg(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n", |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 26 | nd_region->num_lanes); |
| 27 | } |
| 28 | |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 29 | rc = nd_region_activate(nd_region); |
| 30 | if (rc) |
| 31 | return rc; |
| 32 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 33 | rc = nd_blk_region_init(nd_region); |
| 34 | if (rc) |
| 35 | return rc; |
| 36 | |
| 37 | rc = nd_region_register_namespaces(nd_region, &err); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 38 | if (rc < 0) |
| 39 | return rc; |
| 40 | |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 41 | ndrd = dev_get_drvdata(dev); |
| 42 | ndrd->ns_active = rc; |
| 43 | ndrd->ns_count = rc + err; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 44 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 45 | if (rc && err && rc == err) |
| 46 | return -ENODEV; |
| 47 | |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 48 | 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 Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 53 | nd_region->bb_state = sysfs_get_dirent(nd_region->dev.kobj.sd, |
| 54 | "badblocks"); |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame] | 55 | if (!nd_region->bb_state) |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 56 | dev_warn(&nd_region->dev, |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame] | 57 | "'badblocks' notification disabled\n"); |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 58 | ndr_res.start = nd_region->ndr_start; |
| 59 | ndr_res.end = nd_region->ndr_start + nd_region->ndr_size - 1; |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 60 | nvdimm_badblocks_populate(nd_region, &nd_region->bb, &ndr_res); |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 63 | nd_region->btt_seed = nd_btt_create(nd_region); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 64 | nd_region->pfn_seed = nd_pfn_create(nd_region); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 65 | nd_region->dax_seed = nd_dax_create(nd_region); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 66 | if (err == 0) |
| 67 | return 0; |
| 68 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 69 | /* |
| 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 | |
| 83 | static int child_unregister(struct device *dev, void *data) |
| 84 | { |
| 85 | nd_device_unregister(dev, ND_SYNC); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int nd_region_remove(struct device *dev) |
| 90 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 91 | struct nd_region *nd_region = to_nd_region(dev); |
| 92 | |
Dan Williams | a8f7202 | 2016-07-09 16:46:29 -0700 | [diff] [blame] | 93 | device_for_each_child(dev, NULL, child_unregister); |
| 94 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 95 | /* flush attribute readers and disable */ |
| 96 | nvdimm_bus_lock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 97 | nd_region->ns_seed = NULL; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 98 | nd_region->btt_seed = NULL; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 99 | nd_region->pfn_seed = NULL; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 100 | nd_region->dax_seed = NULL; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 101 | dev_set_drvdata(dev, NULL); |
| 102 | nvdimm_bus_unlock(dev); |
| 103 | |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame] | 104 | /* |
| 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 Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 114 | static int child_notify(struct device *dev, void *data) |
| 115 | { |
| 116 | nd_device_notify(dev, *(enum nvdimm_event *) data); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void nd_region_notify(struct device *dev, enum nvdimm_event event) |
| 121 | { |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 122 | 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 Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 132 | if (nd_region->bb_state) |
| 133 | sysfs_notify_dirent(nd_region->bb_state); |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 136 | device_for_each_child(dev, &event, child_notify); |
| 137 | } |
| 138 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 139 | static struct nd_device_driver nd_region_driver = { |
| 140 | .probe = nd_region_probe, |
| 141 | .remove = nd_region_remove, |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 142 | .notify = nd_region_notify, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 143 | .drv = { |
| 144 | .name = "nd_region", |
| 145 | }, |
| 146 | .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM, |
| 147 | }; |
| 148 | |
| 149 | int __init nd_region_init(void) |
| 150 | { |
| 151 | return nd_driver_register(&nd_region_driver); |
| 152 | } |
| 153 | |
| 154 | void nd_region_exit(void) |
| 155 | { |
| 156 | driver_unregister(&nd_region_driver.drv); |
| 157 | } |
| 158 | |
| 159 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM); |
| 160 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK); |