Dan Williams | e145574 | 2015-07-30 17:57:47 -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 | */ |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/sizes.h> |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 15 | #include <linux/pmem.h> |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 16 | #include "nd-core.h" |
Dan Williams | f2b6125 | 2017-05-29 23:00:34 -0700 | [diff] [blame^] | 17 | #include "pmem.h" |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 18 | #include "pfn.h" |
| 19 | #include "btt.h" |
| 20 | #include "nd.h" |
| 21 | |
| 22 | void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns) |
| 23 | { |
| 24 | struct nd_namespace_common *ndns = *_ndns; |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 25 | struct nvdimm_bus *nvdimm_bus; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 26 | |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 27 | if (!ndns) |
| 28 | return; |
| 29 | |
| 30 | nvdimm_bus = walk_to_nvdimm_bus(&ndns->dev); |
| 31 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
Dan Williams | 9cf8bd5 | 2016-12-15 20:04:31 -0800 | [diff] [blame] | 32 | dev_WARN_ONCE(dev, ndns->claim != dev, "%s: invalid claim\n", __func__); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 33 | ndns->claim = NULL; |
| 34 | *_ndns = NULL; |
| 35 | put_device(&ndns->dev); |
| 36 | } |
| 37 | |
| 38 | void nd_detach_ndns(struct device *dev, |
| 39 | struct nd_namespace_common **_ndns) |
| 40 | { |
| 41 | struct nd_namespace_common *ndns = *_ndns; |
| 42 | |
| 43 | if (!ndns) |
| 44 | return; |
| 45 | get_device(&ndns->dev); |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 46 | nvdimm_bus_lock(&ndns->dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 47 | __nd_detach_ndns(dev, _ndns); |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 48 | nvdimm_bus_unlock(&ndns->dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 49 | put_device(&ndns->dev); |
| 50 | } |
| 51 | |
| 52 | bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach, |
| 53 | struct nd_namespace_common **_ndns) |
| 54 | { |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 55 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&attach->dev); |
| 56 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 57 | if (attach->claim) |
| 58 | return false; |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 59 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
Dan Williams | 9cf8bd5 | 2016-12-15 20:04:31 -0800 | [diff] [blame] | 60 | dev_WARN_ONCE(dev, *_ndns, "%s: invalid claim\n", __func__); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 61 | attach->claim = dev; |
| 62 | *_ndns = attach; |
| 63 | get_device(&attach->dev); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach, |
| 68 | struct nd_namespace_common **_ndns) |
| 69 | { |
| 70 | bool claimed; |
| 71 | |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 72 | nvdimm_bus_lock(&attach->dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 73 | claimed = __nd_attach_ndns(dev, attach, _ndns); |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 74 | nvdimm_bus_unlock(&attach->dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 75 | return claimed; |
| 76 | } |
| 77 | |
| 78 | static int namespace_match(struct device *dev, void *data) |
| 79 | { |
| 80 | char *name = data; |
| 81 | |
| 82 | return strcmp(name, dev_name(dev)) == 0; |
| 83 | } |
| 84 | |
| 85 | static bool is_idle(struct device *dev, struct nd_namespace_common *ndns) |
| 86 | { |
| 87 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 88 | struct device *seed = NULL; |
| 89 | |
| 90 | if (is_nd_btt(dev)) |
| 91 | seed = nd_region->btt_seed; |
| 92 | else if (is_nd_pfn(dev)) |
| 93 | seed = nd_region->pfn_seed; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 94 | else if (is_nd_dax(dev)) |
| 95 | seed = nd_region->dax_seed; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 96 | |
| 97 | if (seed == dev || ndns || dev->driver) |
| 98 | return false; |
| 99 | return true; |
| 100 | } |
| 101 | |
Dan Williams | 03dca34 | 2016-05-21 12:22:41 -0700 | [diff] [blame] | 102 | struct nd_pfn *to_nd_pfn_safe(struct device *dev) |
| 103 | { |
| 104 | /* |
| 105 | * pfn device attributes are re-used by dax device instances, so we |
| 106 | * need to be careful to correct device-to-nd_pfn conversion. |
| 107 | */ |
| 108 | if (is_nd_pfn(dev)) |
| 109 | return to_nd_pfn(dev); |
| 110 | |
| 111 | if (is_nd_dax(dev)) { |
| 112 | struct nd_dax *nd_dax = to_nd_dax(dev); |
| 113 | |
| 114 | return &nd_dax->nd_pfn; |
| 115 | } |
| 116 | |
| 117 | WARN_ON(1); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 121 | static void nd_detach_and_reset(struct device *dev, |
| 122 | struct nd_namespace_common **_ndns) |
| 123 | { |
| 124 | /* detach the namespace and destroy / reset the device */ |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 125 | __nd_detach_ndns(dev, _ndns); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 126 | if (is_idle(dev, *_ndns)) { |
| 127 | nd_device_unregister(dev, ND_ASYNC); |
| 128 | } else if (is_nd_btt(dev)) { |
| 129 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 130 | |
| 131 | nd_btt->lbasize = 0; |
| 132 | kfree(nd_btt->uuid); |
| 133 | nd_btt->uuid = NULL; |
Dan Williams | 03dca34 | 2016-05-21 12:22:41 -0700 | [diff] [blame] | 134 | } else if (is_nd_pfn(dev) || is_nd_dax(dev)) { |
| 135 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 136 | |
| 137 | kfree(nd_pfn->uuid); |
| 138 | nd_pfn->uuid = NULL; |
| 139 | nd_pfn->mode = PFN_MODE_NONE; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | ssize_t nd_namespace_store(struct device *dev, |
| 144 | struct nd_namespace_common **_ndns, const char *buf, |
| 145 | size_t len) |
| 146 | { |
| 147 | struct nd_namespace_common *ndns; |
| 148 | struct device *found; |
| 149 | char *name; |
| 150 | |
| 151 | if (dev->driver) { |
| 152 | dev_dbg(dev, "%s: -EBUSY\n", __func__); |
| 153 | return -EBUSY; |
| 154 | } |
| 155 | |
| 156 | name = kstrndup(buf, len, GFP_KERNEL); |
| 157 | if (!name) |
| 158 | return -ENOMEM; |
| 159 | strim(name); |
| 160 | |
| 161 | if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0) |
| 162 | /* pass */; |
| 163 | else { |
| 164 | len = -EINVAL; |
| 165 | goto out; |
| 166 | } |
| 167 | |
| 168 | ndns = *_ndns; |
| 169 | if (strcmp(name, "") == 0) { |
| 170 | nd_detach_and_reset(dev, _ndns); |
| 171 | goto out; |
| 172 | } else if (ndns) { |
| 173 | dev_dbg(dev, "namespace already set to: %s\n", |
| 174 | dev_name(&ndns->dev)); |
| 175 | len = -EBUSY; |
| 176 | goto out; |
| 177 | } |
| 178 | |
| 179 | found = device_find_child(dev->parent, name, namespace_match); |
| 180 | if (!found) { |
| 181 | dev_dbg(dev, "'%s' not found under %s\n", name, |
| 182 | dev_name(dev->parent)); |
| 183 | len = -ENODEV; |
| 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | ndns = to_ndns(found); |
| 188 | if (__nvdimm_namespace_capacity(ndns) < SZ_16M) { |
| 189 | dev_dbg(dev, "%s too small to host\n", name); |
| 190 | len = -ENXIO; |
| 191 | goto out_attach; |
| 192 | } |
| 193 | |
| 194 | WARN_ON_ONCE(!is_nvdimm_bus_locked(dev)); |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 195 | if (!__nd_attach_ndns(dev, ndns, _ndns)) { |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 196 | dev_dbg(dev, "%s already claimed\n", |
| 197 | dev_name(&ndns->dev)); |
| 198 | len = -EBUSY; |
| 199 | } |
| 200 | |
| 201 | out_attach: |
| 202 | put_device(&ndns->dev); /* from device_find_child */ |
| 203 | out: |
| 204 | kfree(name); |
| 205 | return len; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * nd_sb_checksum: compute checksum for a generic info block |
| 210 | * |
| 211 | * Returns a fletcher64 checksum of everything in the given info block |
| 212 | * except the last field (since that's where the checksum lives). |
| 213 | */ |
| 214 | u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb) |
| 215 | { |
| 216 | u64 sum; |
| 217 | __le64 sum_save; |
| 218 | |
| 219 | BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K); |
| 220 | BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K); |
| 221 | BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K); |
| 222 | |
| 223 | sum_save = nd_gen_sb->checksum; |
| 224 | nd_gen_sb->checksum = 0; |
| 225 | sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1); |
| 226 | nd_gen_sb->checksum = sum_save; |
| 227 | return sum; |
| 228 | } |
| 229 | EXPORT_SYMBOL(nd_sb_checksum); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 230 | |
| 231 | static int nsio_rw_bytes(struct nd_namespace_common *ndns, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 232 | resource_size_t offset, void *buf, size_t size, int rw, |
| 233 | unsigned long flags) |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 234 | { |
| 235 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
Dave Jiang | 82bf103 | 2016-11-11 12:37:36 -0700 | [diff] [blame] | 236 | unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512); |
| 237 | sector_t sector = offset >> 9; |
| 238 | int rc = 0; |
| 239 | |
| 240 | if (unlikely(!size)) |
| 241 | return 0; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 242 | |
| 243 | if (unlikely(offset + size > nsio->size)) { |
| 244 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); |
| 245 | return -EFAULT; |
| 246 | } |
| 247 | |
| 248 | if (rw == READ) { |
Dave Jiang | 82bf103 | 2016-11-11 12:37:36 -0700 | [diff] [blame] | 249 | if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 250 | return -EIO; |
Dan Williams | 6abccd1 | 2017-01-13 14:14:23 -0800 | [diff] [blame] | 251 | return memcpy_mcsafe(buf, nsio->addr + offset, size); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Fabian Frederick | d37806d | 2016-12-04 10:45:13 -0800 | [diff] [blame] | 254 | if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) { |
Dan Williams | a3e9af9 | 2017-05-01 10:00:02 -0700 | [diff] [blame] | 255 | /* |
| 256 | * FIXME: nsio_rw_bytes() may be called from atomic |
| 257 | * context in the btt case and the ACPI DSM path for |
| 258 | * clearing the error takes sleeping locks and allocates |
| 259 | * memory. An explicit error clearing path, and support |
| 260 | * for tracking badblocks in BTT metadata is needed to |
| 261 | * work around this collision. |
| 262 | */ |
| 263 | if (IS_ALIGNED(offset, 512) && IS_ALIGNED(size, 512) |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 264 | && !(flags & NVDIMM_IO_ATOMIC) |
| 265 | && !ndns->claim) { |
Fabian Frederick | d37806d | 2016-12-04 10:45:13 -0800 | [diff] [blame] | 266 | long cleared; |
| 267 | |
Toshi Kani | 97681f9 | 2017-04-25 15:16:51 -0600 | [diff] [blame] | 268 | cleared = nvdimm_clear_poison(&ndns->dev, |
| 269 | nsio->res.start + offset, size); |
Dan Williams | 868f036f | 2016-12-16 08:10:31 -0800 | [diff] [blame] | 270 | if (cleared < size) |
Fabian Frederick | d37806d | 2016-12-04 10:45:13 -0800 | [diff] [blame] | 271 | rc = -EIO; |
Dan Williams | 868f036f | 2016-12-16 08:10:31 -0800 | [diff] [blame] | 272 | if (cleared > 0 && cleared / 512) { |
| 273 | cleared /= 512; |
| 274 | badblocks_clear(&nsio->bb, sector, cleared); |
Fabian Frederick | d37806d | 2016-12-04 10:45:13 -0800 | [diff] [blame] | 275 | } |
Dan Williams | f2b6125 | 2017-05-29 23:00:34 -0700 | [diff] [blame^] | 276 | arch_invalidate_pmem(nsio->addr + offset, size); |
Fabian Frederick | d37806d | 2016-12-04 10:45:13 -0800 | [diff] [blame] | 277 | } else |
| 278 | rc = -EIO; |
| 279 | } |
| 280 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 281 | memcpy_flushcache(nsio->addr + offset, buf, size); |
Fabian Frederick | d37806d | 2016-12-04 10:45:13 -0800 | [diff] [blame] | 282 | nvdimm_flush(to_nd_region(ndns->dev.parent)); |
| 283 | |
Dave Jiang | 82bf103 | 2016-11-11 12:37:36 -0700 | [diff] [blame] | 284 | return rc; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio) |
| 288 | { |
| 289 | struct resource *res = &nsio->res; |
| 290 | struct nd_namespace_common *ndns = &nsio->common; |
| 291 | |
| 292 | nsio->size = resource_size(res); |
| 293 | if (!devm_request_mem_region(dev, res->start, resource_size(res), |
Dan Williams | 450c663 | 2016-11-28 11:15:18 -0800 | [diff] [blame] | 294 | dev_name(&ndns->dev))) { |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 295 | dev_warn(dev, "could not reserve region %pR\n", res); |
| 296 | return -EBUSY; |
| 297 | } |
| 298 | |
| 299 | ndns->rw_bytes = nsio_rw_bytes; |
| 300 | if (devm_init_badblocks(dev, &nsio->bb)) |
| 301 | return -ENOMEM; |
| 302 | nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb, |
| 303 | &nsio->res); |
| 304 | |
| 305 | nsio->addr = devm_memremap(dev, res->start, resource_size(res), |
| 306 | ARCH_MEMREMAP_PMEM); |
Dan Williams | 4258895 | 2016-05-27 13:28:31 -0700 | [diff] [blame] | 307 | |
| 308 | return PTR_ERR_OR_ZERO(nsio->addr); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 309 | } |
| 310 | EXPORT_SYMBOL_GPL(devm_nsio_enable); |
| 311 | |
| 312 | void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio) |
| 313 | { |
| 314 | struct resource *res = &nsio->res; |
| 315 | |
| 316 | devm_memunmap(dev, nsio->addr); |
| 317 | devm_exit_badblocks(dev, &nsio->bb); |
| 318 | devm_release_mem_region(dev, res->start, resource_size(res)); |
| 319 | } |
| 320 | EXPORT_SYMBOL_GPL(devm_nsio_disable); |