1
2
3
4
5
6
7
8
9
10
11
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
27LIST_HEAD(nvdimm_bus_list);
28DEFINE_MUTEX(nvdimm_bus_list_mutex);
29
30void 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}
38EXPORT_SYMBOL(nvdimm_bus_lock);
39
40void 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}
48EXPORT_SYMBOL(nvdimm_bus_unlock);
49
50bool 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}
58EXPORT_SYMBOL(is_nvdimm_bus_locked);
59
60struct 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
73static 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
85static 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 dev_err(&nvdimm_bus->dev, "failed to request %pa + %zd for %s\n",
104 &offset, size, dev_name(dev));
105 goto err_request_region;
106 }
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
129static 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
147static 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
159
160
161
162
163
164void *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
177 if (!nvdimm_map)
178 return NULL;
179
180 if (devm_add_action_or_reset(dev, nvdimm_map_put, nvdimm_map))
181 return NULL;
182
183 return nvdimm_map->mem;
184}
185EXPORT_SYMBOL_GPL(devm_nvdimm_memremap);
186
187u64 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}
201EXPORT_SYMBOL_GPL(nd_fletcher64);
202
203struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus)
204{
205
206 return nvdimm_bus->nd_desc;
207}
208EXPORT_SYMBOL_GPL(to_nd_desc);
209
210struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus)
211{
212
213 return &nvdimm_bus->dev;
214}
215EXPORT_SYMBOL_GPL(to_nvdimm_bus_dev);
216
217static bool is_uuid_sep(char sep)
218{
219 if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0')
220 return true;
221 return false;
222}
223
224static 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
251
252
253
254
255
256
257
258
259int 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
280ssize_t nd_size_select_show(unsigned long current_size,
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_size == 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
295ssize_t nd_size_select_store(struct device *dev, const char *buf,
296 unsigned long *current_size, 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_size = lbasize;
314 return 0;
315 } else {
316 return -EINVAL;
317 }
318}
319
320static ssize_t commands_show(struct device *dev,
321 struct device_attribute *attr, char *buf)
322{
323 int cmd, len = 0;
324 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
325 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
326
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");
330 return len;
331}
332static DEVICE_ATTR_RO(commands);
333
334static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
335{
336 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
337 struct device *parent = nvdimm_bus->dev.parent;
338
339 if (nd_desc->provider_name)
340 return nd_desc->provider_name;
341 else if (parent)
342 return dev_name(parent);
343 else
344 return "unknown";
345}
346
347static ssize_t provider_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
350 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
351
352 return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
353}
354static DEVICE_ATTR_RO(provider);
355
356static int flush_namespaces(struct device *dev, void *data)
357{
358 device_lock(dev);
359 device_unlock(dev);
360 return 0;
361}
362
363static int flush_regions_dimms(struct device *dev, void *data)
364{
365 device_lock(dev);
366 device_unlock(dev);
367 device_for_each_child(dev, NULL, flush_namespaces);
368 return 0;
369}
370
371static ssize_t wait_probe_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
373{
374 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
375 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
376 int rc;
377
378 if (nd_desc->flush_probe) {
379 rc = nd_desc->flush_probe(nd_desc);
380 if (rc)
381 return rc;
382 }
383 nd_synchronize();
384 device_for_each_child(dev, NULL, flush_regions_dimms);
385 return sprintf(buf, "1\n");
386}
387static DEVICE_ATTR_RO(wait_probe);
388
389static struct attribute *nvdimm_bus_attributes[] = {
390 &dev_attr_commands.attr,
391 &dev_attr_wait_probe.attr,
392 &dev_attr_provider.attr,
393 NULL,
394};
395
396struct attribute_group nvdimm_bus_attribute_group = {
397 .attrs = nvdimm_bus_attributes,
398};
399EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
400
401int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr, u64 length)
402{
403 return badrange_add(&nvdimm_bus->badrange, addr, length);
404}
405EXPORT_SYMBOL_GPL(nvdimm_bus_add_badrange);
406
407#ifdef CONFIG_BLK_DEV_INTEGRITY
408int nd_integrity_init(struct gendisk *disk, unsigned long meta_size)
409{
410 struct blk_integrity bi;
411
412 if (meta_size == 0)
413 return 0;
414
415 memset(&bi, 0, sizeof(bi));
416
417 bi.tuple_size = meta_size;
418 bi.tag_size = meta_size;
419
420 blk_integrity_register(disk, &bi);
421 blk_queue_max_integrity_segments(disk->queue, 1);
422
423 return 0;
424}
425EXPORT_SYMBOL(nd_integrity_init);
426
427#else
428int nd_integrity_init(struct gendisk *disk, unsigned long meta_size)
429{
430 return 0;
431}
432EXPORT_SYMBOL(nd_integrity_init);
433
434#endif
435
436static __init int libnvdimm_init(void)
437{
438 int rc;
439
440 rc = nvdimm_bus_init();
441 if (rc)
442 return rc;
443 rc = nvdimm_init();
444 if (rc)
445 goto err_dimm;
446 rc = nd_region_init();
447 if (rc)
448 goto err_region;
449
450 nd_label_init();
451
452 return 0;
453 err_region:
454 nvdimm_exit();
455 err_dimm:
456 nvdimm_bus_exit();
457 return rc;
458}
459
460static __exit void libnvdimm_exit(void)
461{
462 WARN_ON(!list_empty(&nvdimm_bus_list));
463 nd_region_exit();
464 nvdimm_exit();
465 nvdimm_bus_exit();
466 nd_region_devs_exit();
467 nvdimm_devs_exit();
468}
469
470MODULE_LICENSE("GPL v2");
471MODULE_AUTHOR("Intel Corporation");
472subsys_initcall(libnvdimm_init);
473module_exit(libnvdimm_exit);
474