blob: a64023690cadeec34e251c38efeb4b036f0a91ac [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
Dave Jiangaa9ad442017-08-23 12:48:26 -070014#include <linux/libnvdimm.h>
Vishal Verma0930a752017-08-30 19:36:02 -060015#include <linux/sched/mm.h>
Dan Williams62232e452015-06-08 14:27:06 -040016#include <linux/vmalloc.h>
Dan Williams45def222015-04-26 19:26:48 -040017#include <linux/uaccess.h>
Dan Williams3d880022015-05-31 15:02:11 -040018#include <linux/module.h>
Dan Williams8c2f7e82015-06-25 04:20:04 -040019#include <linux/blkdev.h>
Dan Williams45def222015-04-26 19:26:48 -040020#include <linux/fcntl.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -040021#include <linux/async.h>
Dan Williams8c2f7e82015-06-25 04:20:04 -040022#include <linux/genhd.h>
Dan Williams62232e452015-06-08 14:27:06 -040023#include <linux/ndctl.h>
Dan Williams4d88a972015-05-31 14:41:48 -040024#include <linux/sched.h>
Dan Williams45def222015-04-26 19:26:48 -040025#include <linux/slab.h>
26#include <linux/fs.h>
27#include <linux/io.h>
Dan Williams62232e452015-06-08 14:27:06 -040028#include <linux/mm.h>
Dan Williams4d88a972015-05-31 14:41:48 -040029#include <linux/nd.h>
Dan Williams45def222015-04-26 19:26:48 -040030#include "nd-core.h"
Dan Williams4d88a972015-05-31 14:41:48 -040031#include "nd.h"
Dave Jiang006358b2017-04-07 15:33:31 -070032#include "pfn.h"
Dan Williams45def222015-04-26 19:26:48 -040033
Dan Williams62232e452015-06-08 14:27:06 -040034int nvdimm_major;
Dan Williams45def222015-04-26 19:26:48 -040035static int nvdimm_bus_major;
36static struct class *nd_class;
Dan Williams18515942016-07-22 23:46:08 -070037static DEFINE_IDA(nd_ida);
Dan Williams45def222015-04-26 19:26:48 -040038
Dan Williams4d88a972015-05-31 14:41:48 -040039static int to_nd_device_type(struct device *dev)
40{
41 if (is_nvdimm(dev))
42 return ND_DEVICE_DIMM;
Dan Williamsc9e582a2017-05-29 23:12:19 -070043 else if (is_memory(dev))
Dan Williams3d880022015-05-31 15:02:11 -040044 return ND_DEVICE_REGION_PMEM;
45 else if (is_nd_blk(dev))
46 return ND_DEVICE_REGION_BLK;
Dan Williamscd034122016-03-11 10:15:36 -080047 else if (is_nd_dax(dev))
48 return ND_DEVICE_DAX_PMEM;
Dan Williamsc9e582a2017-05-29 23:12:19 -070049 else if (is_nd_region(dev->parent))
Dan Williams3d880022015-05-31 15:02:11 -040050 return nd_region_to_nstype(to_nd_region(dev->parent));
Dan Williams4d88a972015-05-31 14:41:48 -040051
52 return 0;
53}
54
55static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
56{
Toshi Kani41d7a6d2015-06-19 12:18:33 -060057 /*
58 * Ensure that region devices always have their numa node set as
59 * early as possible.
60 */
Dan Williamsc9e582a2017-05-29 23:12:19 -070061 if (is_nd_region(dev))
Toshi Kani41d7a6d2015-06-19 12:18:33 -060062 set_dev_node(dev, to_nd_region(dev)->numa_node);
Dan Williams4d88a972015-05-31 14:41:48 -040063 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
64 to_nd_device_type(dev));
65}
66
Dan Williams3d880022015-05-31 15:02:11 -040067static struct module *to_bus_provider(struct device *dev)
68{
69 /* pin bus providers while regions are enabled */
Dan Williamsc9e582a2017-05-29 23:12:19 -070070 if (is_nd_region(dev)) {
Dan Williams3d880022015-05-31 15:02:11 -040071 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
72
Dan Williamsbc9775d2016-07-21 20:03:19 -070073 return nvdimm_bus->nd_desc->module;
Dan Williams3d880022015-05-31 15:02:11 -040074 }
75 return NULL;
76}
77
Dan Williamseaf96152015-05-01 13:11:27 -040078static 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
Dan Williams4d88a972015-05-31 14:41:48 -040093static int nvdimm_bus_probe(struct device *dev)
94{
95 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -040096 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040097 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
98 int rc;
99
Dan Williams3d880022015-05-31 15:02:11 -0400100 if (!try_module_get(provider))
101 return -ENXIO;
102
Dan Williamseaf96152015-05-01 13:11:27 -0400103 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -0400104 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400105 if (rc == 0)
106 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400107 else
108 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400109 nvdimm_bus_probe_end(nvdimm_bus);
110
Dan Williams4d88a972015-05-31 14:41:48 -0400111 dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name,
112 dev_name(dev), rc);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400113
Dan Williams3d880022015-05-31 15:02:11 -0400114 if (rc != 0)
115 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400116 return rc;
117}
118
119static int nvdimm_bus_remove(struct device *dev)
120{
121 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -0400122 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400123 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700124 int rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -0400125
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700126 if (nd_drv->remove)
127 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400128 nd_region_disable(nvdimm_bus, dev);
129
Dan Williams4d88a972015-05-31 14:41:48 -0400130 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
131 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400132 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400133 return rc;
134}
135
Dan Williams476f8482016-07-09 00:12:52 -0700136static void nvdimm_bus_shutdown(struct device *dev)
137{
138 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
139 struct nd_device_driver *nd_drv = NULL;
140
141 if (dev->driver)
142 nd_drv = to_nd_device_driver(dev->driver);
143
144 if (nd_drv && nd_drv->shutdown) {
145 nd_drv->shutdown(dev);
146 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
147 dev->driver->name, dev_name(dev));
148 }
149}
150
Dan Williams71999462016-02-18 10:29:49 -0800151void nd_device_notify(struct device *dev, enum nvdimm_event event)
152{
153 device_lock(dev);
154 if (dev->driver) {
155 struct nd_device_driver *nd_drv;
156
157 nd_drv = to_nd_device_driver(dev->driver);
158 if (nd_drv->notify)
159 nd_drv->notify(dev, event);
160 }
161 device_unlock(dev);
162}
163EXPORT_SYMBOL(nd_device_notify);
164
165void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
166{
167 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
168
169 if (!nvdimm_bus)
170 return;
171
172 /* caller is responsible for holding a reference on the device */
173 nd_device_notify(&nd_region->dev, event);
174}
175EXPORT_SYMBOL_GPL(nvdimm_region_notify);
176
Dan Williams23f49842017-04-29 15:24:03 -0700177struct clear_badblocks_context {
178 resource_size_t phys, cleared;
179};
180
181static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
182{
183 struct clear_badblocks_context *ctx = data;
184 struct nd_region *nd_region;
185 resource_size_t ndr_end;
186 sector_t sector;
187
188 /* make sure device is a region */
189 if (!is_nd_pmem(dev))
190 return 0;
191
192 nd_region = to_nd_region(dev);
193 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
194
195 /* make sure we are in the region */
196 if (ctx->phys < nd_region->ndr_start
197 || (ctx->phys + ctx->cleared) > ndr_end)
198 return 0;
199
200 sector = (ctx->phys - nd_region->ndr_start) / 512;
201 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
202
Toshi Kani975750a2017-06-12 16:25:11 -0600203 if (nd_region->bb_state)
204 sysfs_notify_dirent(nd_region->bb_state);
205
Dan Williams23f49842017-04-29 15:24:03 -0700206 return 0;
207}
208
209static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
210 phys_addr_t phys, u64 cleared)
211{
212 struct clear_badblocks_context ctx = {
213 .phys = phys,
214 .cleared = cleared,
215 };
216
217 device_for_each_child(&nvdimm_bus->dev, &ctx,
218 nvdimm_clear_badblocks_region);
219}
220
221static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
222 phys_addr_t phys, u64 cleared)
223{
224 if (cleared > 0)
Dave Jiangaa9ad442017-08-23 12:48:26 -0700225 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
Dan Williams23f49842017-04-29 15:24:03 -0700226
227 if (cleared > 0 && cleared / 512)
228 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
229}
230
Dan Williams59e64732016-03-08 07:16:07 -0800231long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
232 unsigned int len)
233{
234 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
235 struct nvdimm_bus_descriptor *nd_desc;
236 struct nd_cmd_clear_error clear_err;
237 struct nd_cmd_ars_cap ars_cap;
238 u32 clear_err_unit, mask;
Vishal Verma0930a752017-08-30 19:36:02 -0600239 unsigned int noio_flag;
Dan Williams59e64732016-03-08 07:16:07 -0800240 int cmd_rc, rc;
241
242 if (!nvdimm_bus)
243 return -ENXIO;
244
245 nd_desc = nvdimm_bus->nd_desc;
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700246 /*
247 * if ndctl does not exist, it's PMEM_LEGACY and
248 * we want to just pretend everything is handled.
249 */
Dan Williams59e64732016-03-08 07:16:07 -0800250 if (!nd_desc->ndctl)
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700251 return len;
Dan Williams59e64732016-03-08 07:16:07 -0800252
253 memset(&ars_cap, 0, sizeof(ars_cap));
254 ars_cap.address = phys;
255 ars_cap.length = len;
Vishal Verma0930a752017-08-30 19:36:02 -0600256 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800257 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
258 sizeof(ars_cap), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600259 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800260 if (rc < 0)
261 return rc;
262 if (cmd_rc < 0)
263 return cmd_rc;
264 clear_err_unit = ars_cap.clear_err_unit;
265 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
266 return -ENXIO;
267
268 mask = clear_err_unit - 1;
269 if ((phys | len) & mask)
270 return -ENXIO;
271 memset(&clear_err, 0, sizeof(clear_err));
272 clear_err.address = phys;
273 clear_err.length = len;
Vishal Verma0930a752017-08-30 19:36:02 -0600274 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800275 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
276 sizeof(clear_err), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600277 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800278 if (rc < 0)
279 return rc;
280 if (cmd_rc < 0)
281 return cmd_rc;
Vishal Vermae0461142016-09-30 17:19:31 -0600282
Dan Williams23f49842017-04-29 15:24:03 -0700283 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
Toshi Kani8d13c022017-04-27 16:57:05 -0600284
Dan Williams59e64732016-03-08 07:16:07 -0800285 return clear_err.cleared;
286}
287EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
288
Dan Williams18515942016-07-22 23:46:08 -0700289static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
290
Dan Williams4d88a972015-05-31 14:41:48 -0400291static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400292 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400293 .uevent = nvdimm_bus_uevent,
294 .match = nvdimm_bus_match,
295 .probe = nvdimm_bus_probe,
296 .remove = nvdimm_bus_remove,
Dan Williams476f8482016-07-09 00:12:52 -0700297 .shutdown = nvdimm_bus_shutdown,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400298};
299
Dan Williams18515942016-07-22 23:46:08 -0700300static void nvdimm_bus_release(struct device *dev)
301{
302 struct nvdimm_bus *nvdimm_bus;
303
304 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
305 ida_simple_remove(&nd_ida, nvdimm_bus->id);
306 kfree(nvdimm_bus);
307}
308
309static bool is_nvdimm_bus(struct device *dev)
310{
311 return dev->release == nvdimm_bus_release;
312}
313
314struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
315{
316 struct device *dev;
317
318 for (dev = nd_dev; dev; dev = dev->parent)
319 if (is_nvdimm_bus(dev))
320 break;
321 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
322 if (dev)
323 return to_nvdimm_bus(dev);
324 return NULL;
325}
326
327struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
328{
329 struct nvdimm_bus *nvdimm_bus;
330
331 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
332 WARN_ON(!is_nvdimm_bus(dev));
333 return nvdimm_bus;
334}
335EXPORT_SYMBOL_GPL(to_nvdimm_bus);
336
337struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
338 struct nvdimm_bus_descriptor *nd_desc)
339{
340 struct nvdimm_bus *nvdimm_bus;
341 int rc;
342
343 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
344 if (!nvdimm_bus)
345 return NULL;
346 INIT_LIST_HEAD(&nvdimm_bus->list);
347 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
Dan Williams18515942016-07-22 23:46:08 -0700348 init_waitqueue_head(&nvdimm_bus->probe_wait);
349 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
350 mutex_init(&nvdimm_bus->reconfig_mutex);
Dave Jiangaa9ad442017-08-23 12:48:26 -0700351 badrange_init(&nvdimm_bus->badrange);
Dan Williams18515942016-07-22 23:46:08 -0700352 if (nvdimm_bus->id < 0) {
353 kfree(nvdimm_bus);
354 return NULL;
355 }
356 nvdimm_bus->nd_desc = nd_desc;
357 nvdimm_bus->dev.parent = parent;
358 nvdimm_bus->dev.release = nvdimm_bus_release;
359 nvdimm_bus->dev.groups = nd_desc->attr_groups;
360 nvdimm_bus->dev.bus = &nvdimm_bus_type;
Oliver O'Halloran1ff19f42018-04-06 15:21:13 +1000361 nvdimm_bus->dev.of_node = nd_desc->of_node;
Dan Williams18515942016-07-22 23:46:08 -0700362 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
363 rc = device_register(&nvdimm_bus->dev);
364 if (rc) {
365 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
366 goto err;
367 }
368
369 return nvdimm_bus;
370 err:
371 put_device(&nvdimm_bus->dev);
372 return NULL;
373}
374EXPORT_SYMBOL_GPL(nvdimm_bus_register);
375
376void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
377{
378 if (!nvdimm_bus)
379 return;
380 device_unregister(&nvdimm_bus->dev);
381}
382EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
383
384static int child_unregister(struct device *dev, void *data)
385{
386 /*
387 * the singular ndctl class device per bus needs to be
388 * "device_destroy"ed, so skip it here
389 *
390 * i.e. remove classless children
391 */
392 if (dev->class)
393 /* pass */;
394 else
395 nd_device_unregister(dev, ND_SYNC);
396 return 0;
397}
398
Dave Jiangaa9ad442017-08-23 12:48:26 -0700399static void free_badrange_list(struct list_head *badrange_list)
Dan Williams18515942016-07-22 23:46:08 -0700400{
Dave Jiangaa9ad442017-08-23 12:48:26 -0700401 struct badrange_entry *bre, *next;
Dan Williams18515942016-07-22 23:46:08 -0700402
Dave Jiangaa9ad442017-08-23 12:48:26 -0700403 list_for_each_entry_safe(bre, next, badrange_list, list) {
404 list_del(&bre->list);
405 kfree(bre);
Dan Williams18515942016-07-22 23:46:08 -0700406 }
Dave Jiangaa9ad442017-08-23 12:48:26 -0700407 list_del_init(badrange_list);
Dan Williams18515942016-07-22 23:46:08 -0700408}
409
410static int nd_bus_remove(struct device *dev)
411{
412 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
413
414 mutex_lock(&nvdimm_bus_list_mutex);
415 list_del_init(&nvdimm_bus->list);
416 mutex_unlock(&nvdimm_bus_list_mutex);
417
418 nd_synchronize();
419 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
420
Dave Jiangaa9ad442017-08-23 12:48:26 -0700421 spin_lock(&nvdimm_bus->badrange.lock);
422 free_badrange_list(&nvdimm_bus->badrange.list);
423 spin_unlock(&nvdimm_bus->badrange.lock);
Dan Williams18515942016-07-22 23:46:08 -0700424
425 nvdimm_bus_destroy_ndctl(nvdimm_bus);
426
427 return 0;
428}
429
430static int nd_bus_probe(struct device *dev)
431{
432 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
433 int rc;
434
435 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
436 if (rc)
437 return rc;
438
439 mutex_lock(&nvdimm_bus_list_mutex);
440 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
441 mutex_unlock(&nvdimm_bus_list_mutex);
442
443 /* enable bus provider attributes to look up their local context */
444 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
445
446 return 0;
447}
448
449static struct nd_device_driver nd_bus_driver = {
450 .probe = nd_bus_probe,
451 .remove = nd_bus_remove,
452 .drv = {
453 .name = "nd_bus",
454 .suppress_bind_attrs = true,
455 .bus = &nvdimm_bus_type,
456 .owner = THIS_MODULE,
457 .mod_name = KBUILD_MODNAME,
458 },
459};
460
461static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
462{
463 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
464
465 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
466 return true;
467
468 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
469}
470
Dan Williams4d88a972015-05-31 14:41:48 -0400471static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
472
473void nd_synchronize(void)
474{
475 async_synchronize_full_domain(&nd_async_domain);
476}
477EXPORT_SYMBOL_GPL(nd_synchronize);
478
479static void nd_async_device_register(void *d, async_cookie_t cookie)
480{
481 struct device *dev = d;
482
483 if (device_add(dev) != 0) {
484 dev_err(dev, "%s: failed\n", __func__);
485 put_device(dev);
486 }
487 put_device(dev);
488}
489
490static void nd_async_device_unregister(void *d, async_cookie_t cookie)
491{
492 struct device *dev = d;
493
Dan Williams0ba1c632015-05-30 12:35:36 -0400494 /* flush bus operations before delete */
495 nvdimm_bus_lock(dev);
496 nvdimm_bus_unlock(dev);
497
Dan Williams4d88a972015-05-31 14:41:48 -0400498 device_unregister(dev);
499 put_device(dev);
500}
501
Dan Williams8c2f7e82015-06-25 04:20:04 -0400502void __nd_device_register(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400503{
Dan Williamscd034122016-03-11 10:15:36 -0800504 if (!dev)
505 return;
Dan Williams4d88a972015-05-31 14:41:48 -0400506 dev->bus = &nvdimm_bus_type;
Dan Williams4d88a972015-05-31 14:41:48 -0400507 get_device(dev);
508 async_schedule_domain(nd_async_device_register, dev,
509 &nd_async_domain);
510}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400511
512void nd_device_register(struct device *dev)
513{
514 device_initialize(dev);
515 __nd_device_register(dev);
516}
Dan Williams4d88a972015-05-31 14:41:48 -0400517EXPORT_SYMBOL(nd_device_register);
518
519void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
520{
521 switch (mode) {
522 case ND_ASYNC:
523 get_device(dev);
524 async_schedule_domain(nd_async_device_unregister, dev,
525 &nd_async_domain);
526 break;
527 case ND_SYNC:
528 nd_synchronize();
529 device_unregister(dev);
530 break;
531 }
532}
533EXPORT_SYMBOL(nd_device_unregister);
534
535/**
536 * __nd_driver_register() - register a region or a namespace driver
537 * @nd_drv: driver to register
538 * @owner: automatically set by nd_driver_register() macro
539 * @mod_name: automatically set by nd_driver_register() macro
540 */
541int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
542 const char *mod_name)
543{
544 struct device_driver *drv = &nd_drv->drv;
545
546 if (!nd_drv->type) {
547 pr_debug("driver type bitmask not set (%pf)\n",
548 __builtin_return_address(0));
549 return -EINVAL;
550 }
551
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700552 if (!nd_drv->probe) {
553 pr_debug("%s ->probe() must be specified\n", mod_name);
Dan Williams4d88a972015-05-31 14:41:48 -0400554 return -EINVAL;
555 }
556
557 drv->bus = &nvdimm_bus_type;
558 drv->owner = owner;
559 drv->mod_name = mod_name;
560
561 return driver_register(drv);
562}
563EXPORT_SYMBOL(__nd_driver_register);
564
Dan Williams58138822015-06-23 20:08:34 -0400565int nvdimm_revalidate_disk(struct gendisk *disk)
566{
Dan Williams52c44d92016-06-15 19:43:07 -0700567 struct device *dev = disk_to_dev(disk)->parent;
Dan Williams58138822015-06-23 20:08:34 -0400568 struct nd_region *nd_region = to_nd_region(dev->parent);
569 const char *pol = nd_region->ro ? "only" : "write";
570
571 if (nd_region->ro == get_disk_ro(disk))
572 return 0;
573
574 dev_info(dev, "%s read-%s, marking %s read-%s\n",
575 dev_name(&nd_region->dev), pol, disk->disk_name, pol);
576 set_disk_ro(disk, nd_region->ro);
577
578 return 0;
579
580}
581EXPORT_SYMBOL(nvdimm_revalidate_disk);
582
Dan Williams4d88a972015-05-31 14:41:48 -0400583static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
584 char *buf)
585{
586 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
587 to_nd_device_type(dev));
588}
589static DEVICE_ATTR_RO(modalias);
590
591static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
592 char *buf)
593{
594 return sprintf(buf, "%s\n", dev->type->name);
595}
596static DEVICE_ATTR_RO(devtype);
597
598static struct attribute *nd_device_attributes[] = {
599 &dev_attr_modalias.attr,
600 &dev_attr_devtype.attr,
601 NULL,
602};
603
604/**
605 * nd_device_attribute_group - generic attributes for all devices on an nd bus
606 */
607struct attribute_group nd_device_attribute_group = {
608 .attrs = nd_device_attributes,
609};
610EXPORT_SYMBOL_GPL(nd_device_attribute_group);
611
Toshi Kani74ae66c2015-06-19 12:18:34 -0600612static ssize_t numa_node_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 return sprintf(buf, "%d\n", dev_to_node(dev));
616}
617static DEVICE_ATTR_RO(numa_node);
618
619static struct attribute *nd_numa_attributes[] = {
620 &dev_attr_numa_node.attr,
621 NULL,
622};
623
624static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
625 int n)
626{
627 if (!IS_ENABLED(CONFIG_NUMA))
628 return 0;
629
630 return a->mode;
631}
632
633/**
634 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
635 */
636struct attribute_group nd_numa_attribute_group = {
637 .attrs = nd_numa_attributes,
638 .is_visible = nd_numa_attr_visible,
639};
640EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
641
Dan Williams45def222015-04-26 19:26:48 -0400642int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
643{
644 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
645 struct device *dev;
646
647 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
648 "ndctl%d", nvdimm_bus->id);
649
Dan Williams42588952016-05-27 13:28:31 -0700650 if (IS_ERR(dev))
Dan Williams45def222015-04-26 19:26:48 -0400651 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
652 nvdimm_bus->id, PTR_ERR(dev));
Dan Williams42588952016-05-27 13:28:31 -0700653 return PTR_ERR_OR_ZERO(dev);
Dan Williams45def222015-04-26 19:26:48 -0400654}
655
656void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
657{
658 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
659}
660
Dan Williams62232e452015-06-08 14:27:06 -0400661static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
662 [ND_CMD_IMPLEMENTED] = { },
663 [ND_CMD_SMART] = {
664 .out_num = 2,
Dan Williams21129112016-04-07 19:58:44 -0700665 .out_sizes = { 4, 128, },
Dan Williams62232e452015-06-08 14:27:06 -0400666 },
667 [ND_CMD_SMART_THRESHOLD] = {
668 .out_num = 2,
669 .out_sizes = { 4, 8, },
670 },
671 [ND_CMD_DIMM_FLAGS] = {
672 .out_num = 2,
673 .out_sizes = { 4, 4 },
674 },
675 [ND_CMD_GET_CONFIG_SIZE] = {
676 .out_num = 3,
677 .out_sizes = { 4, 4, 4, },
678 },
679 [ND_CMD_GET_CONFIG_DATA] = {
680 .in_num = 2,
681 .in_sizes = { 4, 4, },
682 .out_num = 2,
683 .out_sizes = { 4, UINT_MAX, },
684 },
685 [ND_CMD_SET_CONFIG_DATA] = {
686 .in_num = 3,
687 .in_sizes = { 4, 4, UINT_MAX, },
688 .out_num = 1,
689 .out_sizes = { 4, },
690 },
691 [ND_CMD_VENDOR] = {
692 .in_num = 3,
693 .in_sizes = { 4, 4, UINT_MAX, },
694 .out_num = 3,
695 .out_sizes = { 4, 4, UINT_MAX, },
696 },
Dan Williams31eca762016-04-28 16:23:43 -0700697 [ND_CMD_CALL] = {
698 .in_num = 2,
699 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
700 .out_num = 1,
701 .out_sizes = { UINT_MAX, },
702 },
Dan Williams62232e452015-06-08 14:27:06 -0400703};
704
705const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
706{
707 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
708 return &__nd_cmd_dimm_descs[cmd];
709 return NULL;
710}
711EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
712
713static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
714 [ND_CMD_IMPLEMENTED] = { },
715 [ND_CMD_ARS_CAP] = {
716 .in_num = 2,
717 .in_sizes = { 8, 8, },
Dan Williams4577b062016-02-17 13:08:58 -0800718 .out_num = 4,
719 .out_sizes = { 4, 4, 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400720 },
721 [ND_CMD_ARS_START] = {
Dan Williams4577b062016-02-17 13:08:58 -0800722 .in_num = 5,
723 .in_sizes = { 8, 8, 2, 1, 5, },
724 .out_num = 2,
725 .out_sizes = { 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400726 },
727 [ND_CMD_ARS_STATUS] = {
Dan Williams747ffe12016-02-19 15:21:14 -0800728 .out_num = 3,
729 .out_sizes = { 4, 4, UINT_MAX, },
Dan Williams62232e452015-06-08 14:27:06 -0400730 },
Dan Williamsd4f32362016-03-03 16:08:54 -0800731 [ND_CMD_CLEAR_ERROR] = {
732 .in_num = 2,
733 .in_sizes = { 8, 8, },
734 .out_num = 3,
735 .out_sizes = { 4, 4, 8, },
736 },
Dan Williams31eca762016-04-28 16:23:43 -0700737 [ND_CMD_CALL] = {
738 .in_num = 2,
739 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
740 .out_num = 1,
741 .out_sizes = { UINT_MAX, },
742 },
Dan Williams62232e452015-06-08 14:27:06 -0400743};
744
745const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
746{
747 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
748 return &__nd_cmd_bus_descs[cmd];
749 return NULL;
750}
751EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
752
753u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
754 const struct nd_cmd_desc *desc, int idx, void *buf)
755{
756 if (idx >= desc->in_num)
757 return UINT_MAX;
758
759 if (desc->in_sizes[idx] < UINT_MAX)
760 return desc->in_sizes[idx];
761
762 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
763 struct nd_cmd_set_config_hdr *hdr = buf;
764
765 return hdr->in_length;
766 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
767 struct nd_cmd_vendor_hdr *hdr = buf;
768
769 return hdr->in_length;
Dan Williams31eca762016-04-28 16:23:43 -0700770 } else if (cmd == ND_CMD_CALL) {
771 struct nd_cmd_pkg *pkg = buf;
772
773 return pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400774 }
775
776 return UINT_MAX;
777}
778EXPORT_SYMBOL_GPL(nd_cmd_in_size);
779
780u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
781 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800782 const u32 *out_field, unsigned long remainder)
Dan Williams62232e452015-06-08 14:27:06 -0400783{
784 if (idx >= desc->out_num)
785 return UINT_MAX;
786
787 if (desc->out_sizes[idx] < UINT_MAX)
788 return desc->out_sizes[idx];
789
790 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
791 return in_field[1];
792 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
793 return out_field[1];
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800794 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
795 /*
796 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
797 * "Size of Output Buffer in bytes, including this
798 * field."
799 */
800 if (out_field[1] < 4)
801 return 0;
802 /*
803 * ACPI 6.1 is ambiguous if 'status' is included in the
804 * output size. If we encounter an output size that
805 * overshoots the remainder by 4 bytes, assume it was
806 * including 'status'.
807 */
808 if (out_field[1] - 8 == remainder)
809 return remainder;
810 return out_field[1] - 4;
811 } else if (cmd == ND_CMD_CALL) {
Dan Williams31eca762016-04-28 16:23:43 -0700812 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
813
814 return pkg->nd_size_out;
815 }
816
Dan Williams62232e452015-06-08 14:27:06 -0400817
818 return UINT_MAX;
819}
820EXPORT_SYMBOL_GPL(nd_cmd_out_size);
821
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400822void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400823{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400824 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
825
Dan Williamseaf96152015-05-01 13:11:27 -0400826 do {
827 if (nvdimm_bus->probe_active == 0)
828 break;
829 nvdimm_bus_unlock(&nvdimm_bus->dev);
830 wait_event(nvdimm_bus->probe_wait,
831 nvdimm_bus->probe_active == 0);
832 nvdimm_bus_lock(&nvdimm_bus->dev);
833 } while (true);
834}
835
Dave Jiang006358b2017-04-07 15:33:31 -0700836static int nd_pmem_forget_poison_check(struct device *dev, void *data)
Dan Williamsd4f32362016-03-03 16:08:54 -0800837{
Dave Jiang006358b2017-04-07 15:33:31 -0700838 struct nd_cmd_clear_error *clear_err =
839 (struct nd_cmd_clear_error *)data;
840 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
841 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
842 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
843 struct nd_namespace_common *ndns = NULL;
844 struct nd_namespace_io *nsio;
845 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
846
847 if (nd_dax || !dev->driver)
848 return 0;
849
850 start = clear_err->address;
851 end = clear_err->address + clear_err->cleared - 1;
852
853 if (nd_btt || nd_pfn || nd_dax) {
854 if (nd_btt)
855 ndns = nd_btt->ndns;
856 else if (nd_pfn)
857 ndns = nd_pfn->ndns;
858 else if (nd_dax)
859 ndns = nd_dax->nd_pfn.ndns;
860
861 if (!ndns)
862 return 0;
863 } else
864 ndns = to_ndns(dev);
865
866 nsio = to_nd_namespace_io(&ndns->dev);
867 pstart = nsio->res.start + offset;
868 pend = nsio->res.end - end_trunc;
869
870 if ((pstart >= start) && (pend <= end))
Dan Williamsd4f32362016-03-03 16:08:54 -0800871 return -EBUSY;
Dave Jiang006358b2017-04-07 15:33:31 -0700872
Dan Williamsd4f32362016-03-03 16:08:54 -0800873 return 0;
Dave Jiang006358b2017-04-07 15:33:31 -0700874
875}
876
877static int nd_ns_forget_poison_check(struct device *dev, void *data)
878{
879 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800880}
881
Dan Williamseaf96152015-05-01 13:11:27 -0400882/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800883static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
Dave Jiang006358b2017-04-07 15:33:31 -0700884 struct nvdimm *nvdimm, unsigned int cmd, void *data)
Dan Williamseaf96152015-05-01 13:11:27 -0400885{
Dan Williams87bf5722016-02-22 21:50:31 -0800886 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
887
888 /* ask the bus provider if it would like to block this request */
889 if (nd_desc->clear_to_send) {
890 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
891
892 if (rc)
893 return rc;
894 }
Dan Williamseaf96152015-05-01 13:11:27 -0400895
Dan Williamsd4f32362016-03-03 16:08:54 -0800896 /* require clear error to go through the pmem driver */
897 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
Dave Jiang006358b2017-04-07 15:33:31 -0700898 return device_for_each_child(&nvdimm_bus->dev, data,
899 nd_ns_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800900
Dan Williamseaf96152015-05-01 13:11:27 -0400901 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
902 return 0;
903
Dan Williams87bf5722016-02-22 21:50:31 -0800904 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400905 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400906 if (atomic_read(&nvdimm->busy))
907 return -EBUSY;
908 return 0;
909}
910
Dan Williams62232e452015-06-08 14:27:06 -0400911static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
912 int read_only, unsigned int ioctl_cmd, unsigned long arg)
913{
914 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
Dan Williams62232e452015-06-08 14:27:06 -0400915 static char out_env[ND_CMD_MAX_ENVELOPE];
916 static char in_env[ND_CMD_MAX_ENVELOPE];
917 const struct nd_cmd_desc *desc = NULL;
918 unsigned int cmd = _IOC_NR(ioctl_cmd);
Dan Williams62232e452015-06-08 14:27:06 -0400919 struct device *dev = &nvdimm_bus->dev;
Dan Williams58738c42017-08-31 15:41:55 -0700920 void __user *p = (void __user *) arg;
Dan Williams62232e452015-06-08 14:27:06 -0400921 const char *cmd_name, *dimm_name;
Dan Williams58738c42017-08-31 15:41:55 -0700922 u32 in_len = 0, out_len = 0;
923 unsigned int func = cmd;
Dan Williamse3654ec2016-04-28 16:17:07 -0700924 unsigned long cmd_mask;
Dan Williams58738c42017-08-31 15:41:55 -0700925 struct nd_cmd_pkg pkg;
Dave Jiang006358b2017-04-07 15:33:31 -0700926 int rc, i, cmd_rc;
Dan Williams58738c42017-08-31 15:41:55 -0700927 u64 buf_len = 0;
928 void *buf;
Dan Williams62232e452015-06-08 14:27:06 -0400929
930 if (nvdimm) {
931 desc = nd_cmd_dimm_desc(cmd);
932 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700933 cmd_mask = nvdimm->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400934 dimm_name = dev_name(&nvdimm->dev);
935 } else {
936 desc = nd_cmd_bus_desc(cmd);
937 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700938 cmd_mask = nd_desc->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400939 dimm_name = "bus";
940 }
941
Dan Williams31eca762016-04-28 16:23:43 -0700942 if (cmd == ND_CMD_CALL) {
943 if (copy_from_user(&pkg, p, sizeof(pkg)))
944 return -EFAULT;
945 }
946
Dan Williams62232e452015-06-08 14:27:06 -0400947 if (!desc || (desc->out_num + desc->in_num == 0) ||
Dan Williamse3654ec2016-04-28 16:17:07 -0700948 !test_bit(cmd, &cmd_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400949 return -ENOTTY;
950
951 /* fail write commands (when read-only) */
952 if (read_only)
Jerry Hoemann07accfa2016-01-06 16:03:41 -0700953 switch (cmd) {
954 case ND_CMD_VENDOR:
955 case ND_CMD_SET_CONFIG_DATA:
956 case ND_CMD_ARS_START:
Dan Williamsd4f32362016-03-03 16:08:54 -0800957 case ND_CMD_CLEAR_ERROR:
Dan Williams31eca762016-04-28 16:23:43 -0700958 case ND_CMD_CALL:
Dan Williams62232e452015-06-08 14:27:06 -0400959 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
960 nvdimm ? nvdimm_cmd_name(cmd)
961 : nvdimm_bus_cmd_name(cmd));
962 return -EPERM;
963 default:
964 break;
965 }
966
967 /* process an input envelope */
968 for (i = 0; i < desc->in_num; i++) {
969 u32 in_size, copy;
970
971 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
972 if (in_size == UINT_MAX) {
973 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
974 __func__, dimm_name, cmd_name, i);
975 return -ENXIO;
976 }
Dan Williams62232e452015-06-08 14:27:06 -0400977 if (in_len < sizeof(in_env))
978 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
979 else
980 copy = 0;
981 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
982 return -EFAULT;
983 in_len += in_size;
984 }
985
Dan Williams31eca762016-04-28 16:23:43 -0700986 if (cmd == ND_CMD_CALL) {
Jerry Hoemann53b85a42017-06-30 20:41:22 -0700987 func = pkg.nd_command;
Dan Williams426824d2018-03-05 16:39:31 -0800988 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
989 dimm_name, pkg.nd_command,
Dan Williams31eca762016-04-28 16:23:43 -0700990 in_len, out_len, buf_len);
Dan Williams31eca762016-04-28 16:23:43 -0700991 }
992
Dan Williams62232e452015-06-08 14:27:06 -0400993 /* process an output envelope */
994 for (i = 0; i < desc->out_num; i++) {
995 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800996 (u32 *) in_env, (u32 *) out_env, 0);
Dan Williams62232e452015-06-08 14:27:06 -0400997 u32 copy;
998
999 if (out_size == UINT_MAX) {
Dan Williams426824d2018-03-05 16:39:31 -08001000 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1001 dimm_name, cmd_name, i);
Dan Williams62232e452015-06-08 14:27:06 -04001002 return -EFAULT;
1003 }
Dan Williams62232e452015-06-08 14:27:06 -04001004 if (out_len < sizeof(out_env))
1005 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1006 else
1007 copy = 0;
1008 if (copy && copy_from_user(&out_env[out_len],
1009 p + in_len + out_len, copy))
1010 return -EFAULT;
1011 out_len += out_size;
1012 }
1013
Dan Williams58738c42017-08-31 15:41:55 -07001014 buf_len = (u64) out_len + (u64) in_len;
Dan Williams62232e452015-06-08 14:27:06 -04001015 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
Dan Williams426824d2018-03-05 16:39:31 -08001016 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1017 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
Dan Williams62232e452015-06-08 14:27:06 -04001018 return -EINVAL;
1019 }
1020
1021 buf = vmalloc(buf_len);
1022 if (!buf)
1023 return -ENOMEM;
1024
1025 if (copy_from_user(buf, p, buf_len)) {
1026 rc = -EFAULT;
1027 goto out;
1028 }
1029
Dan Williamseaf96152015-05-01 13:11:27 -04001030 nvdimm_bus_lock(&nvdimm_bus->dev);
Jerry Hoemann53b85a42017-06-30 20:41:22 -07001031 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
Dan Williamseaf96152015-05-01 13:11:27 -04001032 if (rc)
1033 goto out_unlock;
1034
Dave Jiang006358b2017-04-07 15:33:31 -07001035 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
Dan Williams62232e452015-06-08 14:27:06 -04001036 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -04001037 goto out_unlock;
Dave Jiang006358b2017-04-07 15:33:31 -07001038
1039 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1040 struct nd_cmd_clear_error *clear_err = buf;
Dave Jiang006358b2017-04-07 15:33:31 -07001041
Dan Williams23f49842017-04-29 15:24:03 -07001042 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1043 clear_err->cleared);
Dave Jiang006358b2017-04-07 15:33:31 -07001044 }
Dan Williams0beb2012017-04-07 09:47:24 -07001045 nvdimm_bus_unlock(&nvdimm_bus->dev);
1046
Dan Williams62232e452015-06-08 14:27:06 -04001047 if (copy_to_user(p, buf, buf_len))
1048 rc = -EFAULT;
Dan Williams0beb2012017-04-07 09:47:24 -07001049
1050 vfree(buf);
1051 return rc;
1052
Dan Williamseaf96152015-05-01 13:11:27 -04001053 out_unlock:
1054 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -04001055 out:
1056 vfree(buf);
1057 return rc;
1058}
1059
Dan Williams45def222015-04-26 19:26:48 -04001060static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1061{
Dan Williams62232e452015-06-08 14:27:06 -04001062 long id = (long) file->private_data;
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001063 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001064 struct nvdimm_bus *nvdimm_bus;
1065
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001066 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001067 mutex_lock(&nvdimm_bus_list_mutex);
1068 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1069 if (nvdimm_bus->id == id) {
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001070 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001071 break;
1072 }
1073 }
1074 mutex_unlock(&nvdimm_bus_list_mutex);
1075
1076 return rc;
1077}
1078
1079static int match_dimm(struct device *dev, void *data)
1080{
1081 long id = (long) data;
1082
1083 if (is_nvdimm(dev)) {
1084 struct nvdimm *nvdimm = to_nvdimm(dev);
1085
1086 return nvdimm->id == id;
1087 }
1088
1089 return 0;
1090}
1091
1092static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1093{
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001094 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001095 struct nvdimm_bus *nvdimm_bus;
1096
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001097 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001098 mutex_lock(&nvdimm_bus_list_mutex);
1099 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1100 struct device *dev = device_find_child(&nvdimm_bus->dev,
1101 file->private_data, match_dimm);
1102 struct nvdimm *nvdimm;
1103
1104 if (!dev)
1105 continue;
1106
1107 nvdimm = to_nvdimm(dev);
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001108 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001109 put_device(dev);
1110 break;
1111 }
1112 mutex_unlock(&nvdimm_bus_list_mutex);
1113
1114 return rc;
1115}
1116
1117static int nd_open(struct inode *inode, struct file *file)
1118{
1119 long minor = iminor(inode);
1120
1121 file->private_data = (void *) minor;
1122 return 0;
Dan Williams45def222015-04-26 19:26:48 -04001123}
1124
1125static const struct file_operations nvdimm_bus_fops = {
1126 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -04001127 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -04001128 .unlocked_ioctl = nd_ioctl,
1129 .compat_ioctl = nd_ioctl,
1130 .llseek = noop_llseek,
1131};
1132
Dan Williams62232e452015-06-08 14:27:06 -04001133static const struct file_operations nvdimm_fops = {
1134 .owner = THIS_MODULE,
1135 .open = nd_open,
1136 .unlocked_ioctl = nvdimm_ioctl,
1137 .compat_ioctl = nvdimm_ioctl,
1138 .llseek = noop_llseek,
1139};
1140
Dan Williams45def222015-04-26 19:26:48 -04001141int __init nvdimm_bus_init(void)
1142{
1143 int rc;
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}