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 | |
| 102 | if (!request_mem_region(offset, size, dev_name(&nvdimm_bus->dev))) |
| 103 | goto err_request_region; |
| 104 | |
| 105 | if (flags) |
| 106 | nvdimm_map->mem = memremap(offset, size, flags); |
| 107 | else |
| 108 | nvdimm_map->iomem = ioremap(offset, size); |
| 109 | |
| 110 | if (!nvdimm_map->mem) |
| 111 | goto err_map; |
| 112 | |
| 113 | dev_WARN_ONCE(dev, !is_nvdimm_bus_locked(dev), "%s: bus unlocked!", |
| 114 | __func__); |
| 115 | list_add(&nvdimm_map->list, &nvdimm_bus->mapping_list); |
| 116 | |
| 117 | return nvdimm_map; |
| 118 | |
| 119 | err_map: |
| 120 | release_mem_region(offset, size); |
| 121 | err_request_region: |
| 122 | kfree(nvdimm_map); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | static void nvdimm_map_release(struct kref *kref) |
| 127 | { |
| 128 | struct nvdimm_bus *nvdimm_bus; |
| 129 | struct nvdimm_map *nvdimm_map; |
| 130 | |
| 131 | nvdimm_map = container_of(kref, struct nvdimm_map, kref); |
| 132 | nvdimm_bus = nvdimm_map->nvdimm_bus; |
| 133 | |
| 134 | dev_dbg(&nvdimm_bus->dev, "%s: %pa\n", __func__, &nvdimm_map->offset); |
| 135 | list_del(&nvdimm_map->list); |
| 136 | if (nvdimm_map->flags) |
| 137 | memunmap(nvdimm_map->mem); |
| 138 | else |
| 139 | iounmap(nvdimm_map->iomem); |
| 140 | release_mem_region(nvdimm_map->offset, nvdimm_map->size); |
| 141 | kfree(nvdimm_map); |
| 142 | } |
| 143 | |
| 144 | static void nvdimm_map_put(void *data) |
| 145 | { |
| 146 | struct nvdimm_map *nvdimm_map = data; |
| 147 | struct nvdimm_bus *nvdimm_bus = nvdimm_map->nvdimm_bus; |
| 148 | |
| 149 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 150 | kref_put(&nvdimm_map->kref, nvdimm_map_release); |
| 151 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * devm_nvdimm_memremap - map a resource that is shared across regions |
| 156 | * @dev: device that will own a reference to the shared mapping |
| 157 | * @offset: physical base address of the mapping |
| 158 | * @size: mapping size |
| 159 | * @flags: memremap flags, or, if zero, perform an ioremap instead |
| 160 | */ |
| 161 | void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset, |
| 162 | size_t size, unsigned long flags) |
| 163 | { |
| 164 | struct nvdimm_map *nvdimm_map; |
| 165 | |
| 166 | nvdimm_bus_lock(dev); |
| 167 | nvdimm_map = find_nvdimm_map(dev, offset); |
| 168 | if (!nvdimm_map) |
| 169 | nvdimm_map = alloc_nvdimm_map(dev, offset, size, flags); |
| 170 | else |
| 171 | kref_get(&nvdimm_map->kref); |
| 172 | nvdimm_bus_unlock(dev); |
| 173 | |
| 174 | if (devm_add_action_or_reset(dev, nvdimm_map_put, nvdimm_map)) |
| 175 | return NULL; |
| 176 | |
| 177 | return nvdimm_map->mem; |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(devm_nvdimm_memremap); |
| 180 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 181 | u64 nd_fletcher64(void *addr, size_t len, bool le) |
| 182 | { |
| 183 | u32 *buf = addr; |
| 184 | u32 lo32 = 0; |
| 185 | u64 hi32 = 0; |
| 186 | int i; |
| 187 | |
| 188 | for (i = 0; i < len / sizeof(u32); i++) { |
| 189 | lo32 += le ? le32_to_cpu((__le32) buf[i]) : buf[i]; |
| 190 | hi32 += lo32; |
| 191 | } |
| 192 | |
| 193 | return hi32 << 32 | lo32; |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(nd_fletcher64); |
| 196 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 197 | struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus) |
| 198 | { |
| 199 | /* struct nvdimm_bus definition is private to libnvdimm */ |
| 200 | return nvdimm_bus->nd_desc; |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(to_nd_desc); |
| 203 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 204 | static bool is_uuid_sep(char sep) |
| 205 | { |
| 206 | if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0') |
| 207 | return true; |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | static int nd_uuid_parse(struct device *dev, u8 *uuid_out, const char *buf, |
| 212 | size_t len) |
| 213 | { |
| 214 | const char *str = buf; |
| 215 | u8 uuid[16]; |
| 216 | int i; |
| 217 | |
| 218 | for (i = 0; i < 16; i++) { |
| 219 | if (!isxdigit(str[0]) || !isxdigit(str[1])) { |
| 220 | dev_dbg(dev, "%s: pos: %d buf[%zd]: %c buf[%zd]: %c\n", |
| 221 | __func__, i, str - buf, str[0], |
| 222 | str + 1 - buf, str[1]); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | uuid[i] = (hex_to_bin(str[0]) << 4) | hex_to_bin(str[1]); |
| 227 | str += 2; |
| 228 | if (is_uuid_sep(*str)) |
| 229 | str++; |
| 230 | } |
| 231 | |
| 232 | memcpy(uuid_out, uuid, sizeof(uuid)); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * nd_uuid_store: common implementation for writing 'uuid' sysfs attributes |
| 238 | * @dev: container device for the uuid property |
| 239 | * @uuid_out: uuid buffer to replace |
| 240 | * @buf: raw sysfs buffer to parse |
| 241 | * |
| 242 | * Enforce that uuids can only be changed while the device is disabled |
| 243 | * (driver detached) |
| 244 | * LOCKING: expects device_lock() is held on entry |
| 245 | */ |
| 246 | int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf, |
| 247 | size_t len) |
| 248 | { |
| 249 | u8 uuid[16]; |
| 250 | int rc; |
| 251 | |
| 252 | if (dev->driver) |
| 253 | return -EBUSY; |
| 254 | |
| 255 | rc = nd_uuid_parse(dev, uuid, buf, len); |
| 256 | if (rc) |
| 257 | return rc; |
| 258 | |
| 259 | kfree(*uuid_out); |
| 260 | *uuid_out = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); |
| 261 | if (!(*uuid_out)) |
| 262 | return -ENOMEM; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 267 | ssize_t nd_sector_size_show(unsigned long current_lbasize, |
| 268 | const unsigned long *supported, char *buf) |
| 269 | { |
| 270 | ssize_t len = 0; |
| 271 | int i; |
| 272 | |
| 273 | for (i = 0; supported[i]; i++) |
| 274 | if (current_lbasize == supported[i]) |
| 275 | len += sprintf(buf + len, "[%ld] ", supported[i]); |
| 276 | else |
| 277 | len += sprintf(buf + len, "%ld ", supported[i]); |
| 278 | len += sprintf(buf + len, "\n"); |
| 279 | return len; |
| 280 | } |
| 281 | |
| 282 | ssize_t nd_sector_size_store(struct device *dev, const char *buf, |
| 283 | unsigned long *current_lbasize, const unsigned long *supported) |
| 284 | { |
| 285 | unsigned long lbasize; |
| 286 | int rc, i; |
| 287 | |
| 288 | if (dev->driver) |
| 289 | return -EBUSY; |
| 290 | |
| 291 | rc = kstrtoul(buf, 0, &lbasize); |
| 292 | if (rc) |
| 293 | return rc; |
| 294 | |
| 295 | for (i = 0; supported[i]; i++) |
| 296 | if (lbasize == supported[i]) |
| 297 | break; |
| 298 | |
| 299 | if (supported[i]) { |
| 300 | *current_lbasize = lbasize; |
| 301 | return 0; |
| 302 | } else { |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | } |
| 306 | |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 307 | void __nd_iostat_start(struct bio *bio, unsigned long *start) |
| 308 | { |
| 309 | struct gendisk *disk = bio->bi_bdev->bd_disk; |
| 310 | const int rw = bio_data_dir(bio); |
| 311 | int cpu = part_stat_lock(); |
| 312 | |
| 313 | *start = jiffies; |
| 314 | part_round_stats(cpu, &disk->part0); |
| 315 | part_stat_inc(cpu, &disk->part0, ios[rw]); |
| 316 | part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio)); |
| 317 | part_inc_in_flight(&disk->part0, rw); |
| 318 | part_stat_unlock(); |
| 319 | } |
| 320 | EXPORT_SYMBOL(__nd_iostat_start); |
| 321 | |
| 322 | void nd_iostat_end(struct bio *bio, unsigned long start) |
| 323 | { |
| 324 | struct gendisk *disk = bio->bi_bdev->bd_disk; |
| 325 | unsigned long duration = jiffies - start; |
| 326 | const int rw = bio_data_dir(bio); |
| 327 | int cpu = part_stat_lock(); |
| 328 | |
| 329 | part_stat_add(cpu, &disk->part0, ticks[rw], duration); |
| 330 | part_round_stats(cpu, &disk->part0); |
| 331 | part_dec_in_flight(&disk->part0, rw); |
| 332 | part_stat_unlock(); |
| 333 | } |
| 334 | EXPORT_SYMBOL(nd_iostat_end); |
| 335 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 336 | static ssize_t commands_show(struct device *dev, |
| 337 | struct device_attribute *attr, char *buf) |
| 338 | { |
| 339 | int cmd, len = 0; |
| 340 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 341 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 342 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 343 | for_each_set_bit(cmd, &nd_desc->cmd_mask, BITS_PER_LONG) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 344 | len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd)); |
| 345 | len += sprintf(buf + len, "\n"); |
| 346 | return len; |
| 347 | } |
| 348 | static DEVICE_ATTR_RO(commands); |
| 349 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 350 | static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus) |
| 351 | { |
| 352 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 353 | struct device *parent = nvdimm_bus->dev.parent; |
| 354 | |
| 355 | if (nd_desc->provider_name) |
| 356 | return nd_desc->provider_name; |
| 357 | else if (parent) |
| 358 | return dev_name(parent); |
| 359 | else |
| 360 | return "unknown"; |
| 361 | } |
| 362 | |
| 363 | static ssize_t provider_show(struct device *dev, |
| 364 | struct device_attribute *attr, char *buf) |
| 365 | { |
| 366 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 367 | |
| 368 | return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus)); |
| 369 | } |
| 370 | static DEVICE_ATTR_RO(provider); |
| 371 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 372 | static int flush_namespaces(struct device *dev, void *data) |
| 373 | { |
| 374 | device_lock(dev); |
| 375 | device_unlock(dev); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int flush_regions_dimms(struct device *dev, void *data) |
| 380 | { |
| 381 | device_lock(dev); |
| 382 | device_unlock(dev); |
| 383 | device_for_each_child(dev, NULL, flush_namespaces); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static ssize_t wait_probe_show(struct device *dev, |
| 388 | struct device_attribute *attr, char *buf) |
| 389 | { |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 390 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 391 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 392 | int rc; |
| 393 | |
| 394 | if (nd_desc->flush_probe) { |
| 395 | rc = nd_desc->flush_probe(nd_desc); |
| 396 | if (rc) |
| 397 | return rc; |
| 398 | } |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 399 | nd_synchronize(); |
| 400 | device_for_each_child(dev, NULL, flush_regions_dimms); |
| 401 | return sprintf(buf, "1\n"); |
| 402 | } |
| 403 | static DEVICE_ATTR_RO(wait_probe); |
| 404 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 405 | static struct attribute *nvdimm_bus_attributes[] = { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 406 | &dev_attr_commands.attr, |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 407 | &dev_attr_wait_probe.attr, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 408 | &dev_attr_provider.attr, |
| 409 | NULL, |
| 410 | }; |
| 411 | |
| 412 | struct attribute_group nvdimm_bus_attribute_group = { |
| 413 | .attrs = nvdimm_bus_attributes, |
| 414 | }; |
| 415 | EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group); |
| 416 | |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 417 | static void set_badblock(struct badblocks *bb, sector_t s, int num) |
Dan Williams | 87ba05d | 2016-01-09 07:48:43 -0800 | [diff] [blame] | 418 | { |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 419 | 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] | 420 | (u64) s * 512, (u64) num * 512); |
| 421 | /* 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] | 422 | if (badblocks_set(bb, s, num, 1)) |
| 423 | dev_info_once(bb->dev, "%s: failed for sector %llx\n", |
Dan Williams | 87ba05d | 2016-01-09 07:48:43 -0800 | [diff] [blame] | 424 | __func__, (u64) s); |
| 425 | } |
| 426 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 427 | /** |
| 428 | * __add_badblock_range() - Convert a physical address range to bad sectors |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 429 | * @bb: badblocks instance to populate |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 430 | * @ns_offset: namespace offset where the error range begins (in bytes) |
| 431 | * @len: number of bytes of poison to be added |
| 432 | * |
| 433 | * This assumes that the range provided with (ns_offset, len) is within |
| 434 | * the bounds of physical addresses for this namespace, i.e. lies in the |
| 435 | * interval [ns_start, ns_start + ns_size) |
| 436 | */ |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 437 | 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] | 438 | { |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 439 | const unsigned int sector_size = 512; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 440 | sector_t start_sector; |
| 441 | u64 num_sectors; |
| 442 | u32 rem; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 443 | |
| 444 | start_sector = div_u64(ns_offset, sector_size); |
| 445 | num_sectors = div_u64_rem(len, sector_size, &rem); |
| 446 | if (rem) |
| 447 | num_sectors++; |
| 448 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 449 | if (unlikely(num_sectors > (u64)INT_MAX)) { |
| 450 | u64 remaining = num_sectors; |
| 451 | sector_t s = start_sector; |
| 452 | |
| 453 | while (remaining) { |
| 454 | int done = min_t(u64, remaining, INT_MAX); |
| 455 | |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 456 | set_badblock(bb, s, done); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 457 | remaining -= done; |
| 458 | s += done; |
| 459 | } |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 460 | } else |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 461 | set_badblock(bb, start_sector, num_sectors); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 464 | static void badblocks_populate(struct list_head *poison_list, |
| 465 | struct badblocks *bb, const struct resource *res) |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 466 | { |
| 467 | struct nd_poison *pl; |
| 468 | |
| 469 | if (list_empty(poison_list)) |
| 470 | return; |
| 471 | |
| 472 | list_for_each_entry(pl, poison_list, list) { |
| 473 | u64 pl_end = pl->start + pl->length - 1; |
| 474 | |
| 475 | /* Discard intervals with no intersection */ |
| 476 | if (pl_end < res->start) |
| 477 | continue; |
| 478 | if (pl->start > res->end) |
| 479 | continue; |
| 480 | /* Deal with any overlap after start of the namespace */ |
| 481 | if (pl->start >= res->start) { |
| 482 | u64 start = pl->start; |
| 483 | u64 len; |
| 484 | |
| 485 | if (pl_end <= res->end) |
| 486 | len = pl->length; |
| 487 | else |
| 488 | len = res->start + resource_size(res) |
| 489 | - pl->start; |
| 490 | __add_badblock_range(bb, start - res->start, len); |
| 491 | continue; |
| 492 | } |
| 493 | /* Deal with overlap for poison starting before the namespace */ |
| 494 | if (pl->start < res->start) { |
| 495 | u64 len; |
| 496 | |
| 497 | if (pl_end < res->end) |
| 498 | len = pl->start + pl->length - res->start; |
| 499 | else |
| 500 | len = resource_size(res); |
| 501 | __add_badblock_range(bb, 0, len); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 506 | /** |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 507 | * nvdimm_badblocks_populate() - Convert a list of poison ranges to badblocks |
| 508 | * @region: parent region of the range to interrogate |
| 509 | * @bb: badblocks instance to populate |
| 510 | * @res: resource range to consider |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 511 | * |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 512 | * The poison list generated during bus initialization may contain |
| 513 | * multiple, possibly overlapping physical address ranges. Compare each |
| 514 | * of these ranges to the resource range currently being initialized, |
| 515 | * and add badblocks entries for all matching sub-ranges |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 516 | */ |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 517 | void nvdimm_badblocks_populate(struct nd_region *nd_region, |
| 518 | struct badblocks *bb, const struct resource *res) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 519 | { |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 520 | struct nvdimm_bus *nvdimm_bus; |
| 521 | struct list_head *poison_list; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 522 | |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 523 | if (!is_nd_pmem(&nd_region->dev)) { |
| 524 | dev_WARN_ONCE(&nd_region->dev, 1, |
| 525 | "%s only valid for pmem regions\n", __func__); |
| 526 | return; |
| 527 | } |
| 528 | nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 529 | poison_list = &nvdimm_bus->poison_list; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 530 | |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 531 | nvdimm_bus_lock(&nvdimm_bus->dev); |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 532 | badblocks_populate(poison_list, bb, res); |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 533 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 534 | } |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 535 | EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate); |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 536 | |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 537 | static int add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 538 | { |
| 539 | struct nd_poison *pl; |
| 540 | |
| 541 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); |
| 542 | if (!pl) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | pl->start = addr; |
| 546 | pl->length = length; |
| 547 | list_add_tail(&pl->list, &nvdimm_bus->poison_list); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 552 | 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] | 553 | { |
| 554 | struct nd_poison *pl; |
| 555 | |
| 556 | if (list_empty(&nvdimm_bus->poison_list)) |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 557 | return add_poison(nvdimm_bus, addr, length); |
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; |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * If not a duplicate or a simple length update, add the entry as is, |
| 574 | * as any overlapping ranges will get resolved when the list is consumed |
| 575 | * and converted to badblocks |
| 576 | */ |
Dan Williams | 5faecf4 | 2016-02-17 15:25:36 -0800 | [diff] [blame] | 577 | return add_poison(nvdimm_bus, addr, length); |
| 578 | } |
| 579 | |
| 580 | int nvdimm_bus_add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
| 581 | { |
| 582 | int rc; |
| 583 | |
| 584 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 585 | rc = bus_add_poison(nvdimm_bus, addr, length); |
| 586 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 587 | |
| 588 | return rc; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 589 | } |
| 590 | EXPORT_SYMBOL_GPL(nvdimm_bus_add_poison); |
| 591 | |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 592 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 593 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) |
| 594 | { |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 595 | struct blk_integrity bi; |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 596 | |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 597 | if (meta_size == 0) |
| 598 | return 0; |
| 599 | |
Johannes Thumshirn | 8729bde | 2016-06-23 11:52:04 +0200 | [diff] [blame] | 600 | memset(&bi, 0, sizeof(bi)); |
| 601 | |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 602 | bi.tuple_size = meta_size; |
| 603 | bi.tag_size = meta_size; |
| 604 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 605 | blk_integrity_register(disk, &bi); |
Vishal Verma | 41cd8b7 | 2015-06-25 04:21:52 -0400 | [diff] [blame] | 606 | blk_queue_max_integrity_segments(disk->queue, 1); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | EXPORT_SYMBOL(nd_integrity_init); |
| 611 | |
| 612 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
| 613 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) |
| 614 | { |
| 615 | return 0; |
| 616 | } |
| 617 | EXPORT_SYMBOL(nd_integrity_init); |
| 618 | |
| 619 | #endif |
| 620 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 621 | static __init int libnvdimm_init(void) |
| 622 | { |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 623 | int rc; |
| 624 | |
| 625 | rc = nvdimm_bus_init(); |
| 626 | if (rc) |
| 627 | return rc; |
| 628 | rc = nvdimm_init(); |
| 629 | if (rc) |
| 630 | goto err_dimm; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 631 | rc = nd_region_init(); |
| 632 | if (rc) |
| 633 | goto err_region; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 634 | return 0; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 635 | err_region: |
| 636 | nvdimm_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 637 | err_dimm: |
| 638 | nvdimm_bus_exit(); |
| 639 | return rc; |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static __exit void libnvdimm_exit(void) |
| 643 | { |
| 644 | WARN_ON(!list_empty(&nvdimm_bus_list)); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 645 | nd_region_exit(); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 646 | nvdimm_exit(); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 647 | nvdimm_bus_exit(); |
Dan Williams | b354aba | 2016-05-17 20:24:16 -0700 | [diff] [blame] | 648 | nd_region_devs_exit(); |
| 649 | nvdimm_devs_exit(); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 652 | MODULE_LICENSE("GPL v2"); |
| 653 | MODULE_AUTHOR("Intel Corporation"); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 654 | subsys_initcall(libnvdimm_init); |
| 655 | module_exit(libnvdimm_exit); |