Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1 | /* |
| 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 Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 13 | #include <linux/cpumask.h> |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/nd.h> |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 17 | #include "nd-core.h" |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 18 | #include "nd.h" |
| 19 | |
| 20 | static int nd_region_probe(struct device *dev) |
| 21 | { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 22 | int err, rc; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 23 | static unsigned long once; |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 24 | struct nd_region_data *ndrd; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 25 | struct nd_region *nd_region = to_nd_region(dev); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 26 | |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 27 | 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 Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 37 | rc = nd_region_activate(nd_region); |
| 38 | if (rc) |
| 39 | return rc; |
| 40 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 41 | rc = nd_blk_region_init(nd_region); |
| 42 | if (rc) |
| 43 | return rc; |
| 44 | |
| 45 | rc = nd_region_register_namespaces(nd_region, &err); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 46 | if (rc < 0) |
| 47 | return rc; |
| 48 | |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 49 | ndrd = dev_get_drvdata(dev); |
| 50 | ndrd->ns_active = rc; |
| 51 | ndrd->ns_count = rc + err; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 52 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 53 | if (rc && err && rc == err) |
| 54 | return -ENODEV; |
| 55 | |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 56 | 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; |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 61 | nd_region->bb_state = sysfs_get_dirent(nd_region->dev.kobj.sd, |
| 62 | "badblocks"); |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame^] | 63 | if (!nd_region->bb_state) |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 64 | dev_warn(&nd_region->dev, |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame^] | 65 | "'badblocks' notification disabled\n"); |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 66 | ndr_res.start = nd_region->ndr_start; |
| 67 | ndr_res.end = nd_region->ndr_start + nd_region->ndr_size - 1; |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 68 | nvdimm_badblocks_populate(nd_region, &nd_region->bb, &ndr_res); |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 71 | nd_region->btt_seed = nd_btt_create(nd_region); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 72 | nd_region->pfn_seed = nd_pfn_create(nd_region); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 73 | nd_region->dax_seed = nd_dax_create(nd_region); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 74 | if (err == 0) |
| 75 | return 0; |
| 76 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 77 | /* |
| 78 | * Given multiple namespaces per region, we do not want to |
| 79 | * disable all the successfully registered peer namespaces upon |
| 80 | * a single registration failure. If userspace is missing a |
| 81 | * namespace that it expects it can disable/re-enable the region |
| 82 | * to retry discovery after correcting the failure. |
| 83 | * <regionX>/namespaces returns the current |
| 84 | * "<async-registered>/<total>" namespace count. |
| 85 | */ |
| 86 | dev_err(dev, "failed to register %d namespace%s, continuing...\n", |
| 87 | err, err == 1 ? "" : "s"); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int child_unregister(struct device *dev, void *data) |
| 92 | { |
| 93 | nd_device_unregister(dev, ND_SYNC); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int nd_region_remove(struct device *dev) |
| 98 | { |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 99 | struct nd_region *nd_region = to_nd_region(dev); |
| 100 | |
Dan Williams | a8f7202 | 2016-07-09 16:46:29 -0700 | [diff] [blame] | 101 | device_for_each_child(dev, NULL, child_unregister); |
| 102 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 103 | /* flush attribute readers and disable */ |
| 104 | nvdimm_bus_lock(dev); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 105 | nd_region->ns_seed = NULL; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 106 | nd_region->btt_seed = NULL; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 107 | nd_region->pfn_seed = NULL; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 108 | nd_region->dax_seed = NULL; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 109 | dev_set_drvdata(dev, NULL); |
| 110 | nvdimm_bus_unlock(dev); |
| 111 | |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame^] | 112 | /* |
| 113 | * Note, this assumes device_lock() context to not race |
| 114 | * nd_region_notify() |
| 115 | */ |
| 116 | sysfs_put(nd_region->bb_state); |
| 117 | nd_region->bb_state = NULL; |
| 118 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 122 | static int child_notify(struct device *dev, void *data) |
| 123 | { |
| 124 | nd_device_notify(dev, *(enum nvdimm_event *) data); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static void nd_region_notify(struct device *dev, enum nvdimm_event event) |
| 129 | { |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 130 | if (event == NVDIMM_REVALIDATE_POISON) { |
| 131 | struct nd_region *nd_region = to_nd_region(dev); |
| 132 | struct resource res; |
| 133 | |
| 134 | if (is_nd_pmem(&nd_region->dev)) { |
| 135 | res.start = nd_region->ndr_start; |
| 136 | res.end = nd_region->ndr_start + |
| 137 | nd_region->ndr_size - 1; |
| 138 | nvdimm_badblocks_populate(nd_region, |
| 139 | &nd_region->bb, &res); |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 140 | if (nd_region->bb_state) |
| 141 | sysfs_notify_dirent(nd_region->bb_state); |
Dave Jiang | 6a6bef9 | 2017-04-07 15:33:20 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 144 | device_for_each_child(dev, &event, child_notify); |
| 145 | } |
| 146 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 147 | static struct nd_device_driver nd_region_driver = { |
| 148 | .probe = nd_region_probe, |
| 149 | .remove = nd_region_remove, |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 150 | .notify = nd_region_notify, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 151 | .drv = { |
| 152 | .name = "nd_region", |
| 153 | }, |
| 154 | .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM, |
| 155 | }; |
| 156 | |
| 157 | int __init nd_region_init(void) |
| 158 | { |
| 159 | return nd_driver_register(&nd_region_driver); |
| 160 | } |
| 161 | |
| 162 | void nd_region_exit(void) |
| 163 | { |
| 164 | driver_unregister(&nd_region_driver.drv); |
| 165 | } |
| 166 | |
| 167 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM); |
| 168 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK); |