Dan Williams | b94d523 | 2015-05-19 22:54:31 -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/libnvdimm.h> |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 14 | #include <linux/badblocks.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
| 16 | #include <linux/module.h> |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 17 | #include <linux/blkdev.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 18 | #include <linux/device.h> |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 19 | #include <linux/ctype.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 20 | #include <linux/ndctl.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 22 | #include <linux/slab.h> |
Dan Williams | 29b9aa0 | 2016-06-06 17:42:38 -0700 | [diff] [blame] | 23 | #include <linux/io.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 24 | #include "nd-core.h" |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 25 | #include "nd.h" |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 26 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 27 | LIST_HEAD(nvdimm_bus_list); |
| 28 | DEFINE_MUTEX(nvdimm_bus_list_mutex); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 29 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 30 | void nvdimm_bus_lock(struct device *dev) |
| 31 | { |
| 32 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 33 | |
| 34 | if (!nvdimm_bus) |
| 35 | return; |
| 36 | mutex_lock(&nvdimm_bus->reconfig_mutex); |
| 37 | } |
| 38 | EXPORT_SYMBOL(nvdimm_bus_lock); |
| 39 | |
| 40 | void nvdimm_bus_unlock(struct device *dev) |
| 41 | { |
| 42 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 43 | |
| 44 | if (!nvdimm_bus) |
| 45 | return; |
| 46 | mutex_unlock(&nvdimm_bus->reconfig_mutex); |
| 47 | } |
| 48 | EXPORT_SYMBOL(nvdimm_bus_unlock); |
| 49 | |
| 50 | bool is_nvdimm_bus_locked(struct device *dev) |
| 51 | { |
| 52 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 53 | |
| 54 | if (!nvdimm_bus) |
| 55 | return false; |
| 56 | return mutex_is_locked(&nvdimm_bus->reconfig_mutex); |
| 57 | } |
| 58 | EXPORT_SYMBOL(is_nvdimm_bus_locked); |
| 59 | |
Dan Williams | 29b9aa0 | 2016-06-06 17:42:38 -0700 | [diff] [blame] | 60 | struct nvdimm_map { |
| 61 | struct nvdimm_bus *nvdimm_bus; |
| 62 | struct list_head list; |
| 63 | resource_size_t offset; |
| 64 | unsigned long flags; |
| 65 | size_t size; |
| 66 | union { |
| 67 | void *mem; |
| 68 | void __iomem *iomem; |
| 69 | }; |
| 70 | struct kref kref; |
| 71 | }; |
| 72 | |
| 73 | static struct nvdimm_map *find_nvdimm_map(struct device *dev, |
| 74 | resource_size_t offset) |
| 75 | { |
| 76 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 77 | struct nvdimm_map *nvdimm_map; |
| 78 | |
| 79 | list_for_each_entry(nvdimm_map, &nvdimm_bus->mapping_list, list) |
| 80 | if (nvdimm_map->offset == offset) |
| 81 | return nvdimm_map; |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | static struct nvdimm_map *alloc_nvdimm_map(struct device *dev, |
| 86 | resource_size_t offset, size_t size, unsigned long flags) |
| 87 | { |
| 88 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 89 | struct nvdimm_map *nvdimm_map; |
| 90 | |
| 91 | nvdimm_map = kzalloc(sizeof(*nvdimm_map), GFP_KERNEL); |
| 92 | if (!nvdimm_map) |
| 93 | return NULL; |
| 94 | |
| 95 | INIT_LIST_HEAD(&nvdimm_map->list); |
| 96 | nvdimm_map->nvdimm_bus = nvdimm_bus; |
| 97 | nvdimm_map->offset = offset; |
| 98 | nvdimm_map->flags = flags; |
| 99 | nvdimm_map->size = size; |
| 100 | kref_init(&nvdimm_map->kref); |
| 101 | |
Dan Williams | ecfb6d8 | 2016-09-21 09:22:33 -0700 | [diff] [blame] | 102 | if (!request_mem_region(offset, size, dev_name(&nvdimm_bus->dev))) { |
| 103 | dev_err(&nvdimm_bus->dev, "failed to request %pa + %zd for %s\n", |
| 104 | &offset, size, dev_name(dev)); |
Dan Williams | 29b9aa0 | 2016-06-06 17:42:38 -0700 | [diff] [blame] | 105 | goto err_request_region; |
Dan Williams | ecfb6d8 | 2016-09-21 09:22:33 -0700 | [diff] [blame] | 106 | } |
Dan Williams | 29b9aa0 | 2016-06-06 17:42:38 -0700 | [diff] [blame] | 107 | |
| 108 | if (flags) |
| 109 | nvdimm_map->mem = memremap(offset, size, flags); |
| 110 | else |
| 111 | nvdimm_map->iomem = ioremap(offset, size); |
| 112 | |
| 113 | if (!nvdimm_map->mem) |
| 114 | goto err_map; |
| 115 | |
| 116 | dev_WARN_ONCE(dev, !is_nvdimm_bus_locked(dev), "%s: bus unlocked!", |
| 117 | __func__); |
| 118 | list_add(&nvdimm_map->list, &nvdimm_bus->mapping_list); |
| 119 | |
| 120 | return nvdimm_map; |
| 121 | |
| 122 | err_map: |
| 123 | release_mem_region(offset, size); |
| 124 | err_request_region: |
| 125 | kfree(nvdimm_map); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | static void nvdimm_map_release(struct kref *kref) |
| 130 | { |
| 131 | struct nvdimm_bus *nvdimm_bus; |
| 132 | struct nvdimm_map *nvdimm_map; |
| 133 | |
| 134 | nvdimm_map = container_of(kref, struct nvdimm_map, kref); |
| 135 | nvdimm_bus = nvdimm_map->nvdimm_bus; |
| 136 | |
| 137 | dev_dbg(&nvdimm_bus->dev, "%s: %pa\n", __func__, &nvdimm_map->offset); |
| 138 | list_del(&nvdimm_map->list); |
| 139 | if (nvdimm_map->flags) |
| 140 | memunmap(nvdimm_map->mem); |
| 141 | else |
| 142 | iounmap(nvdimm_map->iomem); |
| 143 | release_mem_region(nvdimm_map->offset, nvdimm_map->size); |
| 144 | kfree(nvdimm_map); |
| 145 | } |
| 146 | |
| 147 | static void nvdimm_map_put(void *data) |
| 148 | { |
| 149 | struct nvdimm_map *nvdimm_map = data; |
| 150 | struct nvdimm_bus *nvdimm_bus = nvdimm_map->nvdimm_bus; |
| 151 | |
| 152 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 153 | kref_put(&nvdimm_map->kref, nvdimm_map_release); |
| 154 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * devm_nvdimm_memremap - map a resource that is shared across regions |
| 159 | * @dev: device that will own a reference to the shared mapping |
| 160 | * @offset: physical base address of the mapping |
| 161 | * @size: mapping size |
| 162 | * @flags: memremap flags, or, if zero, perform an ioremap instead |
| 163 | */ |
| 164 | void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset, |
| 165 | size_t size, unsigned long flags) |
| 166 | { |
| 167 | struct nvdimm_map *nvdimm_map; |
| 168 | |
| 169 | nvdimm_bus_lock(dev); |
| 170 | nvdimm_map = find_nvdimm_map(dev, offset); |
| 171 | if (!nvdimm_map) |
| 172 | nvdimm_map = alloc_nvdimm_map(dev, offset, size, flags); |
| 173 | else |
| 174 | kref_get(&nvdimm_map->kref); |
| 175 | nvdimm_bus_unlock(dev); |
| 176 | |
Dan Williams | ecfb6d8 | 2016-09-21 09:22:33 -0700 | [diff] [blame] | 177 | if (!nvdimm_map) |
| 178 | return NULL; |
| 179 | |
Dan Williams | 29b9aa0 | 2016-06-06 17:42:38 -0700 | [diff] [blame] | 180 | if (devm_add_action_or_reset(dev, nvdimm_map_put, nvdimm_map)) |
| 181 | return NULL; |
| 182 | |
| 183 | return nvdimm_map->mem; |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(devm_nvdimm_memremap); |
| 186 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 187 | u64 nd_fletcher64(void *addr, size_t len, bool le) |
| 188 | { |
| 189 | u32 *buf = addr; |
| 190 | u32 lo32 = 0; |
| 191 | u64 hi32 = 0; |
| 192 | int i; |
| 193 | |
| 194 | for (i = 0; i < len / sizeof(u32); i++) { |
| 195 | lo32 += le ? le32_to_cpu((__le32) buf[i]) : buf[i]; |
| 196 | hi32 += lo32; |
| 197 | } |
| 198 | |
| 199 | return hi32 << 32 | lo32; |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(nd_fletcher64); |
| 202 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 203 | struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus) |
| 204 | { |
| 205 | /* struct nvdimm_bus definition is private to libnvdimm */ |
| 206 | return nvdimm_bus->nd_desc; |
| 207 | } |
| 208 | EXPORT_SYMBOL_GPL(to_nd_desc); |
| 209 | |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 210 | struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus) |
| 211 | { |
| 212 | /* struct nvdimm_bus definition is private to libnvdimm */ |
| 213 | return &nvdimm_bus->dev; |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(to_nvdimm_bus_dev); |
| 216 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 217 | static bool is_uuid_sep(char sep) |
| 218 | { |
| 219 | if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0') |
| 220 | return true; |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | static int nd_uuid_parse(struct device *dev, u8 *uuid_out, const char *buf, |
| 225 | size_t len) |
| 226 | { |
| 227 | const char *str = buf; |
| 228 | u8 uuid[16]; |
| 229 | int i; |
| 230 | |
| 231 | for (i = 0; i < 16; i++) { |
| 232 | if (!isxdigit(str[0]) || !isxdigit(str[1])) { |
| 233 | dev_dbg(dev, "%s: pos: %d buf[%zd]: %c buf[%zd]: %c\n", |
| 234 | __func__, i, str - buf, str[0], |
| 235 | str + 1 - buf, str[1]); |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | uuid[i] = (hex_to_bin(str[0]) << 4) | hex_to_bin(str[1]); |
| 240 | str += 2; |
| 241 | if (is_uuid_sep(*str)) |
| 242 | str++; |
| 243 | } |
| 244 | |
| 245 | memcpy(uuid_out, uuid, sizeof(uuid)); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * nd_uuid_store: common implementation for writing 'uuid' sysfs attributes |
| 251 | * @dev: container device for the uuid property |
| 252 | * @uuid_out: uuid buffer to replace |
| 253 | * @buf: raw sysfs buffer to parse |
| 254 | * |
| 255 | * Enforce that uuids can only be changed while the device is disabled |
| 256 | * (driver detached) |
| 257 | * LOCKING: expects device_lock() is held on entry |
| 258 | */ |
| 259 | int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf, |
| 260 | size_t len) |
| 261 | { |
| 262 | u8 uuid[16]; |
| 263 | int rc; |
| 264 | |
| 265 | if (dev->driver) |
| 266 | return -EBUSY; |
| 267 | |
| 268 | rc = nd_uuid_parse(dev, uuid, buf, len); |
| 269 | if (rc) |
| 270 | return rc; |
| 271 | |
| 272 | kfree(*uuid_out); |
| 273 | *uuid_out = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); |
| 274 | if (!(*uuid_out)) |
| 275 | return -ENOMEM; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 280 | ssize_t nd_sector_size_show(unsigned long current_lbasize, |
| 281 | const unsigned long *supported, char *buf) |
| 282 | { |
| 283 | ssize_t len = 0; |
| 284 | int i; |
| 285 | |
| 286 | for (i = 0; supported[i]; i++) |
| 287 | if (current_lbasize == supported[i]) |
| 288 | len += sprintf(buf + len, "[%ld] ", supported[i]); |
| 289 | else |
| 290 | len += sprintf(buf + len, "%ld ", supported[i]); |
| 291 | len += sprintf(buf + len, "\n"); |
| 292 | return len; |
| 293 | } |
| 294 | |
| 295 | ssize_t nd_sector_size_store(struct device *dev, const char *buf, |
| 296 | unsigned long *current_lbasize, const unsigned long *supported) |
| 297 | { |
| 298 | unsigned long lbasize; |
| 299 | int rc, i; |
| 300 | |
| 301 | if (dev->driver) |
| 302 | return -EBUSY; |
| 303 | |
| 304 | rc = kstrtoul(buf, 0, &lbasize); |
| 305 | if (rc) |
| 306 | return rc; |
| 307 | |
| 308 | for (i = 0; supported[i]; i++) |
| 309 | if (lbasize == supported[i]) |
| 310 | break; |
| 311 | |
| 312 | if (supported[i]) { |
| 313 | *current_lbasize = lbasize; |
| 314 | return 0; |
| 315 | } else { |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | } |
| 319 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 320 | static ssize_t commands_show(struct device *dev, |
| 321 | struct device_attribute *attr, char *buf) |
| 322 | { |
| 323 | int cmd, len = 0; |
| 324 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 325 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 326 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 327 | for_each_set_bit(cmd, &nd_desc->cmd_mask, BITS_PER_LONG) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 328 | len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd)); |
| 329 | len += sprintf(buf + len, "\n"); |
| 330 | return len; |
| 331 | } |
| 332 | static DEVICE_ATTR_RO(commands); |
| 333 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 334 | static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus) |
| 335 | { |
| 336 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 337 | struct device *parent = nvdimm_bus->dev.parent; |
| 338 | |
| 339 | if (nd_desc->provider_name) |
| 340 | return nd_desc->provider_name; |
| 341 | else if (parent) |
| 342 | return dev_name(parent); |
| 343 | else |
| 344 | return "unknown"; |
| 345 | } |
| 346 | |
| 347 | static ssize_t provider_show(struct device *dev, |
| 348 | struct device_attribute *attr, char *buf) |
| 349 | { |
| 350 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 351 | |
| 352 | return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus)); |
| 353 | } |
| 354 | static DEVICE_ATTR_RO(provider); |
| 355 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 356 | static int flush_namespaces(struct device *dev, void *data) |
| 357 | { |
| 358 | device_lock(dev); |
| 359 | device_unlock(dev); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int flush_regions_dimms(struct device *dev, void *data) |
| 364 | { |
| 365 | device_lock(dev); |
| 366 | device_unlock(dev); |
| 367 | device_for_each_child(dev, NULL, flush_namespaces); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static ssize_t wait_probe_show(struct device *dev, |
| 372 | struct device_attribute *attr, char *buf) |
| 373 | { |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 374 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 375 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 376 | int rc; |
| 377 | |
| 378 | if (nd_desc->flush_probe) { |
| 379 | rc = nd_desc->flush_probe(nd_desc); |
| 380 | if (rc) |
| 381 | return rc; |
| 382 | } |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 383 | nd_synchronize(); |
| 384 | device_for_each_child(dev, NULL, flush_regions_dimms); |
| 385 | return sprintf(buf, "1\n"); |
| 386 | } |
| 387 | static DEVICE_ATTR_RO(wait_probe); |
| 388 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 389 | static struct attribute *nvdimm_bus_attributes[] = { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 390 | &dev_attr_commands.attr, |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 391 | &dev_attr_wait_probe.attr, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 392 | &dev_attr_provider.attr, |
| 393 | NULL, |
| 394 | }; |
| 395 | |
| 396 | struct attribute_group nvdimm_bus_attribute_group = { |
| 397 | .attrs = nvdimm_bus_attributes, |
| 398 | }; |
| 399 | EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group); |
| 400 | |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 401 | static void set_badblock(struct badblocks *bb, sector_t s, int num) |
Dan Williams | 87ba05d | 2016-01-09 07:48:43 -0800 | [diff] [blame] | 402 | { |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 403 | dev_dbg(bb->dev, "Found a poison range (0x%llx, 0x%llx)\n", |
Dan Williams | 87ba05d | 2016-01-09 07:48:43 -0800 | [diff] [blame] | 404 | (u64) s * 512, (u64) num * 512); |
| 405 | /* this isn't an error as the hardware will still throw an exception */ |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 406 | if (badblocks_set(bb, s, num, 1)) |
| 407 | dev_info_once(bb->dev, "%s: failed for sector %llx\n", |
Dan Williams | 87ba05d | 2016-01-09 07:48:43 -0800 | [diff] [blame] | 408 | __func__, (u64) s); |
| 409 | } |
| 410 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 411 | /** |
| 412 | * __add_badblock_range() - Convert a physical address range to bad sectors |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 413 | * @bb: badblocks instance to populate |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 414 | * @ns_offset: namespace offset where the error range begins (in bytes) |
| 415 | * @len: number of bytes of poison to be added |
| 416 | * |
| 417 | * This assumes that the range provided with (ns_offset, len) is within |
| 418 | * the bounds of physical addresses for this namespace, i.e. lies in the |
| 419 | * interval [ns_start, ns_start + ns_size) |
| 420 | */ |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 421 | static void __add_badblock_range(struct badblocks *bb, u64 ns_offset, u64 len) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 422 | { |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 423 | const unsigned int sector_size = 512; |
Toshi Kani | 4e3f070 | 2017-07-07 17:44:26 -0600 | [diff] [blame] | 424 | sector_t start_sector, end_sector; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 425 | u64 num_sectors; |
| 426 | u32 rem; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 427 | |
| 428 | start_sector = div_u64(ns_offset, sector_size); |
Toshi Kani | 4e3f070 | 2017-07-07 17:44:26 -0600 | [diff] [blame] | 429 | end_sector = div_u64_rem(ns_offset + len, sector_size, &rem); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 430 | if (rem) |
Toshi Kani | 4e3f070 | 2017-07-07 17:44:26 -0600 | [diff] [blame] | 431 | end_sector++; |
| 432 | num_sectors = end_sector - start_sector; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 433 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 434 | if (unlikely(num_sectors > (u64)INT_MAX)) { |
| 435 | u64 remaining = num_sectors; |
| 436 | sector_t s = start_sector; |
| 437 | |
| 438 | while (remaining) { |
| 439 | int done = min_t(u64, remaining, INT_MAX); |
| 440 | |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 441 | set_badblock(bb, s, done); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 442 | remaining -= done; |
| 443 | s += done; |
| 444 | } |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 445 | } else |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 446 | set_badblock(bb, start_sector, num_sectors); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 449 | static void badblocks_populate(struct list_head *poison_list, |
| 450 | struct badblocks *bb, const struct resource *res) |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 451 | { |
| 452 | struct nd_poison *pl; |
| 453 | |
| 454 | if (list_empty(poison_list)) |
| 455 | return; |
| 456 | |
| 457 | list_for_each_entry(pl, poison_list, list) { |
| 458 | u64 pl_end = pl->start + pl->length - 1; |
| 459 | |
| 460 | /* Discard intervals with no intersection */ |
| 461 | if (pl_end < res->start) |
| 462 | continue; |
| 463 | if (pl->start > res->end) |
| 464 | continue; |
| 465 | /* Deal with any overlap after start of the namespace */ |
| 466 | if (pl->start >= res->start) { |
| 467 | u64 start = pl->start; |
| 468 | u64 len; |
| 469 | |
| 470 | if (pl_end <= res->end) |
| 471 | len = pl->length; |
| 472 | else |
| 473 | len = res->start + resource_size(res) |
| 474 | - pl->start; |
| 475 | __add_badblock_range(bb, start - res->start, len); |
| 476 | continue; |
| 477 | } |
| 478 | /* Deal with overlap for poison starting before the namespace */ |
| 479 | if (pl->start < res->start) { |
| 480 | u64 len; |
| 481 | |
| 482 | if (pl_end < res->end) |
| 483 | len = pl->start + pl->length - res->start; |
| 484 | else |
| 485 | len = resource_size(res); |
| 486 | __add_badblock_range(bb, 0, len); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 491 | /** |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 492 | * nvdimm_badblocks_populate() - Convert a list of poison ranges to badblocks |
| 493 | * @region: parent region of the range to interrogate |
| 494 | * @bb: badblocks instance to populate |
| 495 | * @res: resource range to consider |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 496 | * |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 497 | * The poison list generated during bus initialization may contain |
| 498 | * multiple, possibly overlapping physical address ranges. Compare each |
| 499 | * of these ranges to the resource range currently being initialized, |
| 500 | * and add badblocks entries for all matching sub-ranges |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 501 | */ |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 502 | void nvdimm_badblocks_populate(struct nd_region *nd_region, |
| 503 | struct badblocks *bb, const struct resource *res) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 504 | { |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 505 | struct nvdimm_bus *nvdimm_bus; |
| 506 | struct list_head *poison_list; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 507 | |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 508 | if (!is_memory(&nd_region->dev)) { |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 509 | dev_WARN_ONCE(&nd_region->dev, 1, |
| 510 | "%s only valid for pmem regions\n", __func__); |
| 511 | return; |
| 512 | } |
| 513 | nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 514 | poison_list = &nvdimm_bus->poison_list; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 515 | |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 516 | nvdimm_bus_lock(&nvdimm_bus->dev); |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 517 | badblocks_populate(poison_list, bb, res); |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 518 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 519 | } |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 520 | EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 521 | |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 522 | static void append_poison_entry(struct nvdimm_bus *nvdimm_bus, |
| 523 | struct nd_poison *pl, u64 addr, u64 length) |
| 524 | { |
| 525 | lockdep_assert_held(&nvdimm_bus->poison_lock); |
| 526 | pl->start = addr; |
| 527 | pl->length = length; |
| 528 | list_add_tail(&pl->list, &nvdimm_bus->poison_list); |
| 529 | } |
| 530 | |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 531 | static int add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length, |
| 532 | gfp_t flags) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 533 | { |
| 534 | struct nd_poison *pl; |
| 535 | |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 536 | pl = kzalloc(sizeof(*pl), flags); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 537 | if (!pl) |
| 538 | return -ENOMEM; |
| 539 | |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 540 | append_poison_entry(nvdimm_bus, pl, addr, length); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 544 | static int bus_add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 545 | { |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 546 | struct nd_poison *pl, *pl_new; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 547 | |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 548 | spin_unlock(&nvdimm_bus->poison_lock); |
| 549 | pl_new = kzalloc(sizeof(*pl_new), GFP_KERNEL); |
| 550 | spin_lock(&nvdimm_bus->poison_lock); |
| 551 | |
| 552 | if (list_empty(&nvdimm_bus->poison_list)) { |
| 553 | if (!pl_new) |
| 554 | return -ENOMEM; |
| 555 | append_poison_entry(nvdimm_bus, pl_new, addr, length); |
| 556 | return 0; |
| 557 | } |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 558 | |
| 559 | /* |
| 560 | * There is a chance this is a duplicate, check for those first. |
| 561 | * This will be the common case as ARS_STATUS returns all known |
| 562 | * errors in the SPA space, and we can't query it per region |
| 563 | */ |
| 564 | list_for_each_entry(pl, &nvdimm_bus->poison_list, list) |
| 565 | if (pl->start == addr) { |
| 566 | /* If length has changed, update this list entry */ |
| 567 | if (pl->length != length) |
| 568 | pl->length = length; |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 569 | kfree(pl_new); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * If not a duplicate or a simple length update, add the entry as is, |
| 575 | * as any overlapping ranges will get resolved when the list is consumed |
| 576 | * and converted to badblocks |
| 577 | */ |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 578 | if (!pl_new) |
| 579 | return -ENOMEM; |
| 580 | append_poison_entry(nvdimm_bus, pl_new, addr, length); |
| 581 | |
| 582 | return 0; |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | int nvdimm_bus_add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
| 586 | { |
| 587 | int rc; |
| 588 | |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 589 | spin_lock(&nvdimm_bus->poison_lock); |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 590 | rc = bus_add_poison(nvdimm_bus, addr, length); |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 591 | spin_unlock(&nvdimm_bus->poison_lock); |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 592 | |
| 593 | return rc; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 594 | } |
| 595 | EXPORT_SYMBOL_GPL(nvdimm_bus_add_poison); |
| 596 | |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 597 | void nvdimm_forget_poison(struct nvdimm_bus *nvdimm_bus, phys_addr_t start, |
Dave Jiang | 006358b | 2017-04-07 15:33:31 -0700 | [diff] [blame] | 598 | unsigned int len) |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 599 | { |
| 600 | struct list_head *poison_list = &nvdimm_bus->poison_list; |
| 601 | u64 clr_end = start + len - 1; |
| 602 | struct nd_poison *pl, *next; |
| 603 | |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 604 | spin_lock(&nvdimm_bus->poison_lock); |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 605 | WARN_ON_ONCE(list_empty(poison_list)); |
| 606 | |
| 607 | /* |
| 608 | * [start, clr_end] is the poison interval being cleared. |
| 609 | * [pl->start, pl_end] is the poison_list entry we're comparing |
| 610 | * the above interval against. The poison list entry may need |
| 611 | * to be modified (update either start or length), deleted, or |
| 612 | * split into two based on the overlap characteristics |
| 613 | */ |
| 614 | |
| 615 | list_for_each_entry_safe(pl, next, poison_list, list) { |
| 616 | u64 pl_end = pl->start + pl->length - 1; |
| 617 | |
| 618 | /* Skip intervals with no intersection */ |
| 619 | if (pl_end < start) |
| 620 | continue; |
| 621 | if (pl->start > clr_end) |
| 622 | continue; |
| 623 | /* Delete completely overlapped poison entries */ |
| 624 | if ((pl->start >= start) && (pl_end <= clr_end)) { |
| 625 | list_del(&pl->list); |
| 626 | kfree(pl); |
| 627 | continue; |
| 628 | } |
| 629 | /* Adjust start point of partially cleared entries */ |
| 630 | if ((start <= pl->start) && (clr_end > pl->start)) { |
| 631 | pl->length -= clr_end - pl->start + 1; |
| 632 | pl->start = clr_end + 1; |
| 633 | continue; |
| 634 | } |
| 635 | /* Adjust pl->length for partial clearing at the tail end */ |
| 636 | if ((pl->start < start) && (pl_end <= clr_end)) { |
| 637 | /* pl->start remains the same */ |
| 638 | pl->length = start - pl->start; |
| 639 | continue; |
| 640 | } |
| 641 | /* |
| 642 | * If clearing in the middle of an entry, we split it into |
| 643 | * two by modifying the current entry to represent one half of |
| 644 | * the split, and adding a new entry for the second half. |
| 645 | */ |
| 646 | if ((pl->start < start) && (pl_end > clr_end)) { |
| 647 | u64 new_start = clr_end + 1; |
| 648 | u64 new_len = pl_end - new_start + 1; |
| 649 | |
| 650 | /* Add new entry covering the right half */ |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 651 | add_poison(nvdimm_bus, new_start, new_len, GFP_NOWAIT); |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 652 | /* Adjust this entry to cover the left half */ |
| 653 | pl->length = start - pl->start; |
| 654 | continue; |
| 655 | } |
| 656 | } |
Dave Jiang | b3b454f | 2017-04-13 14:25:17 -0700 | [diff] [blame] | 657 | spin_unlock(&nvdimm_bus->poison_lock); |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 658 | } |
Dave Jiang | 006358b | 2017-04-07 15:33:31 -0700 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(nvdimm_forget_poison); |
Vishal Verma | e046114 | 2016-09-30 17:19:31 -0600 | [diff] [blame] | 660 | |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 661 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 662 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) |
| 663 | { |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 664 | struct blk_integrity bi; |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 665 | |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 666 | if (meta_size == 0) |
| 667 | return 0; |
| 668 | |
Johannes Thumshirn | 8729bde | 2016-06-23 11:52:04 +0200 | [diff] [blame] | 669 | memset(&bi, 0, sizeof(bi)); |
| 670 | |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 671 | bi.tuple_size = meta_size; |
| 672 | bi.tag_size = meta_size; |
| 673 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 674 | blk_integrity_register(disk, &bi); |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 675 | blk_queue_max_integrity_segments(disk->queue, 1); |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | EXPORT_SYMBOL(nd_integrity_init); |
| 680 | |
| 681 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
| 682 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) |
| 683 | { |
| 684 | return 0; |
| 685 | } |
| 686 | EXPORT_SYMBOL(nd_integrity_init); |
| 687 | |
| 688 | #endif |
| 689 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 690 | static __init int libnvdimm_init(void) |
| 691 | { |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 692 | int rc; |
| 693 | |
| 694 | rc = nvdimm_bus_init(); |
| 695 | if (rc) |
| 696 | return rc; |
| 697 | rc = nvdimm_init(); |
| 698 | if (rc) |
| 699 | goto err_dimm; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 700 | rc = nd_region_init(); |
| 701 | if (rc) |
| 702 | goto err_region; |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 703 | |
| 704 | nd_label_init(); |
| 705 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 706 | return 0; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 707 | err_region: |
| 708 | nvdimm_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 709 | err_dimm: |
| 710 | nvdimm_bus_exit(); |
| 711 | return rc; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static __exit void libnvdimm_exit(void) |
| 715 | { |
| 716 | WARN_ON(!list_empty(&nvdimm_bus_list)); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 717 | nd_region_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 718 | nvdimm_exit(); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 719 | nvdimm_bus_exit(); |
Dan Williams | b354aba | 2016-05-17 20:24:16 -0700 | [diff] [blame] | 720 | nd_region_devs_exit(); |
| 721 | nvdimm_devs_exit(); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 722 | } |
| 723 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 724 | MODULE_LICENSE("GPL v2"); |
| 725 | MODULE_AUTHOR("Intel Corporation"); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 726 | subsys_initcall(libnvdimm_init); |
| 727 | module_exit(libnvdimm_exit); |