^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright(c) 2017 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/libnvdimm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/badblocks.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/ndctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "nd-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "nd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void badrange_init(struct badrange *badrange)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) INIT_LIST_HEAD(&badrange->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) spin_lock_init(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) EXPORT_SYMBOL_GPL(badrange_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static void append_badrange_entry(struct badrange *badrange,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct badrange_entry *bre, u64 addr, u64 length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) lockdep_assert_held(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) bre->start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) bre->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) list_add_tail(&bre->list, &badrange->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int alloc_and_append_badrange_entry(struct badrange *badrange,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u64 addr, u64 length, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct badrange_entry *bre;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) bre = kzalloc(sizeof(*bre), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!bre)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) append_badrange_entry(badrange, bre, addr, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int add_badrange(struct badrange *badrange, u64 addr, u64 length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct badrange_entry *bre, *bre_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) spin_unlock(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) bre_new = kzalloc(sizeof(*bre_new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) spin_lock(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (list_empty(&badrange->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!bre_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) append_badrange_entry(badrange, bre_new, addr, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * There is a chance this is a duplicate, check for those first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * This will be the common case as ARS_STATUS returns all known
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * errors in the SPA space, and we can't query it per region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) list_for_each_entry(bre, &badrange->list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (bre->start == addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* If length has changed, update this list entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (bre->length != length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) bre->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) kfree(bre_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * If not a duplicate or a simple length update, add the entry as is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * as any overlapping ranges will get resolved when the list is consumed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * and converted to badblocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!bre_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) append_badrange_entry(badrange, bre_new, addr, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int badrange_add(struct badrange *badrange, u64 addr, u64 length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) spin_lock(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) rc = add_badrange(badrange, addr, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) spin_unlock(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) EXPORT_SYMBOL_GPL(badrange_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) void badrange_forget(struct badrange *badrange, phys_addr_t start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct list_head *badrange_list = &badrange->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u64 clr_end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct badrange_entry *bre, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) spin_lock(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * [start, clr_end] is the badrange interval being cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * [bre->start, bre_end] is the badrange_list entry we're comparing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * the above interval against. The badrange list entry may need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * to be modified (update either start or length), deleted, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * split into two based on the overlap characteristics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) list_for_each_entry_safe(bre, next, badrange_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) u64 bre_end = bre->start + bre->length - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* Skip intervals with no intersection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (bre_end < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (bre->start > clr_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Delete completely overlapped badrange entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if ((bre->start >= start) && (bre_end <= clr_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) list_del(&bre->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) kfree(bre);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Adjust start point of partially cleared entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if ((start <= bre->start) && (clr_end > bre->start)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) bre->length -= clr_end - bre->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) bre->start = clr_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Adjust bre->length for partial clearing at the tail end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if ((bre->start < start) && (bre_end <= clr_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* bre->start remains the same */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) bre->length = start - bre->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * If clearing in the middle of an entry, we split it into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * two by modifying the current entry to represent one half of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * the split, and adding a new entry for the second half.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if ((bre->start < start) && (bre_end > clr_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) u64 new_start = clr_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) u64 new_len = bre_end - new_start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Add new entry covering the right half */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) alloc_and_append_badrange_entry(badrange, new_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) new_len, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Adjust this entry to cover the left half */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) bre->length = start - bre->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) spin_unlock(&badrange->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) EXPORT_SYMBOL_GPL(badrange_forget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void set_badblock(struct badblocks *bb, sector_t s, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dev_dbg(bb->dev, "Found a bad range (0x%llx, 0x%llx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) (u64) s * 512, (u64) num * 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* this isn't an error as the hardware will still throw an exception */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (badblocks_set(bb, s, num, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) dev_info_once(bb->dev, "%s: failed for sector %llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) __func__, (u64) s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * __add_badblock_range() - Convert a physical address range to bad sectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @bb: badblocks instance to populate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @ns_offset: namespace offset where the error range begins (in bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @len: number of bytes of badrange to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * This assumes that the range provided with (ns_offset, len) is within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * the bounds of physical addresses for this namespace, i.e. lies in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * interval [ns_start, ns_start + ns_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static void __add_badblock_range(struct badblocks *bb, u64 ns_offset, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) const unsigned int sector_size = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) sector_t start_sector, end_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) u64 num_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) u32 rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) start_sector = div_u64(ns_offset, sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) end_sector = div_u64_rem(ns_offset + len, sector_size, &rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (rem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) end_sector++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) num_sectors = end_sector - start_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (unlikely(num_sectors > (u64)INT_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) u64 remaining = num_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) sector_t s = start_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) while (remaining) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int done = min_t(u64, remaining, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) set_badblock(bb, s, done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) remaining -= done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) s += done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) set_badblock(bb, start_sector, num_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static void badblocks_populate(struct badrange *badrange,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct badblocks *bb, const struct range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct badrange_entry *bre;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (list_empty(&badrange->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) list_for_each_entry(bre, &badrange->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) u64 bre_end = bre->start + bre->length - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* Discard intervals with no intersection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (bre_end < range->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (bre->start > range->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /* Deal with any overlap after start of the namespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (bre->start >= range->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) u64 start = bre->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (bre_end <= range->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) len = bre->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) len = range->start + range_len(range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) - bre->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) __add_badblock_range(bb, start - range->start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * Deal with overlap for badrange starting before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * the namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (bre->start < range->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (bre_end < range->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) len = bre->start + bre->length - range->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) len = range_len(range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) __add_badblock_range(bb, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * nvdimm_badblocks_populate() - Convert a list of badranges to badblocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @region: parent region of the range to interrogate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @bb: badblocks instance to populate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @res: resource range to consider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * The badrange list generated during bus initialization may contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * multiple, possibly overlapping physical address ranges. Compare each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * of these ranges to the resource range currently being initialized,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * and add badblocks entries for all matching sub-ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) void nvdimm_badblocks_populate(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct badblocks *bb, const struct range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct nvdimm_bus *nvdimm_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!is_memory(&nd_region->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) dev_WARN_ONCE(&nd_region->dev, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) "%s only valid for pmem regions\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) nvdimm_bus_lock(&nvdimm_bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) badblocks_populate(&nvdimm_bus->badrange, bb, range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) nvdimm_bus_unlock(&nvdimm_bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate);