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