2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
13 #include <linux/libnvdimm.h>
14 #include <linux/badblocks.h>
15 #include <linux/export.h>
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/device.h>
19 #include <linux/ctype.h>
20 #include <linux/ndctl.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
27 LIST_HEAD(nvdimm_bus_list
);
28 DEFINE_MUTEX(nvdimm_bus_list_mutex
);
30 void nvdimm_bus_lock(struct device
*dev
)
32 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
36 mutex_lock(&nvdimm_bus
->reconfig_mutex
);
38 EXPORT_SYMBOL(nvdimm_bus_lock
);
40 void nvdimm_bus_unlock(struct device
*dev
)
42 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
46 mutex_unlock(&nvdimm_bus
->reconfig_mutex
);
48 EXPORT_SYMBOL(nvdimm_bus_unlock
);
50 bool is_nvdimm_bus_locked(struct device
*dev
)
52 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
56 return mutex_is_locked(&nvdimm_bus
->reconfig_mutex
);
58 EXPORT_SYMBOL(is_nvdimm_bus_locked
);
61 struct nvdimm_bus
*nvdimm_bus
;
62 struct list_head list
;
63 resource_size_t offset
;
73 static struct nvdimm_map
*find_nvdimm_map(struct device
*dev
,
74 resource_size_t offset
)
76 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
77 struct nvdimm_map
*nvdimm_map
;
79 list_for_each_entry(nvdimm_map
, &nvdimm_bus
->mapping_list
, list
)
80 if (nvdimm_map
->offset
== offset
)
85 static struct nvdimm_map
*alloc_nvdimm_map(struct device
*dev
,
86 resource_size_t offset
, size_t size
, unsigned long flags
)
88 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
89 struct nvdimm_map
*nvdimm_map
;
91 nvdimm_map
= kzalloc(sizeof(*nvdimm_map
), GFP_KERNEL
);
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
);
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
));
105 goto err_request_region
;
109 nvdimm_map
->mem
= memremap(offset
, size
, flags
);
111 nvdimm_map
->iomem
= ioremap(offset
, size
);
113 if (!nvdimm_map
->mem
)
116 dev_WARN_ONCE(dev
, !is_nvdimm_bus_locked(dev
), "%s: bus unlocked!",
118 list_add(&nvdimm_map
->list
, &nvdimm_bus
->mapping_list
);
123 release_mem_region(offset
, size
);
129 static void nvdimm_map_release(struct kref
*kref
)
131 struct nvdimm_bus
*nvdimm_bus
;
132 struct nvdimm_map
*nvdimm_map
;
134 nvdimm_map
= container_of(kref
, struct nvdimm_map
, kref
);
135 nvdimm_bus
= nvdimm_map
->nvdimm_bus
;
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
);
142 iounmap(nvdimm_map
->iomem
);
143 release_mem_region(nvdimm_map
->offset
, nvdimm_map
->size
);
147 static void nvdimm_map_put(void *data
)
149 struct nvdimm_map
*nvdimm_map
= data
;
150 struct nvdimm_bus
*nvdimm_bus
= nvdimm_map
->nvdimm_bus
;
152 nvdimm_bus_lock(&nvdimm_bus
->dev
);
153 kref_put(&nvdimm_map
->kref
, nvdimm_map_release
);
154 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
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
164 void *devm_nvdimm_memremap(struct device
*dev
, resource_size_t offset
,
165 size_t size
, unsigned long flags
)
167 struct nvdimm_map
*nvdimm_map
;
169 nvdimm_bus_lock(dev
);
170 nvdimm_map
= find_nvdimm_map(dev
, offset
);
172 nvdimm_map
= alloc_nvdimm_map(dev
, offset
, size
, flags
);
174 kref_get(&nvdimm_map
->kref
);
175 nvdimm_bus_unlock(dev
);
180 if (devm_add_action_or_reset(dev
, nvdimm_map_put
, nvdimm_map
))
183 return nvdimm_map
->mem
;
185 EXPORT_SYMBOL_GPL(devm_nvdimm_memremap
);
187 u64
nd_fletcher64(void *addr
, size_t len
, bool le
)
194 for (i
= 0; i
< len
/ sizeof(u32
); i
++) {
195 lo32
+= le
? le32_to_cpu((__le32
) buf
[i
]) : buf
[i
];
199 return hi32
<< 32 | lo32
;
201 EXPORT_SYMBOL_GPL(nd_fletcher64
);
203 struct nvdimm_bus_descriptor
*to_nd_desc(struct nvdimm_bus
*nvdimm_bus
)
205 /* struct nvdimm_bus definition is private to libnvdimm */
206 return nvdimm_bus
->nd_desc
;
208 EXPORT_SYMBOL_GPL(to_nd_desc
);
210 struct device
*to_nvdimm_bus_dev(struct nvdimm_bus
*nvdimm_bus
)
212 /* struct nvdimm_bus definition is private to libnvdimm */
213 return &nvdimm_bus
->dev
;
215 EXPORT_SYMBOL_GPL(to_nvdimm_bus_dev
);
217 static bool is_uuid_sep(char sep
)
219 if (sep
== '\n' || sep
== '-' || sep
== ':' || sep
== '\0')
224 static int nd_uuid_parse(struct device
*dev
, u8
*uuid_out
, const char *buf
,
227 const char *str
= buf
;
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]);
239 uuid
[i
] = (hex_to_bin(str
[0]) << 4) | hex_to_bin(str
[1]);
241 if (is_uuid_sep(*str
))
245 memcpy(uuid_out
, uuid
, sizeof(uuid
));
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
255 * Enforce that uuids can only be changed while the device is disabled
257 * LOCKING: expects device_lock() is held on entry
259 int nd_uuid_store(struct device
*dev
, u8
**uuid_out
, const char *buf
,
268 rc
= nd_uuid_parse(dev
, uuid
, buf
, len
);
273 *uuid_out
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
280 ssize_t
nd_size_select_show(unsigned long current_size
,
281 const unsigned long *supported
, char *buf
)
286 for (i
= 0; supported
[i
]; i
++)
287 if (current_size
== supported
[i
])
288 len
+= sprintf(buf
+ len
, "[%ld] ", supported
[i
]);
290 len
+= sprintf(buf
+ len
, "%ld ", supported
[i
]);
291 len
+= sprintf(buf
+ len
, "\n");
295 ssize_t
nd_size_select_store(struct device
*dev
, const char *buf
,
296 unsigned long *current_size
, const unsigned long *supported
)
298 unsigned long lbasize
;
304 rc
= kstrtoul(buf
, 0, &lbasize
);
308 for (i
= 0; supported
[i
]; i
++)
309 if (lbasize
== supported
[i
])
313 *current_size
= lbasize
;
320 static ssize_t
commands_show(struct device
*dev
,
321 struct device_attribute
*attr
, char *buf
)
324 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
325 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
327 for_each_set_bit(cmd
, &nd_desc
->cmd_mask
, BITS_PER_LONG
)
328 len
+= sprintf(buf
+ len
, "%s ", nvdimm_bus_cmd_name(cmd
));
329 len
+= sprintf(buf
+ len
, "\n");
332 static DEVICE_ATTR_RO(commands
);
334 static const char *nvdimm_bus_provider(struct nvdimm_bus
*nvdimm_bus
)
336 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
337 struct device
*parent
= nvdimm_bus
->dev
.parent
;
339 if (nd_desc
->provider_name
)
340 return nd_desc
->provider_name
;
342 return dev_name(parent
);
347 static ssize_t
provider_show(struct device
*dev
,
348 struct device_attribute
*attr
, char *buf
)
350 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
352 return sprintf(buf
, "%s\n", nvdimm_bus_provider(nvdimm_bus
));
354 static DEVICE_ATTR_RO(provider
);
356 static int flush_namespaces(struct device
*dev
, void *data
)
363 static int flush_regions_dimms(struct device
*dev
, void *data
)
367 device_for_each_child(dev
, NULL
, flush_namespaces
);
371 static ssize_t
wait_probe_show(struct device
*dev
,
372 struct device_attribute
*attr
, char *buf
)
374 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
375 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
378 if (nd_desc
->flush_probe
) {
379 rc
= nd_desc
->flush_probe(nd_desc
);
384 device_for_each_child(dev
, NULL
, flush_regions_dimms
);
385 return sprintf(buf
, "1\n");
387 static DEVICE_ATTR_RO(wait_probe
);
389 static struct attribute
*nvdimm_bus_attributes
[] = {
390 &dev_attr_commands
.attr
,
391 &dev_attr_wait_probe
.attr
,
392 &dev_attr_provider
.attr
,
396 struct attribute_group nvdimm_bus_attribute_group
= {
397 .attrs
= nvdimm_bus_attributes
,
399 EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group
);
401 static void set_badblock(struct badblocks
*bb
, sector_t s
, int num
)
403 dev_dbg(bb
->dev
, "Found a poison range (0x%llx, 0x%llx)\n",
404 (u64
) s
* 512, (u64
) num
* 512);
405 /* this isn't an error as the hardware will still throw an exception */
406 if (badblocks_set(bb
, s
, num
, 1))
407 dev_info_once(bb
->dev
, "%s: failed for sector %llx\n",
412 * __add_badblock_range() - Convert a physical address range to bad sectors
413 * @bb: badblocks instance to populate
414 * @ns_offset: namespace offset where the error range begins (in bytes)
415 * @len: number of bytes of poison to be added
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)
421 static void __add_badblock_range(struct badblocks
*bb
, u64 ns_offset
, u64 len
)
423 const unsigned int sector_size
= 512;
424 sector_t start_sector
, end_sector
;
428 start_sector
= div_u64(ns_offset
, sector_size
);
429 end_sector
= div_u64_rem(ns_offset
+ len
, sector_size
, &rem
);
432 num_sectors
= end_sector
- start_sector
;
434 if (unlikely(num_sectors
> (u64
)INT_MAX
)) {
435 u64 remaining
= num_sectors
;
436 sector_t s
= start_sector
;
439 int done
= min_t(u64
, remaining
, INT_MAX
);
441 set_badblock(bb
, s
, done
);
446 set_badblock(bb
, start_sector
, num_sectors
);
449 static void badblocks_populate(struct list_head
*poison_list
,
450 struct badblocks
*bb
, const struct resource
*res
)
452 struct nd_poison
*pl
;
454 if (list_empty(poison_list
))
457 list_for_each_entry(pl
, poison_list
, list
) {
458 u64 pl_end
= pl
->start
+ pl
->length
- 1;
460 /* Discard intervals with no intersection */
461 if (pl_end
< res
->start
)
463 if (pl
->start
> res
->end
)
465 /* Deal with any overlap after start of the namespace */
466 if (pl
->start
>= res
->start
) {
467 u64 start
= pl
->start
;
470 if (pl_end
<= res
->end
)
473 len
= res
->start
+ resource_size(res
)
475 __add_badblock_range(bb
, start
- res
->start
, len
);
478 /* Deal with overlap for poison starting before the namespace */
479 if (pl
->start
< res
->start
) {
482 if (pl_end
< res
->end
)
483 len
= pl
->start
+ pl
->length
- res
->start
;
485 len
= resource_size(res
);
486 __add_badblock_range(bb
, 0, len
);
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
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
502 void nvdimm_badblocks_populate(struct nd_region
*nd_region
,
503 struct badblocks
*bb
, const struct resource
*res
)
505 struct nvdimm_bus
*nvdimm_bus
;
506 struct list_head
*poison_list
;
508 if (!is_memory(&nd_region
->dev
)) {
509 dev_WARN_ONCE(&nd_region
->dev
, 1,
510 "%s only valid for pmem regions\n", __func__
);
513 nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
514 poison_list
= &nvdimm_bus
->poison_list
;
516 nvdimm_bus_lock(&nvdimm_bus
->dev
);
517 badblocks_populate(poison_list
, bb
, res
);
518 nvdimm_bus_unlock(&nvdimm_bus
->dev
);
520 EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate
);
522 static void append_poison_entry(struct nvdimm_bus
*nvdimm_bus
,
523 struct nd_poison
*pl
, u64 addr
, u64 length
)
525 lockdep_assert_held(&nvdimm_bus
->poison_lock
);
528 list_add_tail(&pl
->list
, &nvdimm_bus
->poison_list
);
531 static int add_poison(struct nvdimm_bus
*nvdimm_bus
, u64 addr
, u64 length
,
534 struct nd_poison
*pl
;
536 pl
= kzalloc(sizeof(*pl
), flags
);
540 append_poison_entry(nvdimm_bus
, pl
, addr
, length
);
544 static int bus_add_poison(struct nvdimm_bus
*nvdimm_bus
, u64 addr
, u64 length
)
546 struct nd_poison
*pl
, *pl_new
;
548 spin_unlock(&nvdimm_bus
->poison_lock
);
549 pl_new
= kzalloc(sizeof(*pl_new
), GFP_KERNEL
);
550 spin_lock(&nvdimm_bus
->poison_lock
);
552 if (list_empty(&nvdimm_bus
->poison_list
)) {
555 append_poison_entry(nvdimm_bus
, pl_new
, addr
, length
);
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
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
)
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
580 append_poison_entry(nvdimm_bus
, pl_new
, addr
, length
);
585 int nvdimm_bus_add_poison(struct nvdimm_bus
*nvdimm_bus
, u64 addr
, u64 length
)
589 spin_lock(&nvdimm_bus
->poison_lock
);
590 rc
= bus_add_poison(nvdimm_bus
, addr
, length
);
591 spin_unlock(&nvdimm_bus
->poison_lock
);
595 EXPORT_SYMBOL_GPL(nvdimm_bus_add_poison
);
597 void nvdimm_forget_poison(struct nvdimm_bus
*nvdimm_bus
, phys_addr_t start
,
600 struct list_head
*poison_list
= &nvdimm_bus
->poison_list
;
601 u64 clr_end
= start
+ len
- 1;
602 struct nd_poison
*pl
, *next
;
604 spin_lock(&nvdimm_bus
->poison_lock
);
605 WARN_ON_ONCE(list_empty(poison_list
));
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
615 list_for_each_entry_safe(pl
, next
, poison_list
, list
) {
616 u64 pl_end
= pl
->start
+ pl
->length
- 1;
618 /* Skip intervals with no intersection */
621 if (pl
->start
> clr_end
)
623 /* Delete completely overlapped poison entries */
624 if ((pl
->start
>= start
) && (pl_end
<= clr_end
)) {
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;
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
;
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.
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;
650 /* Add new entry covering the right half */
651 add_poison(nvdimm_bus
, new_start
, new_len
, GFP_NOWAIT
);
652 /* Adjust this entry to cover the left half */
653 pl
->length
= start
- pl
->start
;
657 spin_unlock(&nvdimm_bus
->poison_lock
);
659 EXPORT_SYMBOL_GPL(nvdimm_forget_poison
);
661 #ifdef CONFIG_BLK_DEV_INTEGRITY
662 int nd_integrity_init(struct gendisk
*disk
, unsigned long meta_size
)
664 struct blk_integrity bi
;
669 memset(&bi
, 0, sizeof(bi
));
671 bi
.tuple_size
= meta_size
;
672 bi
.tag_size
= meta_size
;
674 blk_integrity_register(disk
, &bi
);
675 blk_queue_max_integrity_segments(disk
->queue
, 1);
679 EXPORT_SYMBOL(nd_integrity_init
);
681 #else /* CONFIG_BLK_DEV_INTEGRITY */
682 int nd_integrity_init(struct gendisk
*disk
, unsigned long meta_size
)
686 EXPORT_SYMBOL(nd_integrity_init
);
690 static __init
int libnvdimm_init(void)
694 rc
= nvdimm_bus_init();
700 rc
= nd_region_init();
714 static __exit
void libnvdimm_exit(void)
716 WARN_ON(!list_empty(&nvdimm_bus_list
));
720 nd_region_devs_exit();
724 MODULE_LICENSE("GPL v2");
725 MODULE_AUTHOR("Intel Corporation");
726 subsys_initcall(libnvdimm_init
);
727 module_exit(libnvdimm_exit
);