Commit | Line | Data |
---|---|---|
b94d5230 DW |
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> | |
b95f5f43 | 14 | #include <linux/badblocks.h> |
b94d5230 DW |
15 | #include <linux/export.h> |
16 | #include <linux/module.h> | |
41cd8b70 | 17 | #include <linux/blkdev.h> |
b94d5230 | 18 | #include <linux/device.h> |
bf9bccc1 | 19 | #include <linux/ctype.h> |
62232e45 | 20 | #include <linux/ndctl.h> |
45def22c | 21 | #include <linux/mutex.h> |
b94d5230 | 22 | #include <linux/slab.h> |
29b9aa0a | 23 | #include <linux/io.h> |
b94d5230 | 24 | #include "nd-core.h" |
4d88a97a | 25 | #include "nd.h" |
b94d5230 | 26 | |
e6dfb2de DW |
27 | LIST_HEAD(nvdimm_bus_list); |
28 | DEFINE_MUTEX(nvdimm_bus_list_mutex); | |
b94d5230 | 29 | |
3d88002e DW |
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 | ||
29b9aa0a DW |
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 | ||
ecfb6d8a DW |
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)); | |
29b9aa0a | 105 | goto err_request_region; |
ecfb6d8a | 106 | } |
29b9aa0a DW |
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 | ||
ecfb6d8a DW |
177 | if (!nvdimm_map) |
178 | return NULL; | |
179 | ||
29b9aa0a DW |
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 | ||
eaf96153 DW |
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 | ||
45def22c DW |
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 | ||
37b137ff VV |
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 | ||
bf9bccc1 DW |
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 | ||
1b40e09a DW |
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 | ||
f0dc089c DW |
320 | void __nd_iostat_start(struct bio *bio, unsigned long *start) |
321 | { | |
322 | struct gendisk *disk = bio->bi_bdev->bd_disk; | |
323 | const int rw = bio_data_dir(bio); | |
324 | int cpu = part_stat_lock(); | |
325 | ||
326 | *start = jiffies; | |
327 | part_round_stats(cpu, &disk->part0); | |
328 | part_stat_inc(cpu, &disk->part0, ios[rw]); | |
329 | part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio)); | |
330 | part_inc_in_flight(&disk->part0, rw); | |
331 | part_stat_unlock(); | |
332 | } | |
333 | EXPORT_SYMBOL(__nd_iostat_start); | |
334 | ||
335 | void nd_iostat_end(struct bio *bio, unsigned long start) | |
336 | { | |
337 | struct gendisk *disk = bio->bi_bdev->bd_disk; | |
338 | unsigned long duration = jiffies - start; | |
339 | const int rw = bio_data_dir(bio); | |
340 | int cpu = part_stat_lock(); | |
341 | ||
342 | part_stat_add(cpu, &disk->part0, ticks[rw], duration); | |
343 | part_round_stats(cpu, &disk->part0); | |
344 | part_dec_in_flight(&disk->part0, rw); | |
345 | part_stat_unlock(); | |
346 | } | |
347 | EXPORT_SYMBOL(nd_iostat_end); | |
348 | ||
62232e45 DW |
349 | static ssize_t commands_show(struct device *dev, |
350 | struct device_attribute *attr, char *buf) | |
351 | { | |
352 | int cmd, len = 0; | |
353 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); | |
354 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; | |
355 | ||
e3654eca | 356 | for_each_set_bit(cmd, &nd_desc->cmd_mask, BITS_PER_LONG) |
62232e45 DW |
357 | len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd)); |
358 | len += sprintf(buf + len, "\n"); | |
359 | return len; | |
360 | } | |
361 | static DEVICE_ATTR_RO(commands); | |
362 | ||
45def22c DW |
363 | static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus) |
364 | { | |
365 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; | |
366 | struct device *parent = nvdimm_bus->dev.parent; | |
367 | ||
368 | if (nd_desc->provider_name) | |
369 | return nd_desc->provider_name; | |
370 | else if (parent) | |
371 | return dev_name(parent); | |
372 | else | |
373 | return "unknown"; | |
374 | } | |
375 | ||
376 | static ssize_t provider_show(struct device *dev, | |
377 | struct device_attribute *attr, char *buf) | |
378 | { | |
379 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); | |
380 | ||
381 | return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus)); | |
382 | } | |
383 | static DEVICE_ATTR_RO(provider); | |
384 | ||
4d88a97a DW |
385 | static int flush_namespaces(struct device *dev, void *data) |
386 | { | |
387 | device_lock(dev); | |
388 | device_unlock(dev); | |
389 | return 0; | |
390 | } | |
391 | ||
392 | static int flush_regions_dimms(struct device *dev, void *data) | |
393 | { | |
394 | device_lock(dev); | |
395 | device_unlock(dev); | |
396 | device_for_each_child(dev, NULL, flush_namespaces); | |
397 | return 0; | |
398 | } | |
399 | ||
400 | static ssize_t wait_probe_show(struct device *dev, | |
401 | struct device_attribute *attr, char *buf) | |
402 | { | |
7ae0fa43 DW |
403 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
404 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; | |
405 | int rc; | |
406 | ||
407 | if (nd_desc->flush_probe) { | |
408 | rc = nd_desc->flush_probe(nd_desc); | |
409 | if (rc) | |
410 | return rc; | |
411 | } | |
4d88a97a DW |
412 | nd_synchronize(); |
413 | device_for_each_child(dev, NULL, flush_regions_dimms); | |
414 | return sprintf(buf, "1\n"); | |
415 | } | |
416 | static DEVICE_ATTR_RO(wait_probe); | |
417 | ||
45def22c | 418 | static struct attribute *nvdimm_bus_attributes[] = { |
62232e45 | 419 | &dev_attr_commands.attr, |
4d88a97a | 420 | &dev_attr_wait_probe.attr, |
45def22c DW |
421 | &dev_attr_provider.attr, |
422 | NULL, | |
423 | }; | |
424 | ||
425 | struct attribute_group nvdimm_bus_attribute_group = { | |
426 | .attrs = nvdimm_bus_attributes, | |
427 | }; | |
428 | EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group); | |
429 | ||
b95f5f43 | 430 | static void set_badblock(struct badblocks *bb, sector_t s, int num) |
87ba05df | 431 | { |
b95f5f43 | 432 | dev_dbg(bb->dev, "Found a poison range (0x%llx, 0x%llx)\n", |
87ba05df DW |
433 | (u64) s * 512, (u64) num * 512); |
434 | /* this isn't an error as the hardware will still throw an exception */ | |
b95f5f43 DW |
435 | if (badblocks_set(bb, s, num, 1)) |
436 | dev_info_once(bb->dev, "%s: failed for sector %llx\n", | |
87ba05df DW |
437 | __func__, (u64) s); |
438 | } | |
439 | ||
0caeef63 VV |
440 | /** |
441 | * __add_badblock_range() - Convert a physical address range to bad sectors | |
b95f5f43 | 442 | * @bb: badblocks instance to populate |
0caeef63 VV |
443 | * @ns_offset: namespace offset where the error range begins (in bytes) |
444 | * @len: number of bytes of poison to be added | |
445 | * | |
446 | * This assumes that the range provided with (ns_offset, len) is within | |
447 | * the bounds of physical addresses for this namespace, i.e. lies in the | |
448 | * interval [ns_start, ns_start + ns_size) | |
449 | */ | |
b95f5f43 | 450 | static void __add_badblock_range(struct badblocks *bb, u64 ns_offset, u64 len) |
0caeef63 | 451 | { |
b95f5f43 | 452 | const unsigned int sector_size = 512; |
0caeef63 VV |
453 | sector_t start_sector; |
454 | u64 num_sectors; | |
455 | u32 rem; | |
0caeef63 VV |
456 | |
457 | start_sector = div_u64(ns_offset, sector_size); | |
458 | num_sectors = div_u64_rem(len, sector_size, &rem); | |
459 | if (rem) | |
460 | num_sectors++; | |
461 | ||
0caeef63 VV |
462 | if (unlikely(num_sectors > (u64)INT_MAX)) { |
463 | u64 remaining = num_sectors; | |
464 | sector_t s = start_sector; | |
465 | ||
466 | while (remaining) { | |
467 | int done = min_t(u64, remaining, INT_MAX); | |
468 | ||
b95f5f43 | 469 | set_badblock(bb, s, done); |
0caeef63 VV |
470 | remaining -= done; |
471 | s += done; | |
472 | } | |
0caeef63 | 473 | } else |
b95f5f43 | 474 | set_badblock(bb, start_sector, num_sectors); |
0caeef63 VV |
475 | } |
476 | ||
a3901802 DW |
477 | static void badblocks_populate(struct list_head *poison_list, |
478 | struct badblocks *bb, const struct resource *res) | |
0caeef63 | 479 | { |
0caeef63 | 480 | struct nd_poison *pl; |
0caeef63 | 481 | |
0caeef63 | 482 | if (list_empty(poison_list)) |
b95f5f43 | 483 | return; |
0caeef63 VV |
484 | |
485 | list_for_each_entry(pl, poison_list, list) { | |
486 | u64 pl_end = pl->start + pl->length - 1; | |
487 | ||
488 | /* Discard intervals with no intersection */ | |
5faecf4e | 489 | if (pl_end < res->start) |
0caeef63 | 490 | continue; |
5faecf4e | 491 | if (pl->start > res->end) |
0caeef63 VV |
492 | continue; |
493 | /* Deal with any overlap after start of the namespace */ | |
5faecf4e | 494 | if (pl->start >= res->start) { |
0caeef63 VV |
495 | u64 start = pl->start; |
496 | u64 len; | |
497 | ||
5faecf4e | 498 | if (pl_end <= res->end) |
0caeef63 VV |
499 | len = pl->length; |
500 | else | |
5faecf4e DW |
501 | len = res->start + resource_size(res) |
502 | - pl->start; | |
503 | __add_badblock_range(bb, start - res->start, len); | |
0caeef63 VV |
504 | continue; |
505 | } | |
506 | /* Deal with overlap for poison starting before the namespace */ | |
5faecf4e | 507 | if (pl->start < res->start) { |
0caeef63 VV |
508 | u64 len; |
509 | ||
5faecf4e DW |
510 | if (pl_end < res->end) |
511 | len = pl->start + pl->length - res->start; | |
0caeef63 | 512 | else |
5faecf4e | 513 | len = resource_size(res); |
b95f5f43 | 514 | __add_badblock_range(bb, 0, len); |
0caeef63 VV |
515 | } |
516 | } | |
0caeef63 | 517 | } |
5faecf4e DW |
518 | |
519 | /** | |
a3901802 DW |
520 | * nvdimm_badblocks_populate() - Convert a list of poison ranges to badblocks |
521 | * @region: parent region of the range to interrogate | |
522 | * @bb: badblocks instance to populate | |
523 | * @res: resource range to consider | |
5faecf4e | 524 | * |
a3901802 DW |
525 | * The poison list generated during bus initialization may contain |
526 | * multiple, possibly overlapping physical address ranges. Compare each | |
527 | * of these ranges to the resource range currently being initialized, | |
528 | * and add badblocks entries for all matching sub-ranges | |
5faecf4e | 529 | */ |
a3901802 DW |
530 | void nvdimm_badblocks_populate(struct nd_region *nd_region, |
531 | struct badblocks *bb, const struct resource *res) | |
5faecf4e | 532 | { |
5faecf4e DW |
533 | struct nvdimm_bus *nvdimm_bus; |
534 | struct list_head *poison_list; | |
5faecf4e | 535 | |
a3901802 DW |
536 | if (!is_nd_pmem(&nd_region->dev)) { |
537 | dev_WARN_ONCE(&nd_region->dev, 1, | |
538 | "%s only valid for pmem regions\n", __func__); | |
539 | return; | |
540 | } | |
541 | nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); | |
5faecf4e DW |
542 | poison_list = &nvdimm_bus->poison_list; |
543 | ||
544 | nvdimm_bus_lock(&nvdimm_bus->dev); | |
a3901802 | 545 | badblocks_populate(poison_list, bb, res); |
5faecf4e DW |
546 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
547 | } | |
a3901802 | 548 | EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate); |
0caeef63 | 549 | |
5faecf4e | 550 | static int add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
0caeef63 VV |
551 | { |
552 | struct nd_poison *pl; | |
553 | ||
554 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); | |
555 | if (!pl) | |
556 | return -ENOMEM; | |
557 | ||
558 | pl->start = addr; | |
559 | pl->length = length; | |
560 | list_add_tail(&pl->list, &nvdimm_bus->poison_list); | |
561 | ||
562 | return 0; | |
563 | } | |
564 | ||
5faecf4e | 565 | static int bus_add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) |
0caeef63 VV |
566 | { |
567 | struct nd_poison *pl; | |
568 | ||
569 | if (list_empty(&nvdimm_bus->poison_list)) | |
5faecf4e | 570 | return add_poison(nvdimm_bus, addr, length); |
0caeef63 VV |
571 | |
572 | /* | |
573 | * There is a chance this is a duplicate, check for those first. | |
574 | * This will be the common case as ARS_STATUS returns all known | |
575 | * errors in the SPA space, and we can't query it per region | |
576 | */ | |
577 | list_for_each_entry(pl, &nvdimm_bus->poison_list, list) | |
578 | if (pl->start == addr) { | |
579 | /* If length has changed, update this list entry */ | |
580 | if (pl->length != length) | |
581 | pl->length = length; | |
582 | return 0; | |
583 | } | |
584 | ||
585 | /* | |
586 | * If not a duplicate or a simple length update, add the entry as is, | |
587 | * as any overlapping ranges will get resolved when the list is consumed | |
588 | * and converted to badblocks | |
589 | */ | |
5faecf4e DW |
590 | return add_poison(nvdimm_bus, addr, length); |
591 | } | |
592 | ||
593 | int nvdimm_bus_add_poison(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length) | |
594 | { | |
595 | int rc; | |
596 | ||
597 | nvdimm_bus_lock(&nvdimm_bus->dev); | |
598 | rc = bus_add_poison(nvdimm_bus, addr, length); | |
599 | nvdimm_bus_unlock(&nvdimm_bus->dev); | |
600 | ||
601 | return rc; | |
0caeef63 VV |
602 | } |
603 | EXPORT_SYMBOL_GPL(nvdimm_bus_add_poison); | |
604 | ||
41cd8b70 | 605 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
41cd8b70 VV |
606 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) |
607 | { | |
0f8087ec | 608 | struct blk_integrity bi; |
41cd8b70 | 609 | |
fcae6957 VV |
610 | if (meta_size == 0) |
611 | return 0; | |
612 | ||
8729bdea JT |
613 | memset(&bi, 0, sizeof(bi)); |
614 | ||
0f8087ec MP |
615 | bi.tuple_size = meta_size; |
616 | bi.tag_size = meta_size; | |
617 | ||
25520d55 | 618 | blk_integrity_register(disk, &bi); |
41cd8b70 VV |
619 | blk_queue_max_integrity_segments(disk->queue, 1); |
620 | ||
621 | return 0; | |
622 | } | |
623 | EXPORT_SYMBOL(nd_integrity_init); | |
624 | ||
625 | #else /* CONFIG_BLK_DEV_INTEGRITY */ | |
626 | int nd_integrity_init(struct gendisk *disk, unsigned long meta_size) | |
627 | { | |
628 | return 0; | |
629 | } | |
630 | EXPORT_SYMBOL(nd_integrity_init); | |
631 | ||
632 | #endif | |
633 | ||
45def22c DW |
634 | static __init int libnvdimm_init(void) |
635 | { | |
4d88a97a DW |
636 | int rc; |
637 | ||
638 | rc = nvdimm_bus_init(); | |
639 | if (rc) | |
640 | return rc; | |
641 | rc = nvdimm_init(); | |
642 | if (rc) | |
643 | goto err_dimm; | |
3d88002e DW |
644 | rc = nd_region_init(); |
645 | if (rc) | |
646 | goto err_region; | |
4d88a97a | 647 | return 0; |
3d88002e DW |
648 | err_region: |
649 | nvdimm_exit(); | |
4d88a97a DW |
650 | err_dimm: |
651 | nvdimm_bus_exit(); | |
652 | return rc; | |
45def22c DW |
653 | } |
654 | ||
655 | static __exit void libnvdimm_exit(void) | |
656 | { | |
657 | WARN_ON(!list_empty(&nvdimm_bus_list)); | |
3d88002e | 658 | nd_region_exit(); |
4d88a97a | 659 | nvdimm_exit(); |
45def22c | 660 | nvdimm_bus_exit(); |
b354aba0 DW |
661 | nd_region_devs_exit(); |
662 | nvdimm_devs_exit(); | |
45def22c DW |
663 | } |
664 | ||
b94d5230 DW |
665 | MODULE_LICENSE("GPL v2"); |
666 | MODULE_AUTHOR("Intel Corporation"); | |
45def22c DW |
667 | subsys_initcall(libnvdimm_init); |
668 | module_exit(libnvdimm_exit); |