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> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/module.h> |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 16 | #include <linux/blkdev.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 17 | #include <linux/device.h> |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 18 | #include <linux/ctype.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 19 | #include <linux/ndctl.h> |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include "nd-core.h" |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 23 | #include "nd.h" |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 24 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 25 | LIST_HEAD(nvdimm_bus_list); |
| 26 | DEFINE_MUTEX(nvdimm_bus_list_mutex); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 27 | static DEFINE_IDA(nd_ida); |
| 28 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 29 | void nvdimm_bus_lock(struct device *dev) |
| 30 | { |
| 31 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 32 | |
| 33 | if (!nvdimm_bus) |
| 34 | return; |
| 35 | mutex_lock(&nvdimm_bus->reconfig_mutex); |
| 36 | } |
| 37 | EXPORT_SYMBOL(nvdimm_bus_lock); |
| 38 | |
| 39 | void nvdimm_bus_unlock(struct device *dev) |
| 40 | { |
| 41 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 42 | |
| 43 | if (!nvdimm_bus) |
| 44 | return; |
| 45 | mutex_unlock(&nvdimm_bus->reconfig_mutex); |
| 46 | } |
| 47 | EXPORT_SYMBOL(nvdimm_bus_unlock); |
| 48 | |
| 49 | bool is_nvdimm_bus_locked(struct device *dev) |
| 50 | { |
| 51 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 52 | |
| 53 | if (!nvdimm_bus) |
| 54 | return false; |
| 55 | return mutex_is_locked(&nvdimm_bus->reconfig_mutex); |
| 56 | } |
| 57 | EXPORT_SYMBOL(is_nvdimm_bus_locked); |
| 58 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 59 | u64 nd_fletcher64(void *addr, size_t len, bool le) |
| 60 | { |
| 61 | u32 *buf = addr; |
| 62 | u32 lo32 = 0; |
| 63 | u64 hi32 = 0; |
| 64 | int i; |
| 65 | |
| 66 | for (i = 0; i < len / sizeof(u32); i++) { |
| 67 | lo32 += le ? le32_to_cpu((__le32) buf[i]) : buf[i]; |
| 68 | hi32 += lo32; |
| 69 | } |
| 70 | |
| 71 | return hi32 << 32 | lo32; |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(nd_fletcher64); |
| 74 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 75 | static void nvdimm_bus_release(struct device *dev) |
| 76 | { |
| 77 | struct nvdimm_bus *nvdimm_bus; |
| 78 | |
| 79 | nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); |
| 80 | ida_simple_remove(&nd_ida, nvdimm_bus->id); |
| 81 | kfree(nvdimm_bus); |
| 82 | } |
| 83 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 84 | struct nvdimm_bus *to_nvdimm_bus(struct device *dev) |
| 85 | { |
| 86 | struct nvdimm_bus *nvdimm_bus; |
| 87 | |
| 88 | nvdimm_bus = container_of(dev, struct nvdimm_bus, dev); |
| 89 | WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release); |
| 90 | return nvdimm_bus; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(to_nvdimm_bus); |
| 93 | |
| 94 | struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus) |
| 95 | { |
| 96 | /* struct nvdimm_bus definition is private to libnvdimm */ |
| 97 | return nvdimm_bus->nd_desc; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(to_nd_desc); |
| 100 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 101 | struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev) |
| 102 | { |
| 103 | struct device *dev; |
| 104 | |
| 105 | for (dev = nd_dev; dev; dev = dev->parent) |
| 106 | if (dev->release == nvdimm_bus_release) |
| 107 | break; |
| 108 | dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n"); |
| 109 | if (dev) |
| 110 | return to_nvdimm_bus(dev); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 114 | static bool is_uuid_sep(char sep) |
| 115 | { |
| 116 | if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0') |
| 117 | return true; |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | static int nd_uuid_parse(struct device *dev, u8 *uuid_out, const char *buf, |
| 122 | size_t len) |
| 123 | { |
| 124 | const char *str = buf; |
| 125 | u8 uuid[16]; |
| 126 | int i; |
| 127 | |
| 128 | for (i = 0; i < 16; i++) { |
| 129 | if (!isxdigit(str[0]) || !isxdigit(str[1])) { |
| 130 | dev_dbg(dev, "%s: pos: %d buf[%zd]: %c buf[%zd]: %c\n", |
| 131 | __func__, i, str - buf, str[0], |
| 132 | str + 1 - buf, str[1]); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | uuid[i] = (hex_to_bin(str[0]) << 4) | hex_to_bin(str[1]); |
| 137 | str += 2; |
| 138 | if (is_uuid_sep(*str)) |
| 139 | str++; |
| 140 | } |
| 141 | |
| 142 | memcpy(uuid_out, uuid, sizeof(uuid)); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * nd_uuid_store: common implementation for writing 'uuid' sysfs attributes |
| 148 | * @dev: container device for the uuid property |
| 149 | * @uuid_out: uuid buffer to replace |
| 150 | * @buf: raw sysfs buffer to parse |
| 151 | * |
| 152 | * Enforce that uuids can only be changed while the device is disabled |
| 153 | * (driver detached) |
| 154 | * LOCKING: expects device_lock() is held on entry |
| 155 | */ |
| 156 | int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf, |
| 157 | size_t len) |
| 158 | { |
| 159 | u8 uuid[16]; |
| 160 | int rc; |
| 161 | |
| 162 | if (dev->driver) |
| 163 | return -EBUSY; |
| 164 | |
| 165 | rc = nd_uuid_parse(dev, uuid, buf, len); |
| 166 | if (rc) |
| 167 | return rc; |
| 168 | |
| 169 | kfree(*uuid_out); |
| 170 | *uuid_out = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); |
| 171 | if (!(*uuid_out)) |
| 172 | return -ENOMEM; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 177 | ssize_t nd_sector_size_show(unsigned long current_lbasize, |
| 178 | const unsigned long *supported, char *buf) |
| 179 | { |
| 180 | ssize_t len = 0; |
| 181 | int i; |
| 182 | |
| 183 | for (i = 0; supported[i]; i++) |
| 184 | if (current_lbasize == supported[i]) |
| 185 | len += sprintf(buf + len, "[%ld] ", supported[i]); |
| 186 | else |
| 187 | len += sprintf(buf + len, "%ld ", supported[i]); |
| 188 | len += sprintf(buf + len, "\n"); |
| 189 | return len; |
| 190 | } |
| 191 | |
| 192 | ssize_t nd_sector_size_store(struct device *dev, const char *buf, |
| 193 | unsigned long *current_lbasize, const unsigned long *supported) |
| 194 | { |
| 195 | unsigned long lbasize; |
| 196 | int rc, i; |
| 197 | |
| 198 | if (dev->driver) |
| 199 | return -EBUSY; |
| 200 | |
| 201 | rc = kstrtoul(buf, 0, &lbasize); |
| 202 | if (rc) |
| 203 | return rc; |
| 204 | |
| 205 | for (i = 0; supported[i]; i++) |
| 206 | if (lbasize == supported[i]) |
| 207 | break; |
| 208 | |
| 209 | if (supported[i]) { |
| 210 | *current_lbasize = lbasize; |
| 211 | return 0; |
| 212 | } else { |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | } |
| 216 | |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 217 | void __nd_iostat_start(struct bio *bio, unsigned long *start) |
| 218 | { |
| 219 | struct gendisk *disk = bio->bi_bdev->bd_disk; |
| 220 | const int rw = bio_data_dir(bio); |
| 221 | int cpu = part_stat_lock(); |
| 222 | |
| 223 | *start = jiffies; |
| 224 | part_round_stats(cpu, &disk->part0); |
| 225 | part_stat_inc(cpu, &disk->part0, ios[rw]); |
| 226 | part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio)); |
| 227 | part_inc_in_flight(&disk->part0, rw); |
| 228 | part_stat_unlock(); |
| 229 | } |
| 230 | EXPORT_SYMBOL(__nd_iostat_start); |
| 231 | |
| 232 | void nd_iostat_end(struct bio *bio, unsigned long start) |
| 233 | { |
| 234 | struct gendisk *disk = bio->bi_bdev->bd_disk; |
| 235 | unsigned long duration = jiffies - start; |
| 236 | const int rw = bio_data_dir(bio); |
| 237 | int cpu = part_stat_lock(); |
| 238 | |
| 239 | part_stat_add(cpu, &disk->part0, ticks[rw], duration); |
| 240 | part_round_stats(cpu, &disk->part0); |
| 241 | part_dec_in_flight(&disk->part0, rw); |
| 242 | part_stat_unlock(); |
| 243 | } |
| 244 | EXPORT_SYMBOL(nd_iostat_end); |
| 245 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 246 | static ssize_t commands_show(struct device *dev, |
| 247 | struct device_attribute *attr, char *buf) |
| 248 | { |
| 249 | int cmd, len = 0; |
| 250 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 251 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 252 | |
| 253 | for_each_set_bit(cmd, &nd_desc->dsm_mask, BITS_PER_LONG) |
| 254 | len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd)); |
| 255 | len += sprintf(buf + len, "\n"); |
| 256 | return len; |
| 257 | } |
| 258 | static DEVICE_ATTR_RO(commands); |
| 259 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 260 | static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus) |
| 261 | { |
| 262 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 263 | struct device *parent = nvdimm_bus->dev.parent; |
| 264 | |
| 265 | if (nd_desc->provider_name) |
| 266 | return nd_desc->provider_name; |
| 267 | else if (parent) |
| 268 | return dev_name(parent); |
| 269 | else |
| 270 | return "unknown"; |
| 271 | } |
| 272 | |
| 273 | static ssize_t provider_show(struct device *dev, |
| 274 | struct device_attribute *attr, char *buf) |
| 275 | { |
| 276 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 277 | |
| 278 | return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus)); |
| 279 | } |
| 280 | static DEVICE_ATTR_RO(provider); |
| 281 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 282 | static int flush_namespaces(struct device *dev, void *data) |
| 283 | { |
| 284 | device_lock(dev); |
| 285 | device_unlock(dev); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int flush_regions_dimms(struct device *dev, void *data) |
| 290 | { |
| 291 | device_lock(dev); |
| 292 | device_unlock(dev); |
| 293 | device_for_each_child(dev, NULL, flush_namespaces); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static ssize_t wait_probe_show(struct device *dev, |
| 298 | struct device_attribute *attr, char *buf) |
| 299 | { |
| 300 | nd_synchronize(); |
| 301 | device_for_each_child(dev, NULL, flush_regions_dimms); |
| 302 | return sprintf(buf, "1\n"); |
| 303 | } |
| 304 | static DEVICE_ATTR_RO(wait_probe); |
| 305 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 306 | static struct attribute *nvdimm_bus_attributes[] = { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 307 | &dev_attr_commands.attr, |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 308 | &dev_attr_wait_probe.attr, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 309 | &dev_attr_provider.attr, |
| 310 | NULL, |
| 311 | }; |
| 312 | |
| 313 | struct attribute_group nvdimm_bus_attribute_group = { |
| 314 | .attrs = nvdimm_bus_attributes, |
| 315 | }; |
| 316 | EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group); |
| 317 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 318 | struct nvdimm_bus *__nvdimm_bus_register(struct device *parent, |
| 319 | struct nvdimm_bus_descriptor *nd_desc, struct module *module) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 320 | { |
| 321 | struct nvdimm_bus *nvdimm_bus; |
| 322 | int rc; |
| 323 | |
| 324 | nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL); |
| 325 | if (!nvdimm_bus) |
| 326 | return NULL; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 327 | INIT_LIST_HEAD(&nvdimm_bus->list); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame^] | 328 | INIT_LIST_HEAD(&nvdimm_bus->poison_list); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 329 | init_waitqueue_head(&nvdimm_bus->probe_wait); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 330 | nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 331 | mutex_init(&nvdimm_bus->reconfig_mutex); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 332 | if (nvdimm_bus->id < 0) { |
| 333 | kfree(nvdimm_bus); |
| 334 | return NULL; |
| 335 | } |
| 336 | nvdimm_bus->nd_desc = nd_desc; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 337 | nvdimm_bus->module = module; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 338 | nvdimm_bus->dev.parent = parent; |
| 339 | nvdimm_bus->dev.release = nvdimm_bus_release; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 340 | nvdimm_bus->dev.groups = nd_desc->attr_groups; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 341 | dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id); |
| 342 | rc = device_register(&nvdimm_bus->dev); |
| 343 | if (rc) { |
| 344 | dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 345 | goto err; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 348 | rc = nvdimm_bus_create_ndctl(nvdimm_bus); |
| 349 | if (rc) |
| 350 | goto err; |
| 351 | |
| 352 | mutex_lock(&nvdimm_bus_list_mutex); |
| 353 | list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list); |
| 354 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 355 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 356 | return nvdimm_bus; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 357 | err: |
| 358 | put_device(&nvdimm_bus->dev); |
| 359 | return NULL; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 360 | } |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 361 | EXPORT_SYMBOL_GPL(__nvdimm_bus_register); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 362 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame^] | 363 | /** |
| 364 | * __add_badblock_range() - Convert a physical address range to bad sectors |
| 365 | * @disk: the disk associated with the namespace |
| 366 | * @ns_offset: namespace offset where the error range begins (in bytes) |
| 367 | * @len: number of bytes of poison to be added |
| 368 | * |
| 369 | * This assumes that the range provided with (ns_offset, len) is within |
| 370 | * the bounds of physical addresses for this namespace, i.e. lies in the |
| 371 | * interval [ns_start, ns_start + ns_size) |
| 372 | */ |
| 373 | static int __add_badblock_range(struct gendisk *disk, u64 ns_offset, u64 len) |
| 374 | { |
| 375 | unsigned int sector_size = queue_logical_block_size(disk->queue); |
| 376 | sector_t start_sector; |
| 377 | u64 num_sectors; |
| 378 | u32 rem; |
| 379 | int rc; |
| 380 | |
| 381 | start_sector = div_u64(ns_offset, sector_size); |
| 382 | num_sectors = div_u64_rem(len, sector_size, &rem); |
| 383 | if (rem) |
| 384 | num_sectors++; |
| 385 | |
| 386 | if (!disk->bb) { |
| 387 | rc = disk_alloc_badblocks(disk); |
| 388 | if (rc) |
| 389 | return rc; |
| 390 | } |
| 391 | |
| 392 | if (unlikely(num_sectors > (u64)INT_MAX)) { |
| 393 | u64 remaining = num_sectors; |
| 394 | sector_t s = start_sector; |
| 395 | |
| 396 | while (remaining) { |
| 397 | int done = min_t(u64, remaining, INT_MAX); |
| 398 | |
| 399 | rc = disk_set_badblocks(disk, s, done); |
| 400 | if (rc) |
| 401 | return rc; |
| 402 | remaining -= done; |
| 403 | s += done; |
| 404 | } |
| 405 | return 0; |
| 406 | } else |
| 407 | return disk_set_badblocks(disk, start_sector, num_sectors); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * nvdimm_namespace_add_poison() - Convert a list of poison ranges to badblocks |
| 412 | * @disk: the gendisk associated with the namespace where badblocks |
| 413 | * will be stored |
| 414 | * @offset: offset at the start of the namespace before 'sector 0' |
| 415 | * @ndns: the namespace containing poison ranges |
| 416 | * |
| 417 | * The poison list generated during NFIT initialization may contain multiple, |
| 418 | * possibly overlapping ranges in the SPA (System Physical Address) space. |
| 419 | * Compare each of these ranges to the namespace currently being initialized, |
| 420 | * and add badblocks to the gendisk for all matching sub-ranges |
| 421 | * |
| 422 | * Return: |
| 423 | * 0 - Success |
| 424 | */ |
| 425 | int nvdimm_namespace_add_poison(struct gendisk *disk, resource_size_t offset, |
| 426 | struct nd_namespace_common *ndns) |
| 427 | { |
| 428 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 429 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); |
| 430 | struct nvdimm_bus *nvdimm_bus; |
| 431 | struct list_head *poison_list; |
| 432 | u64 ns_start, ns_end, ns_size; |
| 433 | struct nd_poison *pl; |
| 434 | int rc; |
| 435 | |
| 436 | ns_size = nvdimm_namespace_capacity(ndns) - offset; |
| 437 | ns_start = nsio->res.start + offset; |
| 438 | ns_end = nsio->res.end; |
| 439 | |
| 440 | nvdimm_bus = to_nvdimm_bus(nd_region->dev.parent); |
| 441 | poison_list = &nvdimm_bus->poison_list; |
| 442 | if (list_empty(poison_list)) |
| 443 | return 0; |
| 444 | |
| 445 | list_for_each_entry(pl, poison_list, list) { |
| 446 | u64 pl_end = pl->start + pl->length - 1; |
| 447 | |
| 448 | /* Discard intervals with no intersection */ |
| 449 | if (pl_end < ns_start) |
| 450 | continue; |
| 451 | if (pl->start > ns_end) |
| 452 | continue; |
| 453 | /* Deal with any overlap after start of the namespace */ |
| 454 | if (pl->start >= ns_start) { |
| 455 | u64 start = pl->start; |
| 456 | u64 len; |
| 457 | |
| 458 | if (pl_end <= ns_end) |
| 459 | len = pl->length; |
| 460 | else |
| 461 | len = ns_start + ns_size - pl->start; |
| 462 | |
| 463 | rc = __add_badblock_range(disk, start - ns_start, len); |
| 464 | if (rc) |
| 465 | return rc; |
| 466 | dev_info(&nvdimm_bus->dev, |
| 467 | "Found a poison range (0x%llx, 0x%llx)\n", |
| 468 | start, len); |
| 469 | continue; |
| 470 | } |
| 471 | /* Deal with overlap for poison starting before the namespace */ |
| 472 | if (pl->start < ns_start) { |
| 473 | u64 len; |
| 474 | |
| 475 | if (pl_end < ns_end) |
| 476 | len = pl->start + pl->length - ns_start; |
| 477 | else |
| 478 | len = ns_size; |
| 479 | |
| 480 | rc = __add_badblock_range(disk, 0, len); |
| 481 | if (rc) |
| 482 | return rc; |
| 483 | dev_info(&nvdimm_bus->dev, |
| 484 | "Found a poison range (0x%llx, 0x%llx)\n", |
| 485 | pl->start, len); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | EXPORT_SYMBOL_GPL(nvdimm_namespace_add_poison); |
| 492 | |
| 493 | static int __add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
| 494 | { |
| 495 | struct nd_poison *pl; |
| 496 | |
| 497 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); |
| 498 | if (!pl) |
| 499 | return -ENOMEM; |
| 500 | |
| 501 | pl->start = addr; |
| 502 | pl->length = length; |
| 503 | list_add_tail(&pl->list, &nvdimm_bus->poison_list); |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | int nvdimm_bus_add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
| 509 | { |
| 510 | struct nd_poison *pl; |
| 511 | |
| 512 | if (list_empty(&nvdimm_bus->poison_list)) |
| 513 | return __add_poison(nvdimm_bus, addr, length); |
| 514 | |
| 515 | /* |
| 516 | * There is a chance this is a duplicate, check for those first. |
| 517 | * This will be the common case as ARS_STATUS returns all known |
| 518 | * errors in the SPA space, and we can't query it per region |
| 519 | */ |
| 520 | list_for_each_entry(pl, &nvdimm_bus->poison_list, list) |
| 521 | if (pl->start == addr) { |
| 522 | /* If length has changed, update this list entry */ |
| 523 | if (pl->length != length) |
| 524 | pl->length = length; |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * If not a duplicate or a simple length update, add the entry as is, |
| 530 | * as any overlapping ranges will get resolved when the list is consumed |
| 531 | * and converted to badblocks |
| 532 | */ |
| 533 | return __add_poison(nvdimm_bus, addr, length); |
| 534 | } |
| 535 | EXPORT_SYMBOL_GPL(nvdimm_bus_add_poison); |
| 536 | |
| 537 | static void free_poison_list(struct list_head *poison_list) |
| 538 | { |
| 539 | struct nd_poison *pl, *next; |
| 540 | |
| 541 | list_for_each_entry_safe(pl, next, poison_list, list) { |
| 542 | list_del(&pl->list); |
| 543 | kfree(pl); |
| 544 | } |
| 545 | list_del_init(poison_list); |
| 546 | } |
| 547 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 548 | static int child_unregister(struct device *dev, void *data) |
| 549 | { |
| 550 | /* |
| 551 | * the singular ndctl class device per bus needs to be |
| 552 | * "device_destroy"ed, so skip it here |
| 553 | * |
| 554 | * i.e. remove classless children |
| 555 | */ |
| 556 | if (dev->class) |
| 557 | /* pass */; |
| 558 | else |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 559 | nd_device_unregister(dev, ND_SYNC); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 563 | void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus) |
| 564 | { |
| 565 | if (!nvdimm_bus) |
| 566 | return; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 567 | |
| 568 | mutex_lock(&nvdimm_bus_list_mutex); |
| 569 | list_del_init(&nvdimm_bus->list); |
| 570 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 571 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 572 | nd_synchronize(); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 573 | device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame^] | 574 | free_poison_list(&nvdimm_bus->poison_list); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 575 | nvdimm_bus_destroy_ndctl(nvdimm_bus); |
| 576 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 577 | device_unregister(&nvdimm_bus->dev); |
| 578 | } |
| 579 | EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); |
| 580 | |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 581 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 582 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) |
| 583 | { |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 584 | struct blk_integrity bi; |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 585 | |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 586 | if (meta_size == 0) |
| 587 | return 0; |
| 588 | |
Dan Williams | 4125a09 | 2015-10-21 13:20:29 -0400 | [diff] [blame] | 589 | bi.profile = NULL; |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 590 | bi.tuple_size = meta_size; |
| 591 | bi.tag_size = meta_size; |
| 592 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 593 | blk_integrity_register(disk, &bi); |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 594 | blk_queue_max_integrity_segments(disk->queue, 1); |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | EXPORT_SYMBOL(nd_integrity_init); |
| 599 | |
| 600 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
| 601 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) |
| 602 | { |
| 603 | return 0; |
| 604 | } |
| 605 | EXPORT_SYMBOL(nd_integrity_init); |
| 606 | |
| 607 | #endif |
| 608 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 609 | static __init int libnvdimm_init(void) |
| 610 | { |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 611 | int rc; |
| 612 | |
| 613 | rc = nvdimm_bus_init(); |
| 614 | if (rc) |
| 615 | return rc; |
| 616 | rc = nvdimm_init(); |
| 617 | if (rc) |
| 618 | goto err_dimm; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 619 | rc = nd_region_init(); |
| 620 | if (rc) |
| 621 | goto err_region; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 622 | return 0; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 623 | err_region: |
| 624 | nvdimm_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 625 | err_dimm: |
| 626 | nvdimm_bus_exit(); |
| 627 | return rc; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static __exit void libnvdimm_exit(void) |
| 631 | { |
| 632 | WARN_ON(!list_empty(&nvdimm_bus_list)); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 633 | nd_region_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 634 | nvdimm_exit(); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 635 | nvdimm_bus_exit(); |
| 636 | } |
| 637 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 638 | MODULE_LICENSE("GPL v2"); |
| 639 | MODULE_AUTHOR("Intel Corporation"); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 640 | subsys_initcall(libnvdimm_init); |
| 641 | module_exit(libnvdimm_exit); |