blob: 937fafa1886a8056f08ae809a129eb43f90daffd [file] [log] [blame]
Dan Williams45def222015-04-26 19:26:48 -04001/*
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
Dan Williams62232e452015-06-08 14:27:06 -040014#include <linux/vmalloc.h>
Dan Williams45def222015-04-26 19:26:48 -040015#include <linux/uaccess.h>
Dan Williams3d880022015-05-31 15:02:11 -040016#include <linux/module.h>
Dan Williams8c2f7e82015-06-25 04:20:04 -040017#include <linux/blkdev.h>
Dan Williams45def222015-04-26 19:26:48 -040018#include <linux/fcntl.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -040019#include <linux/async.h>
Dan Williams8c2f7e82015-06-25 04:20:04 -040020#include <linux/genhd.h>
Dan Williams62232e452015-06-08 14:27:06 -040021#include <linux/ndctl.h>
Dan Williams4d88a972015-05-31 14:41:48 -040022#include <linux/sched.h>
Dan Williams45def222015-04-26 19:26:48 -040023#include <linux/slab.h>
24#include <linux/fs.h>
25#include <linux/io.h>
Dan Williams62232e452015-06-08 14:27:06 -040026#include <linux/mm.h>
Dan Williams4d88a972015-05-31 14:41:48 -040027#include <linux/nd.h>
Dan Williams45def222015-04-26 19:26:48 -040028#include "nd-core.h"
Dan Williams4d88a972015-05-31 14:41:48 -040029#include "nd.h"
Dave Jiang006358b2017-04-07 15:33:31 -070030#include "pfn.h"
Dan Williams45def222015-04-26 19:26:48 -040031
Dan Williams62232e452015-06-08 14:27:06 -040032int nvdimm_major;
Dan Williams45def222015-04-26 19:26:48 -040033static int nvdimm_bus_major;
34static struct class *nd_class;
Dan Williams18515942016-07-22 23:46:08 -070035static DEFINE_IDA(nd_ida);
Dan Williams45def222015-04-26 19:26:48 -040036
Dan Williams4d88a972015-05-31 14:41:48 -040037static int to_nd_device_type(struct device *dev)
38{
39 if (is_nvdimm(dev))
40 return ND_DEVICE_DIMM;
Dan Williamsc9e582a2017-05-29 23:12:19 -070041 else if (is_memory(dev))
Dan Williams3d880022015-05-31 15:02:11 -040042 return ND_DEVICE_REGION_PMEM;
43 else if (is_nd_blk(dev))
44 return ND_DEVICE_REGION_BLK;
Dan Williamscd034122016-03-11 10:15:36 -080045 else if (is_nd_dax(dev))
46 return ND_DEVICE_DAX_PMEM;
Dan Williamsc9e582a2017-05-29 23:12:19 -070047 else if (is_nd_region(dev->parent))
Dan Williams3d880022015-05-31 15:02:11 -040048 return nd_region_to_nstype(to_nd_region(dev->parent));
Dan Williams4d88a972015-05-31 14:41:48 -040049
50 return 0;
51}
52
53static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
54{
Toshi Kani41d7a6d2015-06-19 12:18:33 -060055 /*
56 * Ensure that region devices always have their numa node set as
57 * early as possible.
58 */
Dan Williamsc9e582a2017-05-29 23:12:19 -070059 if (is_nd_region(dev))
Toshi Kani41d7a6d2015-06-19 12:18:33 -060060 set_dev_node(dev, to_nd_region(dev)->numa_node);
Dan Williams4d88a972015-05-31 14:41:48 -040061 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
62 to_nd_device_type(dev));
63}
64
Dan Williams3d880022015-05-31 15:02:11 -040065static struct module *to_bus_provider(struct device *dev)
66{
67 /* pin bus providers while regions are enabled */
Dan Williamsc9e582a2017-05-29 23:12:19 -070068 if (is_nd_region(dev)) {
Dan Williams3d880022015-05-31 15:02:11 -040069 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
70
Dan Williamsbc9775d2016-07-21 20:03:19 -070071 return nvdimm_bus->nd_desc->module;
Dan Williams3d880022015-05-31 15:02:11 -040072 }
73 return NULL;
74}
75
Dan Williamseaf96152015-05-01 13:11:27 -040076static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
77{
78 nvdimm_bus_lock(&nvdimm_bus->dev);
79 nvdimm_bus->probe_active++;
80 nvdimm_bus_unlock(&nvdimm_bus->dev);
81}
82
83static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
84{
85 nvdimm_bus_lock(&nvdimm_bus->dev);
86 if (--nvdimm_bus->probe_active == 0)
87 wake_up(&nvdimm_bus->probe_wait);
88 nvdimm_bus_unlock(&nvdimm_bus->dev);
89}
90
Dan Williams4d88a972015-05-31 14:41:48 -040091static int nvdimm_bus_probe(struct device *dev)
92{
93 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -040094 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040095 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
96 int rc;
97
Dan Williams3d880022015-05-31 15:02:11 -040098 if (!try_module_get(provider))
99 return -ENXIO;
100
Dan Williamseaf96152015-05-01 13:11:27 -0400101 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -0400102 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400103 if (rc == 0)
104 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400105 else
106 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400107 nvdimm_bus_probe_end(nvdimm_bus);
108
Dan Williams4d88a972015-05-31 14:41:48 -0400109 dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
110 dev_name(dev), rc);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400111
Dan Williams3d880022015-05-31 15:02:11 -0400112 if (rc != 0)
113 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400114 return rc;
115}
116
117static int nvdimm_bus_remove(struct device *dev)
118{
119 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -0400120 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400121 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700122 int rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -0400123
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700124 if (nd_drv->remove)
125 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400126 nd_region_disable(nvdimm_bus, dev);
127
Dan Williams4d88a972015-05-31 14:41:48 -0400128 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
129 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400130 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400131 return rc;
132}
133
Dan Williams476f8482016-07-09 00:12:52 -0700134static void nvdimm_bus_shutdown(struct device *dev)
135{
136 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
137 struct nd_device_driver *nd_drv = NULL;
138
139 if (dev->driver)
140 nd_drv = to_nd_device_driver(dev->driver);
141
142 if (nd_drv && nd_drv->shutdown) {
143 nd_drv->shutdown(dev);
144 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
145 dev->driver->name, dev_name(dev));
146 }
147}
148
Dan Williams71999462016-02-18 10:29:49 -0800149void nd_device_notify(struct device *dev, enum nvdimm_event event)
150{
151 device_lock(dev);
152 if (dev->driver) {
153 struct nd_device_driver *nd_drv;
154
155 nd_drv = to_nd_device_driver(dev->driver);
156 if (nd_drv->notify)
157 nd_drv->notify(dev, event);
158 }
159 device_unlock(dev);
160}
161EXPORT_SYMBOL(nd_device_notify);
162
163void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
164{
165 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
166
167 if (!nvdimm_bus)
168 return;
169
170 /* caller is responsible for holding a reference on the device */
171 nd_device_notify(&nd_region->dev, event);
172}
173EXPORT_SYMBOL_GPL(nvdimm_region_notify);
174
Dan Williams23f49842017-04-29 15:24:03 -0700175struct clear_badblocks_context {
176 resource_size_t phys, cleared;
177};
178
179static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
180{
181 struct clear_badblocks_context *ctx = data;
182 struct nd_region *nd_region;
183 resource_size_t ndr_end;
184 sector_t sector;
185
186 /* make sure device is a region */
187 if (!is_nd_pmem(dev))
188 return 0;
189
190 nd_region = to_nd_region(dev);
191 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
192
193 /* make sure we are in the region */
194 if (ctx->phys < nd_region->ndr_start
195 || (ctx->phys + ctx->cleared) > ndr_end)
196 return 0;
197
198 sector = (ctx->phys - nd_region->ndr_start) / 512;
199 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
200
Toshi Kani975750a2017-06-12 16:25:11 -0600201 if (nd_region->bb_state)
202 sysfs_notify_dirent(nd_region->bb_state);
203
Dan Williams23f49842017-04-29 15:24:03 -0700204 return 0;
205}
206
207static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
208 phys_addr_t phys, u64 cleared)
209{
210 struct clear_badblocks_context ctx = {
211 .phys = phys,
212 .cleared = cleared,
213 };
214
215 device_for_each_child(&nvdimm_bus->dev, &ctx,
216 nvdimm_clear_badblocks_region);
217}
218
219static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
220 phys_addr_t phys, u64 cleared)
221{
222 if (cleared > 0)
223 nvdimm_forget_poison(nvdimm_bus, phys, cleared);
224
225 if (cleared > 0 && cleared / 512)
226 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
227}
228
Dan Williams59e64732016-03-08 07:16:07 -0800229long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
230 unsigned int len)
231{
232 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
233 struct nvdimm_bus_descriptor *nd_desc;
234 struct nd_cmd_clear_error clear_err;
235 struct nd_cmd_ars_cap ars_cap;
236 u32 clear_err_unit, mask;
237 int cmd_rc, rc;
238
239 if (!nvdimm_bus)
240 return -ENXIO;
241
242 nd_desc = nvdimm_bus->nd_desc;
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700243 /*
244 * if ndctl does not exist, it's PMEM_LEGACY and
245 * we want to just pretend everything is handled.
246 */
Dan Williams59e64732016-03-08 07:16:07 -0800247 if (!nd_desc->ndctl)
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700248 return len;
Dan Williams59e64732016-03-08 07:16:07 -0800249
250 memset(&ars_cap, 0, sizeof(ars_cap));
251 ars_cap.address = phys;
252 ars_cap.length = len;
253 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
254 sizeof(ars_cap), &cmd_rc);
255 if (rc < 0)
256 return rc;
257 if (cmd_rc < 0)
258 return cmd_rc;
259 clear_err_unit = ars_cap.clear_err_unit;
260 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
261 return -ENXIO;
262
263 mask = clear_err_unit - 1;
264 if ((phys | len) & mask)
265 return -ENXIO;
266 memset(&clear_err, 0, sizeof(clear_err));
267 clear_err.address = phys;
268 clear_err.length = len;
269 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
270 sizeof(clear_err), &cmd_rc);
271 if (rc < 0)
272 return rc;
273 if (cmd_rc < 0)
274 return cmd_rc;
Vishal Vermae0461142016-09-30 17:19:31 -0600275
Dan Williams23f49842017-04-29 15:24:03 -0700276 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
Toshi Kani8d13c022017-04-27 16:57:05 -0600277
Dan Williams59e64732016-03-08 07:16:07 -0800278 return clear_err.cleared;
279}
280EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
281
Dan Williams18515942016-07-22 23:46:08 -0700282static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
283
Dan Williams4d88a972015-05-31 14:41:48 -0400284static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400285 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400286 .uevent = nvdimm_bus_uevent,
287 .match = nvdimm_bus_match,
288 .probe = nvdimm_bus_probe,
289 .remove = nvdimm_bus_remove,
Dan Williams476f8482016-07-09 00:12:52 -0700290 .shutdown = nvdimm_bus_shutdown,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400291};
292
Dan Williams18515942016-07-22 23:46:08 -0700293static void nvdimm_bus_release(struct device *dev)
294{
295 struct nvdimm_bus *nvdimm_bus;
296
297 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
298 ida_simple_remove(&nd_ida, nvdimm_bus->id);
299 kfree(nvdimm_bus);
300}
301
302static bool is_nvdimm_bus(struct device *dev)
303{
304 return dev->release == nvdimm_bus_release;
305}
306
307struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
308{
309 struct device *dev;
310
311 for (dev = nd_dev; dev; dev = dev->parent)
312 if (is_nvdimm_bus(dev))
313 break;
314 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
315 if (dev)
316 return to_nvdimm_bus(dev);
317 return NULL;
318}
319
320struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
321{
322 struct nvdimm_bus *nvdimm_bus;
323
324 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
325 WARN_ON(!is_nvdimm_bus(dev));
326 return nvdimm_bus;
327}
328EXPORT_SYMBOL_GPL(to_nvdimm_bus);
329
330struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
331 struct nvdimm_bus_descriptor *nd_desc)
332{
333 struct nvdimm_bus *nvdimm_bus;
334 int rc;
335
336 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
337 if (!nvdimm_bus)
338 return NULL;
339 INIT_LIST_HEAD(&nvdimm_bus->list);
340 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
341 INIT_LIST_HEAD(&nvdimm_bus->poison_list);
342 init_waitqueue_head(&nvdimm_bus->probe_wait);
343 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
344 mutex_init(&nvdimm_bus->reconfig_mutex);
Dave Jiangb3b454f2017-04-13 14:25:17 -0700345 spin_lock_init(&nvdimm_bus->poison_lock);
Dan Williams18515942016-07-22 23:46:08 -0700346 if (nvdimm_bus->id < 0) {
347 kfree(nvdimm_bus);
348 return NULL;
349 }
350 nvdimm_bus->nd_desc = nd_desc;
351 nvdimm_bus->dev.parent = parent;
352 nvdimm_bus->dev.release = nvdimm_bus_release;
353 nvdimm_bus->dev.groups = nd_desc->attr_groups;
354 nvdimm_bus->dev.bus = &nvdimm_bus_type;
355 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
356 rc = device_register(&nvdimm_bus->dev);
357 if (rc) {
358 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
359 goto err;
360 }
361
362 return nvdimm_bus;
363 err:
364 put_device(&nvdimm_bus->dev);
365 return NULL;
366}
367EXPORT_SYMBOL_GPL(nvdimm_bus_register);
368
369void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
370{
371 if (!nvdimm_bus)
372 return;
373 device_unregister(&nvdimm_bus->dev);
374}
375EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
376
377static int child_unregister(struct device *dev, void *data)
378{
379 /*
380 * the singular ndctl class device per bus needs to be
381 * "device_destroy"ed, so skip it here
382 *
383 * i.e. remove classless children
384 */
385 if (dev->class)
386 /* pass */;
387 else
388 nd_device_unregister(dev, ND_SYNC);
389 return 0;
390}
391
392static void free_poison_list(struct list_head *poison_list)
393{
394 struct nd_poison *pl, *next;
395
396 list_for_each_entry_safe(pl, next, poison_list, list) {
397 list_del(&pl->list);
398 kfree(pl);
399 }
400 list_del_init(poison_list);
401}
402
403static int nd_bus_remove(struct device *dev)
404{
405 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
406
407 mutex_lock(&nvdimm_bus_list_mutex);
408 list_del_init(&nvdimm_bus->list);
409 mutex_unlock(&nvdimm_bus_list_mutex);
410
411 nd_synchronize();
412 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
413
Dave Jiangb3b454f2017-04-13 14:25:17 -0700414 spin_lock(&nvdimm_bus->poison_lock);
Dan Williams18515942016-07-22 23:46:08 -0700415 free_poison_list(&nvdimm_bus->poison_list);
Dave Jiangb3b454f2017-04-13 14:25:17 -0700416 spin_unlock(&nvdimm_bus->poison_lock);
Dan Williams18515942016-07-22 23:46:08 -0700417
418 nvdimm_bus_destroy_ndctl(nvdimm_bus);
419
420 return 0;
421}
422
423static int nd_bus_probe(struct device *dev)
424{
425 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
426 int rc;
427
428 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
429 if (rc)
430 return rc;
431
432 mutex_lock(&nvdimm_bus_list_mutex);
433 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
434 mutex_unlock(&nvdimm_bus_list_mutex);
435
436 /* enable bus provider attributes to look up their local context */
437 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
438
439 return 0;
440}
441
442static struct nd_device_driver nd_bus_driver = {
443 .probe = nd_bus_probe,
444 .remove = nd_bus_remove,
445 .drv = {
446 .name = "nd_bus",
447 .suppress_bind_attrs = true,
448 .bus = &nvdimm_bus_type,
449 .owner = THIS_MODULE,
450 .mod_name = KBUILD_MODNAME,
451 },
452};
453
454static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
455{
456 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
457
458 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
459 return true;
460
461 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
462}
463
Dan Williams4d88a972015-05-31 14:41:48 -0400464static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
465
466void nd_synchronize(void)
467{
468 async_synchronize_full_domain(&nd_async_domain);
469}
470EXPORT_SYMBOL_GPL(nd_synchronize);
471
472static void nd_async_device_register(void *d, async_cookie_t cookie)
473{
474 struct device *dev = d;
475
476 if (device_add(dev) != 0) {
477 dev_err(dev, "%s: failed\n", __func__);
478 put_device(dev);
479 }
480 put_device(dev);
481}
482
483static void nd_async_device_unregister(void *d, async_cookie_t cookie)
484{
485 struct device *dev = d;
486
Dan Williams0ba1c632015-05-30 12:35:36 -0400487 /* flush bus operations before delete */
488 nvdimm_bus_lock(dev);
489 nvdimm_bus_unlock(dev);
490
Dan Williams4d88a972015-05-31 14:41:48 -0400491 device_unregister(dev);
492 put_device(dev);
493}
494
Dan Williams8c2f7e82015-06-25 04:20:04 -0400495void __nd_device_register(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400496{
Dan Williamscd034122016-03-11 10:15:36 -0800497 if (!dev)
498 return;
Dan Williams4d88a972015-05-31 14:41:48 -0400499 dev->bus = &nvdimm_bus_type;
Dan Williams4d88a972015-05-31 14:41:48 -0400500 get_device(dev);
501 async_schedule_domain(nd_async_device_register, dev,
502 &nd_async_domain);
503}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400504
505void nd_device_register(struct device *dev)
506{
507 device_initialize(dev);
508 __nd_device_register(dev);
509}
Dan Williams4d88a972015-05-31 14:41:48 -0400510EXPORT_SYMBOL(nd_device_register);
511
512void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
513{
514 switch (mode) {
515 case ND_ASYNC:
516 get_device(dev);
517 async_schedule_domain(nd_async_device_unregister, dev,
518 &nd_async_domain);
519 break;
520 case ND_SYNC:
521 nd_synchronize();
522 device_unregister(dev);
523 break;
524 }
525}
526EXPORT_SYMBOL(nd_device_unregister);
527
528/**
529 * __nd_driver_register() - register a region or a namespace driver
530 * @nd_drv: driver to register
531 * @owner: automatically set by nd_driver_register() macro
532 * @mod_name: automatically set by nd_driver_register() macro
533 */
534int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
535 const char *mod_name)
536{
537 struct device_driver *drv = &nd_drv->drv;
538
539 if (!nd_drv->type) {
540 pr_debug("driver type bitmask not set (%pf)\n",
541 __builtin_return_address(0));
542 return -EINVAL;
543 }
544
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700545 if (!nd_drv->probe) {
546 pr_debug("%s ->probe() must be specified\n", mod_name);
Dan Williams4d88a972015-05-31 14:41:48 -0400547 return -EINVAL;
548 }
549
550 drv->bus = &nvdimm_bus_type;
551 drv->owner = owner;
552 drv->mod_name = mod_name;
553
554 return driver_register(drv);
555}
556EXPORT_SYMBOL(__nd_driver_register);
557
Dan Williams58138822015-06-23 20:08:34 -0400558int nvdimm_revalidate_disk(struct gendisk *disk)
559{
Dan Williams52c44d92016-06-15 19:43:07 -0700560 struct device *dev = disk_to_dev(disk)->parent;
Dan Williams58138822015-06-23 20:08:34 -0400561 struct nd_region *nd_region = to_nd_region(dev->parent);
562 const char *pol = nd_region->ro ? "only" : "write";
563
564 if (nd_region->ro == get_disk_ro(disk))
565 return 0;
566
567 dev_info(dev, "%s read-%s, marking %s read-%s\n",
568 dev_name(&nd_region->dev), pol, disk->disk_name, pol);
569 set_disk_ro(disk, nd_region->ro);
570
571 return 0;
572
573}
574EXPORT_SYMBOL(nvdimm_revalidate_disk);
575
Dan Williams4d88a972015-05-31 14:41:48 -0400576static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
577 char *buf)
578{
579 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
580 to_nd_device_type(dev));
581}
582static DEVICE_ATTR_RO(modalias);
583
584static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
585 char *buf)
586{
587 return sprintf(buf, "%s\n", dev->type->name);
588}
589static DEVICE_ATTR_RO(devtype);
590
591static struct attribute *nd_device_attributes[] = {
592 &dev_attr_modalias.attr,
593 &dev_attr_devtype.attr,
594 NULL,
595};
596
597/**
598 * nd_device_attribute_group - generic attributes for all devices on an nd bus
599 */
600struct attribute_group nd_device_attribute_group = {
601 .attrs = nd_device_attributes,
602};
603EXPORT_SYMBOL_GPL(nd_device_attribute_group);
604
Toshi Kani74ae66c2015-06-19 12:18:34 -0600605static ssize_t numa_node_show(struct device *dev,
606 struct device_attribute *attr, char *buf)
607{
608 return sprintf(buf, "%d\n", dev_to_node(dev));
609}
610static DEVICE_ATTR_RO(numa_node);
611
612static struct attribute *nd_numa_attributes[] = {
613 &dev_attr_numa_node.attr,
614 NULL,
615};
616
617static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
618 int n)
619{
620 if (!IS_ENABLED(CONFIG_NUMA))
621 return 0;
622
623 return a->mode;
624}
625
626/**
627 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
628 */
629struct attribute_group nd_numa_attribute_group = {
630 .attrs = nd_numa_attributes,
631 .is_visible = nd_numa_attr_visible,
632};
633EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
634
Dan Williams45def222015-04-26 19:26:48 -0400635int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
636{
637 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
638 struct device *dev;
639
640 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
641 "ndctl%d", nvdimm_bus->id);
642
Dan Williams42588952016-05-27 13:28:31 -0700643 if (IS_ERR(dev))
Dan Williams45def222015-04-26 19:26:48 -0400644 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
645 nvdimm_bus->id, PTR_ERR(dev));
Dan Williams42588952016-05-27 13:28:31 -0700646 return PTR_ERR_OR_ZERO(dev);
Dan Williams45def222015-04-26 19:26:48 -0400647}
648
649void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
650{
651 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
652}
653
Dan Williams62232e452015-06-08 14:27:06 -0400654static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
655 [ND_CMD_IMPLEMENTED] = { },
656 [ND_CMD_SMART] = {
657 .out_num = 2,
Dan Williams21129112016-04-07 19:58:44 -0700658 .out_sizes = { 4, 128, },
Dan Williams62232e452015-06-08 14:27:06 -0400659 },
660 [ND_CMD_SMART_THRESHOLD] = {
661 .out_num = 2,
662 .out_sizes = { 4, 8, },
663 },
664 [ND_CMD_DIMM_FLAGS] = {
665 .out_num = 2,
666 .out_sizes = { 4, 4 },
667 },
668 [ND_CMD_GET_CONFIG_SIZE] = {
669 .out_num = 3,
670 .out_sizes = { 4, 4, 4, },
671 },
672 [ND_CMD_GET_CONFIG_DATA] = {
673 .in_num = 2,
674 .in_sizes = { 4, 4, },
675 .out_num = 2,
676 .out_sizes = { 4, UINT_MAX, },
677 },
678 [ND_CMD_SET_CONFIG_DATA] = {
679 .in_num = 3,
680 .in_sizes = { 4, 4, UINT_MAX, },
681 .out_num = 1,
682 .out_sizes = { 4, },
683 },
684 [ND_CMD_VENDOR] = {
685 .in_num = 3,
686 .in_sizes = { 4, 4, UINT_MAX, },
687 .out_num = 3,
688 .out_sizes = { 4, 4, UINT_MAX, },
689 },
Dan Williams31eca762016-04-28 16:23:43 -0700690 [ND_CMD_CALL] = {
691 .in_num = 2,
692 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
693 .out_num = 1,
694 .out_sizes = { UINT_MAX, },
695 },
Dan Williams62232e452015-06-08 14:27:06 -0400696};
697
698const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
699{
700 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
701 return &__nd_cmd_dimm_descs[cmd];
702 return NULL;
703}
704EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
705
706static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
707 [ND_CMD_IMPLEMENTED] = { },
708 [ND_CMD_ARS_CAP] = {
709 .in_num = 2,
710 .in_sizes = { 8, 8, },
Dan Williams4577b062016-02-17 13:08:58 -0800711 .out_num = 4,
712 .out_sizes = { 4, 4, 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400713 },
714 [ND_CMD_ARS_START] = {
Dan Williams4577b062016-02-17 13:08:58 -0800715 .in_num = 5,
716 .in_sizes = { 8, 8, 2, 1, 5, },
717 .out_num = 2,
718 .out_sizes = { 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400719 },
720 [ND_CMD_ARS_STATUS] = {
Dan Williams747ffe12016-02-19 15:21:14 -0800721 .out_num = 3,
722 .out_sizes = { 4, 4, UINT_MAX, },
Dan Williams62232e452015-06-08 14:27:06 -0400723 },
Dan Williamsd4f32362016-03-03 16:08:54 -0800724 [ND_CMD_CLEAR_ERROR] = {
725 .in_num = 2,
726 .in_sizes = { 8, 8, },
727 .out_num = 3,
728 .out_sizes = { 4, 4, 8, },
729 },
Dan Williams31eca762016-04-28 16:23:43 -0700730 [ND_CMD_CALL] = {
731 .in_num = 2,
732 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
733 .out_num = 1,
734 .out_sizes = { UINT_MAX, },
735 },
Dan Williams62232e452015-06-08 14:27:06 -0400736};
737
738const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
739{
740 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
741 return &__nd_cmd_bus_descs[cmd];
742 return NULL;
743}
744EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
745
746u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
747 const struct nd_cmd_desc *desc, int idx, void *buf)
748{
749 if (idx >= desc->in_num)
750 return UINT_MAX;
751
752 if (desc->in_sizes[idx] < UINT_MAX)
753 return desc->in_sizes[idx];
754
755 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
756 struct nd_cmd_set_config_hdr *hdr = buf;
757
758 return hdr->in_length;
759 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
760 struct nd_cmd_vendor_hdr *hdr = buf;
761
762 return hdr->in_length;
Dan Williams31eca762016-04-28 16:23:43 -0700763 } else if (cmd == ND_CMD_CALL) {
764 struct nd_cmd_pkg *pkg = buf;
765
766 return pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400767 }
768
769 return UINT_MAX;
770}
771EXPORT_SYMBOL_GPL(nd_cmd_in_size);
772
773u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
774 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800775 const u32 *out_field, unsigned long remainder)
Dan Williams62232e452015-06-08 14:27:06 -0400776{
777 if (idx >= desc->out_num)
778 return UINT_MAX;
779
780 if (desc->out_sizes[idx] < UINT_MAX)
781 return desc->out_sizes[idx];
782
783 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
784 return in_field[1];
785 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
786 return out_field[1];
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800787 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
788 /*
789 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
790 * "Size of Output Buffer in bytes, including this
791 * field."
792 */
793 if (out_field[1] < 4)
794 return 0;
795 /*
796 * ACPI 6.1 is ambiguous if 'status' is included in the
797 * output size. If we encounter an output size that
798 * overshoots the remainder by 4 bytes, assume it was
799 * including 'status'.
800 */
801 if (out_field[1] - 8 == remainder)
802 return remainder;
803 return out_field[1] - 4;
804 } else if (cmd == ND_CMD_CALL) {
Dan Williams31eca762016-04-28 16:23:43 -0700805 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
806
807 return pkg->nd_size_out;
808 }
809
Dan Williams62232e452015-06-08 14:27:06 -0400810
811 return UINT_MAX;
812}
813EXPORT_SYMBOL_GPL(nd_cmd_out_size);
814
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400815void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400816{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400817 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
818
Dan Williamseaf96152015-05-01 13:11:27 -0400819 do {
820 if (nvdimm_bus->probe_active == 0)
821 break;
822 nvdimm_bus_unlock(&nvdimm_bus->dev);
823 wait_event(nvdimm_bus->probe_wait,
824 nvdimm_bus->probe_active == 0);
825 nvdimm_bus_lock(&nvdimm_bus->dev);
826 } while (true);
827}
828
Dave Jiang006358b2017-04-07 15:33:31 -0700829static int nd_pmem_forget_poison_check(struct device *dev, void *data)
Dan Williamsd4f32362016-03-03 16:08:54 -0800830{
Dave Jiang006358b2017-04-07 15:33:31 -0700831 struct nd_cmd_clear_error *clear_err =
832 (struct nd_cmd_clear_error *)data;
833 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
834 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
835 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
836 struct nd_namespace_common *ndns = NULL;
837 struct nd_namespace_io *nsio;
838 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
839
840 if (nd_dax || !dev->driver)
841 return 0;
842
843 start = clear_err->address;
844 end = clear_err->address + clear_err->cleared - 1;
845
846 if (nd_btt || nd_pfn || nd_dax) {
847 if (nd_btt)
848 ndns = nd_btt->ndns;
849 else if (nd_pfn)
850 ndns = nd_pfn->ndns;
851 else if (nd_dax)
852 ndns = nd_dax->nd_pfn.ndns;
853
854 if (!ndns)
855 return 0;
856 } else
857 ndns = to_ndns(dev);
858
859 nsio = to_nd_namespace_io(&ndns->dev);
860 pstart = nsio->res.start + offset;
861 pend = nsio->res.end - end_trunc;
862
863 if ((pstart >= start) && (pend <= end))
Dan Williamsd4f32362016-03-03 16:08:54 -0800864 return -EBUSY;
Dave Jiang006358b2017-04-07 15:33:31 -0700865
Dan Williamsd4f32362016-03-03 16:08:54 -0800866 return 0;
Dave Jiang006358b2017-04-07 15:33:31 -0700867
868}
869
870static int nd_ns_forget_poison_check(struct device *dev, void *data)
871{
872 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800873}
874
Dan Williamseaf96152015-05-01 13:11:27 -0400875/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800876static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
Dave Jiang006358b2017-04-07 15:33:31 -0700877 struct nvdimm *nvdimm, unsigned int cmd, void *data)
Dan Williamseaf96152015-05-01 13:11:27 -0400878{
Dan Williams87bf5722016-02-22 21:50:31 -0800879 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
880
881 /* ask the bus provider if it would like to block this request */
882 if (nd_desc->clear_to_send) {
883 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
884
885 if (rc)
886 return rc;
887 }
Dan Williamseaf96152015-05-01 13:11:27 -0400888
Dan Williamsd4f32362016-03-03 16:08:54 -0800889 /* require clear error to go through the pmem driver */
890 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
Dave Jiang006358b2017-04-07 15:33:31 -0700891 return device_for_each_child(&nvdimm_bus->dev, data,
892 nd_ns_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800893
Dan Williamseaf96152015-05-01 13:11:27 -0400894 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
895 return 0;
896
Dan Williams87bf5722016-02-22 21:50:31 -0800897 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400898 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400899 if (atomic_read(&nvdimm->busy))
900 return -EBUSY;
901 return 0;
902}
903
Dan Williams62232e452015-06-08 14:27:06 -0400904static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
905 int read_only, unsigned int ioctl_cmd, unsigned long arg)
906{
907 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
908 size_t buf_len = 0, in_len = 0, out_len = 0;
909 static char out_env[ND_CMD_MAX_ENVELOPE];
910 static char in_env[ND_CMD_MAX_ENVELOPE];
911 const struct nd_cmd_desc *desc = NULL;
912 unsigned int cmd = _IOC_NR(ioctl_cmd);
Jerry Hoemann53b85a42017-06-30 20:41:22 -0700913 unsigned int func = cmd;
Dan Williams62232e452015-06-08 14:27:06 -0400914 void __user *p = (void __user *) arg;
915 struct device *dev = &nvdimm_bus->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700916 struct nd_cmd_pkg pkg;
Dan Williams62232e452015-06-08 14:27:06 -0400917 const char *cmd_name, *dimm_name;
Dan Williamse3654ec2016-04-28 16:17:07 -0700918 unsigned long cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400919 void *buf;
Dave Jiang006358b2017-04-07 15:33:31 -0700920 int rc, i, cmd_rc;
Dan Williams62232e452015-06-08 14:27:06 -0400921
922 if (nvdimm) {
923 desc = nd_cmd_dimm_desc(cmd);
924 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700925 cmd_mask = nvdimm->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400926 dimm_name = dev_name(&nvdimm->dev);
927 } else {
928 desc = nd_cmd_bus_desc(cmd);
929 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700930 cmd_mask = nd_desc->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400931 dimm_name = "bus";
932 }
933
Dan Williams31eca762016-04-28 16:23:43 -0700934 if (cmd == ND_CMD_CALL) {
935 if (copy_from_user(&pkg, p, sizeof(pkg)))
936 return -EFAULT;
937 }
938
Dan Williams62232e452015-06-08 14:27:06 -0400939 if (!desc || (desc->out_num + desc->in_num == 0) ||
Dan Williamse3654ec2016-04-28 16:17:07 -0700940 !test_bit(cmd, &cmd_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400941 return -ENOTTY;
942
943 /* fail write commands (when read-only) */
944 if (read_only)
Jerry Hoemann07accfa2016-01-06 16:03:41 -0700945 switch (cmd) {
946 case ND_CMD_VENDOR:
947 case ND_CMD_SET_CONFIG_DATA:
948 case ND_CMD_ARS_START:
Dan Williamsd4f32362016-03-03 16:08:54 -0800949 case ND_CMD_CLEAR_ERROR:
Dan Williams31eca762016-04-28 16:23:43 -0700950 case ND_CMD_CALL:
Dan Williams62232e452015-06-08 14:27:06 -0400951 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
952 nvdimm ? nvdimm_cmd_name(cmd)
953 : nvdimm_bus_cmd_name(cmd));
954 return -EPERM;
955 default:
956 break;
957 }
958
959 /* process an input envelope */
960 for (i = 0; i < desc->in_num; i++) {
961 u32 in_size, copy;
962
963 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
964 if (in_size == UINT_MAX) {
965 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
966 __func__, dimm_name, cmd_name, i);
967 return -ENXIO;
968 }
Dan Williams62232e452015-06-08 14:27:06 -0400969 if (in_len < sizeof(in_env))
970 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
971 else
972 copy = 0;
973 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
974 return -EFAULT;
975 in_len += in_size;
976 }
977
Dan Williams31eca762016-04-28 16:23:43 -0700978 if (cmd == ND_CMD_CALL) {
Jerry Hoemann53b85a42017-06-30 20:41:22 -0700979 func = pkg.nd_command;
Dan Williams31eca762016-04-28 16:23:43 -0700980 dev_dbg(dev, "%s:%s, idx: %llu, in: %zu, out: %zu, len %zu\n",
981 __func__, dimm_name, pkg.nd_command,
982 in_len, out_len, buf_len);
983
984 for (i = 0; i < ARRAY_SIZE(pkg.nd_reserved2); i++)
985 if (pkg.nd_reserved2[i])
986 return -EINVAL;
987 }
988
Dan Williams62232e452015-06-08 14:27:06 -0400989 /* process an output envelope */
990 for (i = 0; i < desc->out_num; i++) {
991 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800992 (u32 *) in_env, (u32 *) out_env, 0);
Dan Williams62232e452015-06-08 14:27:06 -0400993 u32 copy;
994
995 if (out_size == UINT_MAX) {
996 dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
997 __func__, dimm_name, cmd_name, i);
998 return -EFAULT;
999 }
Dan Williams62232e452015-06-08 14:27:06 -04001000 if (out_len < sizeof(out_env))
1001 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1002 else
1003 copy = 0;
1004 if (copy && copy_from_user(&out_env[out_len],
1005 p + in_len + out_len, copy))
1006 return -EFAULT;
1007 out_len += out_size;
1008 }
1009
1010 buf_len = out_len + in_len;
Dan Williams62232e452015-06-08 14:27:06 -04001011 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
1012 dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
1013 dimm_name, cmd_name, buf_len,
1014 ND_IOCTL_MAX_BUFLEN);
1015 return -EINVAL;
1016 }
1017
1018 buf = vmalloc(buf_len);
1019 if (!buf)
1020 return -ENOMEM;
1021
1022 if (copy_from_user(buf, p, buf_len)) {
1023 rc = -EFAULT;
1024 goto out;
1025 }
1026
Dan Williamseaf96152015-05-01 13:11:27 -04001027 nvdimm_bus_lock(&nvdimm_bus->dev);
Jerry Hoemann53b85a42017-06-30 20:41:22 -07001028 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
Dan Williamseaf96152015-05-01 13:11:27 -04001029 if (rc)
1030 goto out_unlock;
1031
Dave Jiang006358b2017-04-07 15:33:31 -07001032 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
Dan Williams62232e452015-06-08 14:27:06 -04001033 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -04001034 goto out_unlock;
Dave Jiang006358b2017-04-07 15:33:31 -07001035
1036 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1037 struct nd_cmd_clear_error *clear_err = buf;
Dave Jiang006358b2017-04-07 15:33:31 -07001038
Dan Williams23f49842017-04-29 15:24:03 -07001039 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1040 clear_err->cleared);
Dave Jiang006358b2017-04-07 15:33:31 -07001041 }
Dan Williams0beb2012017-04-07 09:47:24 -07001042 nvdimm_bus_unlock(&nvdimm_bus->dev);
1043
Dan Williams62232e452015-06-08 14:27:06 -04001044 if (copy_to_user(p, buf, buf_len))
1045 rc = -EFAULT;
Dan Williams0beb2012017-04-07 09:47:24 -07001046
1047 vfree(buf);
1048 return rc;
1049
Dan Williamseaf96152015-05-01 13:11:27 -04001050 out_unlock:
1051 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -04001052 out:
1053 vfree(buf);
1054 return rc;
1055}
1056
Dan Williams45def222015-04-26 19:26:48 -04001057static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1058{
Dan Williams62232e452015-06-08 14:27:06 -04001059 long id = (long) file->private_data;
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001060 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001061 struct nvdimm_bus *nvdimm_bus;
1062
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001063 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001064 mutex_lock(&nvdimm_bus_list_mutex);
1065 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1066 if (nvdimm_bus->id == id) {
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001067 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001068 break;
1069 }
1070 }
1071 mutex_unlock(&nvdimm_bus_list_mutex);
1072
1073 return rc;
1074}
1075
1076static int match_dimm(struct device *dev, void *data)
1077{
1078 long id = (long) data;
1079
1080 if (is_nvdimm(dev)) {
1081 struct nvdimm *nvdimm = to_nvdimm(dev);
1082
1083 return nvdimm->id == id;
1084 }
1085
1086 return 0;
1087}
1088
1089static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1090{
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001091 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001092 struct nvdimm_bus *nvdimm_bus;
1093
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001094 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001095 mutex_lock(&nvdimm_bus_list_mutex);
1096 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1097 struct device *dev = device_find_child(&nvdimm_bus->dev,
1098 file->private_data, match_dimm);
1099 struct nvdimm *nvdimm;
1100
1101 if (!dev)
1102 continue;
1103
1104 nvdimm = to_nvdimm(dev);
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001105 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001106 put_device(dev);
1107 break;
1108 }
1109 mutex_unlock(&nvdimm_bus_list_mutex);
1110
1111 return rc;
1112}
1113
1114static int nd_open(struct inode *inode, struct file *file)
1115{
1116 long minor = iminor(inode);
1117
1118 file->private_data = (void *) minor;
1119 return 0;
Dan Williams45def222015-04-26 19:26:48 -04001120}
1121
1122static const struct file_operations nvdimm_bus_fops = {
1123 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -04001124 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -04001125 .unlocked_ioctl = nd_ioctl,
1126 .compat_ioctl = nd_ioctl,
1127 .llseek = noop_llseek,
1128};
1129
Dan Williams62232e452015-06-08 14:27:06 -04001130static const struct file_operations nvdimm_fops = {
1131 .owner = THIS_MODULE,
1132 .open = nd_open,
1133 .unlocked_ioctl = nvdimm_ioctl,
1134 .compat_ioctl = nvdimm_ioctl,
1135 .llseek = noop_llseek,
1136};
1137
Dan Williams45def222015-04-26 19:26:48 -04001138int __init nvdimm_bus_init(void)
1139{
1140 int rc;
1141
Dan Williamsbaa51272016-04-05 17:40:52 -07001142 BUILD_BUG_ON(sizeof(struct nd_smart_payload) != 128);
1143 BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload) != 8);
1144
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001145 rc = bus_register(&nvdimm_bus_type);
1146 if (rc)
1147 return rc;
1148
Dan Williams45def222015-04-26 19:26:48 -04001149 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1150 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -04001151 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -04001152 nvdimm_bus_major = rc;
1153
Dan Williams62232e452015-06-08 14:27:06 -04001154 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1155 if (rc < 0)
1156 goto err_dimm_chrdev;
1157 nvdimm_major = rc;
1158
Dan Williams45def222015-04-26 19:26:48 -04001159 nd_class = class_create(THIS_MODULE, "nd");
Axel Lindaa1dee2015-06-28 17:00:57 +08001160 if (IS_ERR(nd_class)) {
1161 rc = PTR_ERR(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001162 goto err_class;
Axel Lindaa1dee2015-06-28 17:00:57 +08001163 }
Dan Williams45def222015-04-26 19:26:48 -04001164
Dan Williams18515942016-07-22 23:46:08 -07001165 rc = driver_register(&nd_bus_driver.drv);
1166 if (rc)
1167 goto err_nd_bus;
1168
Dan Williams45def222015-04-26 19:26:48 -04001169 return 0;
1170
Dan Williams18515942016-07-22 23:46:08 -07001171 err_nd_bus:
1172 class_destroy(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001173 err_class:
Dan Williams62232e452015-06-08 14:27:06 -04001174 unregister_chrdev(nvdimm_major, "dimmctl");
1175 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -04001176 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001177 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001178 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -04001179
1180 return rc;
1181}
1182
Dan Williams4d88a972015-05-31 14:41:48 -04001183void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -04001184{
Dan Williams18515942016-07-22 23:46:08 -07001185 driver_unregister(&nd_bus_driver.drv);
Dan Williams45def222015-04-26 19:26:48 -04001186 class_destroy(nd_class);
1187 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001188 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001189 bus_unregister(&nvdimm_bus_type);
Dan Williams18515942016-07-22 23:46:08 -07001190 ida_destroy(&nd_ida);
Dan Williams45def222015-04-26 19:26:48 -04001191}