mfd: kempld-core: Constify variables that point to const structure
[linux-2.6-block.git] / drivers / nvdimm / bus.c
CommitLineData
45def22c
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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
aa9ad44a 14#include <linux/libnvdimm.h>
0930a750 15#include <linux/sched/mm.h>
62232e45 16#include <linux/vmalloc.h>
45def22c 17#include <linux/uaccess.h>
3d88002e 18#include <linux/module.h>
8c2f7e86 19#include <linux/blkdev.h>
45def22c 20#include <linux/fcntl.h>
e6dfb2de 21#include <linux/async.h>
8c2f7e86 22#include <linux/genhd.h>
62232e45 23#include <linux/ndctl.h>
4d88a97a 24#include <linux/sched.h>
45def22c
DW
25#include <linux/slab.h>
26#include <linux/fs.h>
27#include <linux/io.h>
62232e45 28#include <linux/mm.h>
4d88a97a 29#include <linux/nd.h>
45def22c 30#include "nd-core.h"
4d88a97a 31#include "nd.h"
006358b3 32#include "pfn.h"
45def22c 33
62232e45 34int nvdimm_major;
45def22c
DW
35static int nvdimm_bus_major;
36static struct class *nd_class;
18515942 37static DEFINE_IDA(nd_ida);
45def22c 38
4d88a97a
DW
39static int to_nd_device_type(struct device *dev)
40{
41 if (is_nvdimm(dev))
42 return ND_DEVICE_DIMM;
c9e582aa 43 else if (is_memory(dev))
3d88002e
DW
44 return ND_DEVICE_REGION_PMEM;
45 else if (is_nd_blk(dev))
46 return ND_DEVICE_REGION_BLK;
cd03412a
DW
47 else if (is_nd_dax(dev))
48 return ND_DEVICE_DAX_PMEM;
c9e582aa 49 else if (is_nd_region(dev->parent))
3d88002e 50 return nd_region_to_nstype(to_nd_region(dev->parent));
4d88a97a
DW
51
52 return 0;
53}
54
55static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
56{
41d7a6d6
TK
57 /*
58 * Ensure that region devices always have their numa node set as
59 * early as possible.
60 */
c9e582aa 61 if (is_nd_region(dev))
41d7a6d6 62 set_dev_node(dev, to_nd_region(dev)->numa_node);
4d88a97a
DW
63 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
64 to_nd_device_type(dev));
65}
66
3d88002e
DW
67static struct module *to_bus_provider(struct device *dev)
68{
69 /* pin bus providers while regions are enabled */
c9e582aa 70 if (is_nd_region(dev)) {
3d88002e
DW
71 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
72
bc9775d8 73 return nvdimm_bus->nd_desc->module;
3d88002e
DW
74 }
75 return NULL;
76}
77
eaf96153
DW
78static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
79{
80 nvdimm_bus_lock(&nvdimm_bus->dev);
81 nvdimm_bus->probe_active++;
82 nvdimm_bus_unlock(&nvdimm_bus->dev);
83}
84
85static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
86{
87 nvdimm_bus_lock(&nvdimm_bus->dev);
88 if (--nvdimm_bus->probe_active == 0)
89 wake_up(&nvdimm_bus->probe_wait);
90 nvdimm_bus_unlock(&nvdimm_bus->dev);
91}
92
4d88a97a
DW
93static int nvdimm_bus_probe(struct device *dev)
94{
95 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
3d88002e 96 struct module *provider = to_bus_provider(dev);
4d88a97a
DW
97 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
98 int rc;
99
3d88002e
DW
100 if (!try_module_get(provider))
101 return -ENXIO;
102
3f46833d
DW
103 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
104 dev->driver->name, dev_name(dev));
105
eaf96153 106 nvdimm_bus_probe_start(nvdimm_bus);
4d88a97a 107 rc = nd_drv->probe(dev);
eaf96153
DW
108 if (rc == 0)
109 nd_region_probe_success(nvdimm_bus, dev);
bf9bccc1
DW
110 else
111 nd_region_disable(nvdimm_bus, dev);
eaf96153
DW
112 nvdimm_bus_probe_end(nvdimm_bus);
113
3f46833d 114 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
4d88a97a 115 dev_name(dev), rc);
8c2f7e86 116
3d88002e
DW
117 if (rc != 0)
118 module_put(provider);
4d88a97a
DW
119 return rc;
120}
121
122static int nvdimm_bus_remove(struct device *dev)
123{
124 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
3d88002e 125 struct module *provider = to_bus_provider(dev);
4d88a97a 126 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
6cf9c5ba 127 int rc = 0;
4d88a97a 128
6cf9c5ba
DW
129 if (nd_drv->remove)
130 rc = nd_drv->remove(dev);
eaf96153
DW
131 nd_region_disable(nvdimm_bus, dev);
132
4d88a97a
DW
133 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
134 dev_name(dev), rc);
3d88002e 135 module_put(provider);
4d88a97a
DW
136 return rc;
137}
138
476f848a
DW
139static void nvdimm_bus_shutdown(struct device *dev)
140{
141 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
142 struct nd_device_driver *nd_drv = NULL;
143
144 if (dev->driver)
145 nd_drv = to_nd_device_driver(dev->driver);
146
147 if (nd_drv && nd_drv->shutdown) {
148 nd_drv->shutdown(dev);
149 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
150 dev->driver->name, dev_name(dev));
151 }
152}
153
71999466
DW
154void nd_device_notify(struct device *dev, enum nvdimm_event event)
155{
156 device_lock(dev);
157 if (dev->driver) {
158 struct nd_device_driver *nd_drv;
159
160 nd_drv = to_nd_device_driver(dev->driver);
161 if (nd_drv->notify)
162 nd_drv->notify(dev, event);
163 }
164 device_unlock(dev);
165}
166EXPORT_SYMBOL(nd_device_notify);
167
168void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
169{
170 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
171
172 if (!nvdimm_bus)
173 return;
174
175 /* caller is responsible for holding a reference on the device */
176 nd_device_notify(&nd_region->dev, event);
177}
178EXPORT_SYMBOL_GPL(nvdimm_region_notify);
179
23f49844
DW
180struct clear_badblocks_context {
181 resource_size_t phys, cleared;
182};
183
184static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
185{
186 struct clear_badblocks_context *ctx = data;
187 struct nd_region *nd_region;
188 resource_size_t ndr_end;
189 sector_t sector;
190
191 /* make sure device is a region */
192 if (!is_nd_pmem(dev))
193 return 0;
194
195 nd_region = to_nd_region(dev);
196 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
197
198 /* make sure we are in the region */
199 if (ctx->phys < nd_region->ndr_start
200 || (ctx->phys + ctx->cleared) > ndr_end)
201 return 0;
202
203 sector = (ctx->phys - nd_region->ndr_start) / 512;
204 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
205
975750a9
TK
206 if (nd_region->bb_state)
207 sysfs_notify_dirent(nd_region->bb_state);
208
23f49844
DW
209 return 0;
210}
211
212static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
213 phys_addr_t phys, u64 cleared)
214{
215 struct clear_badblocks_context ctx = {
216 .phys = phys,
217 .cleared = cleared,
218 };
219
220 device_for_each_child(&nvdimm_bus->dev, &ctx,
221 nvdimm_clear_badblocks_region);
222}
223
224static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
225 phys_addr_t phys, u64 cleared)
226{
227 if (cleared > 0)
aa9ad44a 228 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
23f49844
DW
229
230 if (cleared > 0 && cleared / 512)
231 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
232}
233
59e64739
DW
234long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
235 unsigned int len)
236{
237 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
238 struct nvdimm_bus_descriptor *nd_desc;
239 struct nd_cmd_clear_error clear_err;
240 struct nd_cmd_ars_cap ars_cap;
241 u32 clear_err_unit, mask;
0930a750 242 unsigned int noio_flag;
59e64739
DW
243 int cmd_rc, rc;
244
245 if (!nvdimm_bus)
246 return -ENXIO;
247
248 nd_desc = nvdimm_bus->nd_desc;
1e8b8d96
DJ
249 /*
250 * if ndctl does not exist, it's PMEM_LEGACY and
251 * we want to just pretend everything is handled.
252 */
59e64739 253 if (!nd_desc->ndctl)
1e8b8d96 254 return len;
59e64739
DW
255
256 memset(&ars_cap, 0, sizeof(ars_cap));
257 ars_cap.address = phys;
258 ars_cap.length = len;
0930a750 259 noio_flag = memalloc_noio_save();
59e64739
DW
260 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
261 sizeof(ars_cap), &cmd_rc);
0930a750 262 memalloc_noio_restore(noio_flag);
59e64739
DW
263 if (rc < 0)
264 return rc;
265 if (cmd_rc < 0)
266 return cmd_rc;
267 clear_err_unit = ars_cap.clear_err_unit;
268 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
269 return -ENXIO;
270
271 mask = clear_err_unit - 1;
272 if ((phys | len) & mask)
273 return -ENXIO;
274 memset(&clear_err, 0, sizeof(clear_err));
275 clear_err.address = phys;
276 clear_err.length = len;
0930a750 277 noio_flag = memalloc_noio_save();
59e64739
DW
278 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
279 sizeof(clear_err), &cmd_rc);
0930a750 280 memalloc_noio_restore(noio_flag);
59e64739
DW
281 if (rc < 0)
282 return rc;
283 if (cmd_rc < 0)
284 return cmd_rc;
e046114a 285
23f49844 286 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
8d13c029 287
59e64739
DW
288 return clear_err.cleared;
289}
290EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
291
18515942
DW
292static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
293
4d88a97a 294static struct bus_type nvdimm_bus_type = {
e6dfb2de 295 .name = "nd",
4d88a97a
DW
296 .uevent = nvdimm_bus_uevent,
297 .match = nvdimm_bus_match,
298 .probe = nvdimm_bus_probe,
299 .remove = nvdimm_bus_remove,
476f848a 300 .shutdown = nvdimm_bus_shutdown,
4d88a97a
DW
301};
302
18515942
DW
303static void nvdimm_bus_release(struct device *dev)
304{
305 struct nvdimm_bus *nvdimm_bus;
306
307 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
308 ida_simple_remove(&nd_ida, nvdimm_bus->id);
309 kfree(nvdimm_bus);
310}
311
312static bool is_nvdimm_bus(struct device *dev)
313{
314 return dev->release == nvdimm_bus_release;
315}
316
317struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
318{
319 struct device *dev;
320
321 for (dev = nd_dev; dev; dev = dev->parent)
322 if (is_nvdimm_bus(dev))
323 break;
324 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
325 if (dev)
326 return to_nvdimm_bus(dev);
327 return NULL;
328}
329
330struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
331{
332 struct nvdimm_bus *nvdimm_bus;
333
334 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
335 WARN_ON(!is_nvdimm_bus(dev));
336 return nvdimm_bus;
337}
338EXPORT_SYMBOL_GPL(to_nvdimm_bus);
339
340struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
341 struct nvdimm_bus_descriptor *nd_desc)
342{
343 struct nvdimm_bus *nvdimm_bus;
344 int rc;
345
346 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
347 if (!nvdimm_bus)
348 return NULL;
349 INIT_LIST_HEAD(&nvdimm_bus->list);
350 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
18515942
DW
351 init_waitqueue_head(&nvdimm_bus->probe_wait);
352 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
353 mutex_init(&nvdimm_bus->reconfig_mutex);
aa9ad44a 354 badrange_init(&nvdimm_bus->badrange);
18515942
DW
355 if (nvdimm_bus->id < 0) {
356 kfree(nvdimm_bus);
357 return NULL;
358 }
359 nvdimm_bus->nd_desc = nd_desc;
360 nvdimm_bus->dev.parent = parent;
361 nvdimm_bus->dev.release = nvdimm_bus_release;
362 nvdimm_bus->dev.groups = nd_desc->attr_groups;
363 nvdimm_bus->dev.bus = &nvdimm_bus_type;
1ff19f48 364 nvdimm_bus->dev.of_node = nd_desc->of_node;
18515942
DW
365 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
366 rc = device_register(&nvdimm_bus->dev);
367 if (rc) {
368 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
369 goto err;
370 }
371
372 return nvdimm_bus;
373 err:
374 put_device(&nvdimm_bus->dev);
375 return NULL;
376}
377EXPORT_SYMBOL_GPL(nvdimm_bus_register);
378
379void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
380{
381 if (!nvdimm_bus)
382 return;
383 device_unregister(&nvdimm_bus->dev);
384}
385EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
386
387static int child_unregister(struct device *dev, void *data)
388{
389 /*
390 * the singular ndctl class device per bus needs to be
391 * "device_destroy"ed, so skip it here
392 *
393 * i.e. remove classless children
394 */
395 if (dev->class)
396 /* pass */;
397 else
398 nd_device_unregister(dev, ND_SYNC);
399 return 0;
400}
401
aa9ad44a 402static void free_badrange_list(struct list_head *badrange_list)
18515942 403{
aa9ad44a 404 struct badrange_entry *bre, *next;
18515942 405
aa9ad44a
DJ
406 list_for_each_entry_safe(bre, next, badrange_list, list) {
407 list_del(&bre->list);
408 kfree(bre);
18515942 409 }
aa9ad44a 410 list_del_init(badrange_list);
18515942
DW
411}
412
413static int nd_bus_remove(struct device *dev)
414{
415 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
416
417 mutex_lock(&nvdimm_bus_list_mutex);
418 list_del_init(&nvdimm_bus->list);
419 mutex_unlock(&nvdimm_bus_list_mutex);
420
421 nd_synchronize();
422 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
423
aa9ad44a
DJ
424 spin_lock(&nvdimm_bus->badrange.lock);
425 free_badrange_list(&nvdimm_bus->badrange.list);
426 spin_unlock(&nvdimm_bus->badrange.lock);
18515942
DW
427
428 nvdimm_bus_destroy_ndctl(nvdimm_bus);
429
430 return 0;
431}
432
433static int nd_bus_probe(struct device *dev)
434{
435 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
436 int rc;
437
438 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
439 if (rc)
440 return rc;
441
442 mutex_lock(&nvdimm_bus_list_mutex);
443 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
444 mutex_unlock(&nvdimm_bus_list_mutex);
445
446 /* enable bus provider attributes to look up their local context */
447 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
448
449 return 0;
450}
451
452static struct nd_device_driver nd_bus_driver = {
453 .probe = nd_bus_probe,
454 .remove = nd_bus_remove,
455 .drv = {
456 .name = "nd_bus",
457 .suppress_bind_attrs = true,
458 .bus = &nvdimm_bus_type,
459 .owner = THIS_MODULE,
460 .mod_name = KBUILD_MODNAME,
461 },
4d88a97a
DW
462};
463
18515942
DW
464static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
465{
466 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
467
468 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
469 return true;
470
471 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
472}
473
4d88a97a
DW
474static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
475
476void nd_synchronize(void)
477{
478 async_synchronize_full_domain(&nd_async_domain);
479}
480EXPORT_SYMBOL_GPL(nd_synchronize);
481
482static void nd_async_device_register(void *d, async_cookie_t cookie)
483{
484 struct device *dev = d;
485
486 if (device_add(dev) != 0) {
487 dev_err(dev, "%s: failed\n", __func__);
488 put_device(dev);
489 }
490 put_device(dev);
491}
492
493static void nd_async_device_unregister(void *d, async_cookie_t cookie)
494{
495 struct device *dev = d;
496
0ba1c634
DW
497 /* flush bus operations before delete */
498 nvdimm_bus_lock(dev);
499 nvdimm_bus_unlock(dev);
500
4d88a97a
DW
501 device_unregister(dev);
502 put_device(dev);
503}
504
8c2f7e86 505void __nd_device_register(struct device *dev)
4d88a97a 506{
cd03412a
DW
507 if (!dev)
508 return;
4d88a97a 509 dev->bus = &nvdimm_bus_type;
4d88a97a
DW
510 get_device(dev);
511 async_schedule_domain(nd_async_device_register, dev,
512 &nd_async_domain);
513}
8c2f7e86
DW
514
515void nd_device_register(struct device *dev)
516{
517 device_initialize(dev);
518 __nd_device_register(dev);
519}
4d88a97a
DW
520EXPORT_SYMBOL(nd_device_register);
521
522void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
523{
524 switch (mode) {
525 case ND_ASYNC:
526 get_device(dev);
527 async_schedule_domain(nd_async_device_unregister, dev,
528 &nd_async_domain);
529 break;
530 case ND_SYNC:
531 nd_synchronize();
532 device_unregister(dev);
533 break;
534 }
535}
536EXPORT_SYMBOL(nd_device_unregister);
537
538/**
539 * __nd_driver_register() - register a region or a namespace driver
540 * @nd_drv: driver to register
541 * @owner: automatically set by nd_driver_register() macro
542 * @mod_name: automatically set by nd_driver_register() macro
543 */
544int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
545 const char *mod_name)
546{
547 struct device_driver *drv = &nd_drv->drv;
548
549 if (!nd_drv->type) {
550 pr_debug("driver type bitmask not set (%pf)\n",
551 __builtin_return_address(0));
552 return -EINVAL;
553 }
554
6cf9c5ba
DW
555 if (!nd_drv->probe) {
556 pr_debug("%s ->probe() must be specified\n", mod_name);
4d88a97a
DW
557 return -EINVAL;
558 }
559
560 drv->bus = &nvdimm_bus_type;
561 drv->owner = owner;
562 drv->mod_name = mod_name;
563
564 return driver_register(drv);
565}
566EXPORT_SYMBOL(__nd_driver_register);
567
58138820
DW
568int nvdimm_revalidate_disk(struct gendisk *disk)
569{
52c44d93 570 struct device *dev = disk_to_dev(disk)->parent;
58138820 571 struct nd_region *nd_region = to_nd_region(dev->parent);
254a4cd5 572 int disk_ro = get_disk_ro(disk);
58138820 573
254a4cd5
RE
574 /*
575 * Upgrade to read-only if the region is read-only preserve as
576 * read-only if the disk is already read-only.
577 */
578 if (disk_ro || nd_region->ro == disk_ro)
58138820
DW
579 return 0;
580
254a4cd5
RE
581 dev_info(dev, "%s read-only, marking %s read-only\n",
582 dev_name(&nd_region->dev), disk->disk_name);
583 set_disk_ro(disk, 1);
58138820
DW
584
585 return 0;
586
587}
588EXPORT_SYMBOL(nvdimm_revalidate_disk);
589
4d88a97a
DW
590static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
591 char *buf)
592{
593 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
594 to_nd_device_type(dev));
595}
596static DEVICE_ATTR_RO(modalias);
597
598static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
599 char *buf)
600{
601 return sprintf(buf, "%s\n", dev->type->name);
602}
603static DEVICE_ATTR_RO(devtype);
604
605static struct attribute *nd_device_attributes[] = {
606 &dev_attr_modalias.attr,
607 &dev_attr_devtype.attr,
608 NULL,
609};
610
611/**
612 * nd_device_attribute_group - generic attributes for all devices on an nd bus
613 */
614struct attribute_group nd_device_attribute_group = {
615 .attrs = nd_device_attributes,
e6dfb2de 616};
4d88a97a 617EXPORT_SYMBOL_GPL(nd_device_attribute_group);
e6dfb2de 618
74ae66c3
TK
619static ssize_t numa_node_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
621{
622 return sprintf(buf, "%d\n", dev_to_node(dev));
623}
624static DEVICE_ATTR_RO(numa_node);
625
626static struct attribute *nd_numa_attributes[] = {
627 &dev_attr_numa_node.attr,
628 NULL,
629};
630
631static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
632 int n)
633{
634 if (!IS_ENABLED(CONFIG_NUMA))
635 return 0;
636
637 return a->mode;
638}
639
640/**
641 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
642 */
643struct attribute_group nd_numa_attribute_group = {
644 .attrs = nd_numa_attributes,
645 .is_visible = nd_numa_attr_visible,
646};
647EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
648
45def22c
DW
649int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
650{
651 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
652 struct device *dev;
653
654 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
655 "ndctl%d", nvdimm_bus->id);
656
42588958 657 if (IS_ERR(dev))
45def22c
DW
658 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
659 nvdimm_bus->id, PTR_ERR(dev));
42588958 660 return PTR_ERR_OR_ZERO(dev);
45def22c
DW
661}
662
663void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
664{
665 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
666}
667
62232e45
DW
668static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
669 [ND_CMD_IMPLEMENTED] = { },
670 [ND_CMD_SMART] = {
671 .out_num = 2,
21129112 672 .out_sizes = { 4, 128, },
62232e45
DW
673 },
674 [ND_CMD_SMART_THRESHOLD] = {
675 .out_num = 2,
676 .out_sizes = { 4, 8, },
677 },
678 [ND_CMD_DIMM_FLAGS] = {
679 .out_num = 2,
680 .out_sizes = { 4, 4 },
681 },
682 [ND_CMD_GET_CONFIG_SIZE] = {
683 .out_num = 3,
684 .out_sizes = { 4, 4, 4, },
685 },
686 [ND_CMD_GET_CONFIG_DATA] = {
687 .in_num = 2,
688 .in_sizes = { 4, 4, },
689 .out_num = 2,
690 .out_sizes = { 4, UINT_MAX, },
691 },
692 [ND_CMD_SET_CONFIG_DATA] = {
693 .in_num = 3,
694 .in_sizes = { 4, 4, UINT_MAX, },
695 .out_num = 1,
696 .out_sizes = { 4, },
697 },
698 [ND_CMD_VENDOR] = {
699 .in_num = 3,
700 .in_sizes = { 4, 4, UINT_MAX, },
701 .out_num = 3,
702 .out_sizes = { 4, 4, UINT_MAX, },
703 },
31eca76b
DW
704 [ND_CMD_CALL] = {
705 .in_num = 2,
706 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
707 .out_num = 1,
708 .out_sizes = { UINT_MAX, },
709 },
62232e45
DW
710};
711
712const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
713{
714 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
715 return &__nd_cmd_dimm_descs[cmd];
716 return NULL;
717}
718EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
719
720static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
721 [ND_CMD_IMPLEMENTED] = { },
722 [ND_CMD_ARS_CAP] = {
723 .in_num = 2,
724 .in_sizes = { 8, 8, },
4577b066
DW
725 .out_num = 4,
726 .out_sizes = { 4, 4, 4, 4, },
62232e45
DW
727 },
728 [ND_CMD_ARS_START] = {
4577b066
DW
729 .in_num = 5,
730 .in_sizes = { 8, 8, 2, 1, 5, },
731 .out_num = 2,
732 .out_sizes = { 4, 4, },
62232e45
DW
733 },
734 [ND_CMD_ARS_STATUS] = {
747ffe11
DW
735 .out_num = 3,
736 .out_sizes = { 4, 4, UINT_MAX, },
62232e45 737 },
d4f32367
DW
738 [ND_CMD_CLEAR_ERROR] = {
739 .in_num = 2,
740 .in_sizes = { 8, 8, },
741 .out_num = 3,
742 .out_sizes = { 4, 4, 8, },
743 },
31eca76b
DW
744 [ND_CMD_CALL] = {
745 .in_num = 2,
746 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
747 .out_num = 1,
748 .out_sizes = { UINT_MAX, },
749 },
62232e45
DW
750};
751
752const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
753{
754 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
755 return &__nd_cmd_bus_descs[cmd];
756 return NULL;
757}
758EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
759
760u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
761 const struct nd_cmd_desc *desc, int idx, void *buf)
762{
763 if (idx >= desc->in_num)
764 return UINT_MAX;
765
766 if (desc->in_sizes[idx] < UINT_MAX)
767 return desc->in_sizes[idx];
768
769 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
770 struct nd_cmd_set_config_hdr *hdr = buf;
771
772 return hdr->in_length;
773 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
774 struct nd_cmd_vendor_hdr *hdr = buf;
775
776 return hdr->in_length;
31eca76b
DW
777 } else if (cmd == ND_CMD_CALL) {
778 struct nd_cmd_pkg *pkg = buf;
779
780 return pkg->nd_size_in;
62232e45
DW
781 }
782
783 return UINT_MAX;
784}
785EXPORT_SYMBOL_GPL(nd_cmd_in_size);
786
787u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
788 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
efda1b5d 789 const u32 *out_field, unsigned long remainder)
62232e45
DW
790{
791 if (idx >= desc->out_num)
792 return UINT_MAX;
793
794 if (desc->out_sizes[idx] < UINT_MAX)
795 return desc->out_sizes[idx];
796
797 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
798 return in_field[1];
799 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
800 return out_field[1];
efda1b5d
DW
801 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
802 /*
803 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
804 * "Size of Output Buffer in bytes, including this
805 * field."
806 */
807 if (out_field[1] < 4)
808 return 0;
809 /*
810 * ACPI 6.1 is ambiguous if 'status' is included in the
811 * output size. If we encounter an output size that
812 * overshoots the remainder by 4 bytes, assume it was
813 * including 'status'.
814 */
815 if (out_field[1] - 8 == remainder)
816 return remainder;
817 return out_field[1] - 4;
818 } else if (cmd == ND_CMD_CALL) {
31eca76b
DW
819 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
820
821 return pkg->nd_size_out;
822 }
823
62232e45
DW
824
825 return UINT_MAX;
826}
827EXPORT_SYMBOL_GPL(nd_cmd_out_size);
828
bf9bccc1 829void wait_nvdimm_bus_probe_idle(struct device *dev)
eaf96153 830{
bf9bccc1
DW
831 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
832
eaf96153
DW
833 do {
834 if (nvdimm_bus->probe_active == 0)
835 break;
836 nvdimm_bus_unlock(&nvdimm_bus->dev);
837 wait_event(nvdimm_bus->probe_wait,
838 nvdimm_bus->probe_active == 0);
839 nvdimm_bus_lock(&nvdimm_bus->dev);
840 } while (true);
841}
842
006358b3 843static int nd_pmem_forget_poison_check(struct device *dev, void *data)
d4f32367 844{
006358b3
DJ
845 struct nd_cmd_clear_error *clear_err =
846 (struct nd_cmd_clear_error *)data;
847 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
848 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
849 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
850 struct nd_namespace_common *ndns = NULL;
851 struct nd_namespace_io *nsio;
852 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
853
854 if (nd_dax || !dev->driver)
855 return 0;
856
857 start = clear_err->address;
858 end = clear_err->address + clear_err->cleared - 1;
859
860 if (nd_btt || nd_pfn || nd_dax) {
861 if (nd_btt)
862 ndns = nd_btt->ndns;
863 else if (nd_pfn)
864 ndns = nd_pfn->ndns;
865 else if (nd_dax)
866 ndns = nd_dax->nd_pfn.ndns;
867
868 if (!ndns)
869 return 0;
870 } else
871 ndns = to_ndns(dev);
872
873 nsio = to_nd_namespace_io(&ndns->dev);
874 pstart = nsio->res.start + offset;
875 pend = nsio->res.end - end_trunc;
876
877 if ((pstart >= start) && (pend <= end))
d4f32367 878 return -EBUSY;
006358b3 879
d4f32367 880 return 0;
006358b3
DJ
881
882}
883
884static int nd_ns_forget_poison_check(struct device *dev, void *data)
885{
886 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
d4f32367
DW
887}
888
eaf96153 889/* set_config requires an idle interleave set */
87bf572e 890static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
006358b3 891 struct nvdimm *nvdimm, unsigned int cmd, void *data)
eaf96153 892{
87bf572e
DW
893 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
894
895 /* ask the bus provider if it would like to block this request */
896 if (nd_desc->clear_to_send) {
897 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
898
899 if (rc)
900 return rc;
901 }
eaf96153 902
d4f32367
DW
903 /* require clear error to go through the pmem driver */
904 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
006358b3
DJ
905 return device_for_each_child(&nvdimm_bus->dev, data,
906 nd_ns_forget_poison_check);
d4f32367 907
eaf96153
DW
908 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
909 return 0;
910
87bf572e 911 /* prevent label manipulation while the kernel owns label updates */
bf9bccc1 912 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
eaf96153
DW
913 if (atomic_read(&nvdimm->busy))
914 return -EBUSY;
915 return 0;
916}
917
62232e45
DW
918static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
919 int read_only, unsigned int ioctl_cmd, unsigned long arg)
920{
921 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
62232e45
DW
922 static char out_env[ND_CMD_MAX_ENVELOPE];
923 static char in_env[ND_CMD_MAX_ENVELOPE];
924 const struct nd_cmd_desc *desc = NULL;
925 unsigned int cmd = _IOC_NR(ioctl_cmd);
62232e45 926 struct device *dev = &nvdimm_bus->dev;
58738c49 927 void __user *p = (void __user *) arg;
62232e45 928 const char *cmd_name, *dimm_name;
58738c49
DW
929 u32 in_len = 0, out_len = 0;
930 unsigned int func = cmd;
e3654eca 931 unsigned long cmd_mask;
58738c49 932 struct nd_cmd_pkg pkg;
006358b3 933 int rc, i, cmd_rc;
58738c49
DW
934 u64 buf_len = 0;
935 void *buf;
62232e45
DW
936
937 if (nvdimm) {
938 desc = nd_cmd_dimm_desc(cmd);
939 cmd_name = nvdimm_cmd_name(cmd);
e3654eca 940 cmd_mask = nvdimm->cmd_mask;
62232e45
DW
941 dimm_name = dev_name(&nvdimm->dev);
942 } else {
943 desc = nd_cmd_bus_desc(cmd);
944 cmd_name = nvdimm_bus_cmd_name(cmd);
e3654eca 945 cmd_mask = nd_desc->cmd_mask;
62232e45
DW
946 dimm_name = "bus";
947 }
948
31eca76b
DW
949 if (cmd == ND_CMD_CALL) {
950 if (copy_from_user(&pkg, p, sizeof(pkg)))
951 return -EFAULT;
952 }
953
62232e45 954 if (!desc || (desc->out_num + desc->in_num == 0) ||
e3654eca 955 !test_bit(cmd, &cmd_mask))
62232e45
DW
956 return -ENOTTY;
957
958 /* fail write commands (when read-only) */
959 if (read_only)
07accfa9
JH
960 switch (cmd) {
961 case ND_CMD_VENDOR:
962 case ND_CMD_SET_CONFIG_DATA:
963 case ND_CMD_ARS_START:
d4f32367 964 case ND_CMD_CLEAR_ERROR:
31eca76b 965 case ND_CMD_CALL:
62232e45
DW
966 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
967 nvdimm ? nvdimm_cmd_name(cmd)
968 : nvdimm_bus_cmd_name(cmd));
969 return -EPERM;
970 default:
971 break;
972 }
973
974 /* process an input envelope */
975 for (i = 0; i < desc->in_num; i++) {
976 u32 in_size, copy;
977
978 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
979 if (in_size == UINT_MAX) {
980 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
981 __func__, dimm_name, cmd_name, i);
982 return -ENXIO;
983 }
62232e45
DW
984 if (in_len < sizeof(in_env))
985 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
986 else
987 copy = 0;
988 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
989 return -EFAULT;
990 in_len += in_size;
991 }
992
31eca76b 993 if (cmd == ND_CMD_CALL) {
53b85a44 994 func = pkg.nd_command;
426824d6
DW
995 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
996 dimm_name, pkg.nd_command,
31eca76b 997 in_len, out_len, buf_len);
31eca76b
DW
998 }
999
62232e45
DW
1000 /* process an output envelope */
1001 for (i = 0; i < desc->out_num; i++) {
1002 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
efda1b5d 1003 (u32 *) in_env, (u32 *) out_env, 0);
62232e45
DW
1004 u32 copy;
1005
1006 if (out_size == UINT_MAX) {
426824d6
DW
1007 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1008 dimm_name, cmd_name, i);
62232e45
DW
1009 return -EFAULT;
1010 }
62232e45
DW
1011 if (out_len < sizeof(out_env))
1012 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1013 else
1014 copy = 0;
1015 if (copy && copy_from_user(&out_env[out_len],
1016 p + in_len + out_len, copy))
1017 return -EFAULT;
1018 out_len += out_size;
1019 }
1020
58738c49 1021 buf_len = (u64) out_len + (u64) in_len;
62232e45 1022 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
426824d6
DW
1023 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1024 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
62232e45
DW
1025 return -EINVAL;
1026 }
1027
1028 buf = vmalloc(buf_len);
1029 if (!buf)
1030 return -ENOMEM;
1031
1032 if (copy_from_user(buf, p, buf_len)) {
1033 rc = -EFAULT;
1034 goto out;
1035 }
1036
eaf96153 1037 nvdimm_bus_lock(&nvdimm_bus->dev);
53b85a44 1038 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
eaf96153
DW
1039 if (rc)
1040 goto out_unlock;
1041
006358b3 1042 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
62232e45 1043 if (rc < 0)
eaf96153 1044 goto out_unlock;
006358b3
DJ
1045
1046 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1047 struct nd_cmd_clear_error *clear_err = buf;
006358b3 1048
23f49844
DW
1049 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1050 clear_err->cleared);
006358b3 1051 }
0beb2012
DW
1052 nvdimm_bus_unlock(&nvdimm_bus->dev);
1053
62232e45
DW
1054 if (copy_to_user(p, buf, buf_len))
1055 rc = -EFAULT;
0beb2012
DW
1056
1057 vfree(buf);
1058 return rc;
1059
eaf96153
DW
1060 out_unlock:
1061 nvdimm_bus_unlock(&nvdimm_bus->dev);
62232e45
DW
1062 out:
1063 vfree(buf);
1064 return rc;
1065}
1066
45def22c
DW
1067static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1068{
62232e45 1069 long id = (long) file->private_data;
4dc0e7be 1070 int rc = -ENXIO, ro;
62232e45
DW
1071 struct nvdimm_bus *nvdimm_bus;
1072
4dc0e7be 1073 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
62232e45
DW
1074 mutex_lock(&nvdimm_bus_list_mutex);
1075 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1076 if (nvdimm_bus->id == id) {
4dc0e7be 1077 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
62232e45
DW
1078 break;
1079 }
1080 }
1081 mutex_unlock(&nvdimm_bus_list_mutex);
1082
1083 return rc;
1084}
1085
1086static int match_dimm(struct device *dev, void *data)
1087{
1088 long id = (long) data;
1089
1090 if (is_nvdimm(dev)) {
1091 struct nvdimm *nvdimm = to_nvdimm(dev);
1092
1093 return nvdimm->id == id;
1094 }
1095
1096 return 0;
1097}
1098
1099static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1100{
4dc0e7be 1101 int rc = -ENXIO, ro;
62232e45
DW
1102 struct nvdimm_bus *nvdimm_bus;
1103
4dc0e7be 1104 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
62232e45
DW
1105 mutex_lock(&nvdimm_bus_list_mutex);
1106 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1107 struct device *dev = device_find_child(&nvdimm_bus->dev,
1108 file->private_data, match_dimm);
1109 struct nvdimm *nvdimm;
1110
1111 if (!dev)
1112 continue;
1113
1114 nvdimm = to_nvdimm(dev);
4dc0e7be 1115 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
62232e45
DW
1116 put_device(dev);
1117 break;
1118 }
1119 mutex_unlock(&nvdimm_bus_list_mutex);
1120
1121 return rc;
1122}
1123
1124static int nd_open(struct inode *inode, struct file *file)
1125{
1126 long minor = iminor(inode);
1127
1128 file->private_data = (void *) minor;
1129 return 0;
45def22c
DW
1130}
1131
1132static const struct file_operations nvdimm_bus_fops = {
1133 .owner = THIS_MODULE,
62232e45 1134 .open = nd_open,
45def22c
DW
1135 .unlocked_ioctl = nd_ioctl,
1136 .compat_ioctl = nd_ioctl,
1137 .llseek = noop_llseek,
1138};
1139
62232e45
DW
1140static const struct file_operations nvdimm_fops = {
1141 .owner = THIS_MODULE,
1142 .open = nd_open,
1143 .unlocked_ioctl = nvdimm_ioctl,
1144 .compat_ioctl = nvdimm_ioctl,
1145 .llseek = noop_llseek,
1146};
1147
45def22c
DW
1148int __init nvdimm_bus_init(void)
1149{
1150 int rc;
1151
e6dfb2de
DW
1152 rc = bus_register(&nvdimm_bus_type);
1153 if (rc)
1154 return rc;
1155
45def22c
DW
1156 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1157 if (rc < 0)
62232e45 1158 goto err_bus_chrdev;
45def22c
DW
1159 nvdimm_bus_major = rc;
1160
62232e45
DW
1161 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1162 if (rc < 0)
1163 goto err_dimm_chrdev;
1164 nvdimm_major = rc;
1165
45def22c 1166 nd_class = class_create(THIS_MODULE, "nd");
daa1dee4
AL
1167 if (IS_ERR(nd_class)) {
1168 rc = PTR_ERR(nd_class);
45def22c 1169 goto err_class;
daa1dee4 1170 }
45def22c 1171
18515942
DW
1172 rc = driver_register(&nd_bus_driver.drv);
1173 if (rc)
1174 goto err_nd_bus;
1175
45def22c
DW
1176 return 0;
1177
18515942
DW
1178 err_nd_bus:
1179 class_destroy(nd_class);
45def22c 1180 err_class:
62232e45
DW
1181 unregister_chrdev(nvdimm_major, "dimmctl");
1182 err_dimm_chrdev:
45def22c 1183 unregister_chrdev(nvdimm_bus_major, "ndctl");
62232e45 1184 err_bus_chrdev:
e6dfb2de 1185 bus_unregister(&nvdimm_bus_type);
45def22c
DW
1186
1187 return rc;
1188}
1189
4d88a97a 1190void nvdimm_bus_exit(void)
45def22c 1191{
18515942 1192 driver_unregister(&nd_bus_driver.drv);
45def22c
DW
1193 class_destroy(nd_class);
1194 unregister_chrdev(nvdimm_bus_major, "ndctl");
62232e45 1195 unregister_chrdev(nvdimm_major, "dimmctl");
e6dfb2de 1196 bus_unregister(&nvdimm_bus_type);
18515942 1197 ida_destroy(&nd_ida);
45def22c 1198}