blob: 9148015ed8036fc59a3ada2f364b558bd58201d9 [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 Williams3f468332018-06-01 14:10:58 -0700103 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
104 dev->driver->name, dev_name(dev));
105
Dan Williamseaf96152015-05-01 13:11:27 -0400106 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -0400107 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400108 if (rc == 0)
109 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400110 else
111 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400112 nvdimm_bus_probe_end(nvdimm_bus);
113
Dan Williams3f468332018-06-01 14:10:58 -0700114 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
Dan Williams4d88a972015-05-31 14:41:48 -0400115 dev_name(dev), rc);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400116
Dan Williams3d880022015-05-31 15:02:11 -0400117 if (rc != 0)
118 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400119 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);
Dan Williams3d880022015-05-31 15:02:11 -0400125 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400126 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700127 int rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -0400128
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700129 if (nd_drv->remove)
130 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400131 nd_region_disable(nvdimm_bus, dev);
132
Dan Williams4d88a972015-05-31 14:41:48 -0400133 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
134 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400135 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400136 return rc;
137}
138
Dan Williams476f8482016-07-09 00:12:52 -0700139static 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
Dan Williams71999462016-02-18 10:29:49 -0800154void 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
Dan Williams23f49842017-04-29 15:24:03 -0700180struct 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
Toshi Kani975750a2017-06-12 16:25:11 -0600206 if (nd_region->bb_state)
207 sysfs_notify_dirent(nd_region->bb_state);
208
Dan Williams23f49842017-04-29 15:24:03 -0700209 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)
Dave Jiangaa9ad442017-08-23 12:48:26 -0700228 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
Dan Williams23f49842017-04-29 15:24:03 -0700229
230 if (cleared > 0 && cleared / 512)
231 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
232}
233
Dan Williams59e64732016-03-08 07:16:07 -0800234long 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;
Vishal Verma0930a752017-08-30 19:36:02 -0600242 unsigned int noio_flag;
Dan Williams59e64732016-03-08 07:16:07 -0800243 int cmd_rc, rc;
244
245 if (!nvdimm_bus)
246 return -ENXIO;
247
248 nd_desc = nvdimm_bus->nd_desc;
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700249 /*
250 * if ndctl does not exist, it's PMEM_LEGACY and
251 * we want to just pretend everything is handled.
252 */
Dan Williams59e64732016-03-08 07:16:07 -0800253 if (!nd_desc->ndctl)
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700254 return len;
Dan Williams59e64732016-03-08 07:16:07 -0800255
256 memset(&ars_cap, 0, sizeof(ars_cap));
257 ars_cap.address = phys;
258 ars_cap.length = len;
Vishal Verma0930a752017-08-30 19:36:02 -0600259 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800260 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
261 sizeof(ars_cap), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600262 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800263 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;
Vishal Verma0930a752017-08-30 19:36:02 -0600277 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800278 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
279 sizeof(clear_err), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600280 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800281 if (rc < 0)
282 return rc;
283 if (cmd_rc < 0)
284 return cmd_rc;
Vishal Vermae0461142016-09-30 17:19:31 -0600285
Dan Williams23f49842017-04-29 15:24:03 -0700286 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
Toshi Kani8d13c022017-04-27 16:57:05 -0600287
Dan Williams59e64732016-03-08 07:16:07 -0800288 return clear_err.cleared;
289}
290EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
291
Dan Williams18515942016-07-22 23:46:08 -0700292static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
293
Dan Williams4d88a972015-05-31 14:41:48 -0400294static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400295 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400296 .uevent = nvdimm_bus_uevent,
297 .match = nvdimm_bus_match,
298 .probe = nvdimm_bus_probe,
299 .remove = nvdimm_bus_remove,
Dan Williams476f8482016-07-09 00:12:52 -0700300 .shutdown = nvdimm_bus_shutdown,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400301};
302
Dan Williams18515942016-07-22 23:46:08 -0700303static 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);
Dan Williams18515942016-07-22 23:46:08 -0700351 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);
Dave Jiangaa9ad442017-08-23 12:48:26 -0700354 badrange_init(&nvdimm_bus->badrange);
Dan Williams18515942016-07-22 23:46:08 -0700355 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;
Oliver O'Halloran1ff19f42018-04-06 15:21:13 +1000364 nvdimm_bus->dev.of_node = nd_desc->of_node;
Dan Williams18515942016-07-22 23:46:08 -0700365 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
Dave Jiangaa9ad442017-08-23 12:48:26 -0700402static void free_badrange_list(struct list_head *badrange_list)
Dan Williams18515942016-07-22 23:46:08 -0700403{
Dave Jiangaa9ad442017-08-23 12:48:26 -0700404 struct badrange_entry *bre, *next;
Dan Williams18515942016-07-22 23:46:08 -0700405
Dave Jiangaa9ad442017-08-23 12:48:26 -0700406 list_for_each_entry_safe(bre, next, badrange_list, list) {
407 list_del(&bre->list);
408 kfree(bre);
Dan Williams18515942016-07-22 23:46:08 -0700409 }
Dave Jiangaa9ad442017-08-23 12:48:26 -0700410 list_del_init(badrange_list);
Dan Williams18515942016-07-22 23:46:08 -0700411}
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
Dave Jiangaa9ad442017-08-23 12:48:26 -0700424 spin_lock(&nvdimm_bus->badrange.lock);
425 free_badrange_list(&nvdimm_bus->badrange.list);
426 spin_unlock(&nvdimm_bus->badrange.lock);
Dan Williams18515942016-07-22 23:46:08 -0700427
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 },
462};
463
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
Dan Williams4d88a972015-05-31 14:41:48 -0400474static 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);
Alexander Duyckb6eae0f2018-09-25 13:53:02 -0700491 if (dev->parent)
492 put_device(dev->parent);
Dan Williams4d88a972015-05-31 14:41:48 -0400493}
494
495static void nd_async_device_unregister(void *d, async_cookie_t cookie)
496{
497 struct device *dev = d;
498
Dan Williams0ba1c632015-05-30 12:35:36 -0400499 /* flush bus operations before delete */
500 nvdimm_bus_lock(dev);
501 nvdimm_bus_unlock(dev);
502
Dan Williams4d88a972015-05-31 14:41:48 -0400503 device_unregister(dev);
504 put_device(dev);
505}
506
Dan Williams8c2f7e82015-06-25 04:20:04 -0400507void __nd_device_register(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400508{
Dan Williamscd034122016-03-11 10:15:36 -0800509 if (!dev)
510 return;
Dan Williams4d88a972015-05-31 14:41:48 -0400511 dev->bus = &nvdimm_bus_type;
Alexander Duyckb6eae0f2018-09-25 13:53:02 -0700512 if (dev->parent)
513 get_device(dev->parent);
Dan Williams4d88a972015-05-31 14:41:48 -0400514 get_device(dev);
515 async_schedule_domain(nd_async_device_register, dev,
516 &nd_async_domain);
517}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400518
519void nd_device_register(struct device *dev)
520{
521 device_initialize(dev);
522 __nd_device_register(dev);
523}
Dan Williams4d88a972015-05-31 14:41:48 -0400524EXPORT_SYMBOL(nd_device_register);
525
526void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
527{
528 switch (mode) {
529 case ND_ASYNC:
530 get_device(dev);
531 async_schedule_domain(nd_async_device_unregister, dev,
532 &nd_async_domain);
533 break;
534 case ND_SYNC:
535 nd_synchronize();
536 device_unregister(dev);
537 break;
538 }
539}
540EXPORT_SYMBOL(nd_device_unregister);
541
542/**
543 * __nd_driver_register() - register a region or a namespace driver
544 * @nd_drv: driver to register
545 * @owner: automatically set by nd_driver_register() macro
546 * @mod_name: automatically set by nd_driver_register() macro
547 */
548int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
549 const char *mod_name)
550{
551 struct device_driver *drv = &nd_drv->drv;
552
553 if (!nd_drv->type) {
554 pr_debug("driver type bitmask not set (%pf)\n",
555 __builtin_return_address(0));
556 return -EINVAL;
557 }
558
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700559 if (!nd_drv->probe) {
560 pr_debug("%s ->probe() must be specified\n", mod_name);
Dan Williams4d88a972015-05-31 14:41:48 -0400561 return -EINVAL;
562 }
563
564 drv->bus = &nvdimm_bus_type;
565 drv->owner = owner;
566 drv->mod_name = mod_name;
567
568 return driver_register(drv);
569}
570EXPORT_SYMBOL(__nd_driver_register);
571
Dan Williams58138822015-06-23 20:08:34 -0400572int nvdimm_revalidate_disk(struct gendisk *disk)
573{
Dan Williams52c44d92016-06-15 19:43:07 -0700574 struct device *dev = disk_to_dev(disk)->parent;
Dan Williams58138822015-06-23 20:08:34 -0400575 struct nd_region *nd_region = to_nd_region(dev->parent);
Robert Elliott254a4cd2018-05-31 18:36:36 -0500576 int disk_ro = get_disk_ro(disk);
Dan Williams58138822015-06-23 20:08:34 -0400577
Robert Elliott254a4cd2018-05-31 18:36:36 -0500578 /*
579 * Upgrade to read-only if the region is read-only preserve as
580 * read-only if the disk is already read-only.
581 */
582 if (disk_ro || nd_region->ro == disk_ro)
Dan Williams58138822015-06-23 20:08:34 -0400583 return 0;
584
Robert Elliott254a4cd2018-05-31 18:36:36 -0500585 dev_info(dev, "%s read-only, marking %s read-only\n",
586 dev_name(&nd_region->dev), disk->disk_name);
587 set_disk_ro(disk, 1);
Dan Williams58138822015-06-23 20:08:34 -0400588
589 return 0;
590
591}
592EXPORT_SYMBOL(nvdimm_revalidate_disk);
593
Dan Williams4d88a972015-05-31 14:41:48 -0400594static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
595 char *buf)
596{
597 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
598 to_nd_device_type(dev));
599}
600static DEVICE_ATTR_RO(modalias);
601
602static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
603 char *buf)
604{
605 return sprintf(buf, "%s\n", dev->type->name);
606}
607static DEVICE_ATTR_RO(devtype);
608
609static struct attribute *nd_device_attributes[] = {
610 &dev_attr_modalias.attr,
611 &dev_attr_devtype.attr,
612 NULL,
613};
614
615/**
616 * nd_device_attribute_group - generic attributes for all devices on an nd bus
617 */
618struct attribute_group nd_device_attribute_group = {
619 .attrs = nd_device_attributes,
620};
621EXPORT_SYMBOL_GPL(nd_device_attribute_group);
622
Toshi Kani74ae66c2015-06-19 12:18:34 -0600623static ssize_t numa_node_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
625{
626 return sprintf(buf, "%d\n", dev_to_node(dev));
627}
628static DEVICE_ATTR_RO(numa_node);
629
630static struct attribute *nd_numa_attributes[] = {
631 &dev_attr_numa_node.attr,
632 NULL,
633};
634
635static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
636 int n)
637{
638 if (!IS_ENABLED(CONFIG_NUMA))
639 return 0;
640
641 return a->mode;
642}
643
644/**
645 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
646 */
647struct attribute_group nd_numa_attribute_group = {
648 .attrs = nd_numa_attributes,
649 .is_visible = nd_numa_attr_visible,
650};
651EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
652
Dan Williams45def222015-04-26 19:26:48 -0400653int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
654{
655 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
656 struct device *dev;
657
658 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
659 "ndctl%d", nvdimm_bus->id);
660
Dan Williams42588952016-05-27 13:28:31 -0700661 if (IS_ERR(dev))
Dan Williams45def222015-04-26 19:26:48 -0400662 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
663 nvdimm_bus->id, PTR_ERR(dev));
Dan Williams42588952016-05-27 13:28:31 -0700664 return PTR_ERR_OR_ZERO(dev);
Dan Williams45def222015-04-26 19:26:48 -0400665}
666
667void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
668{
669 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
670}
671
Dan Williams62232e452015-06-08 14:27:06 -0400672static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
673 [ND_CMD_IMPLEMENTED] = { },
674 [ND_CMD_SMART] = {
675 .out_num = 2,
Dan Williams21129112016-04-07 19:58:44 -0700676 .out_sizes = { 4, 128, },
Dan Williams62232e452015-06-08 14:27:06 -0400677 },
678 [ND_CMD_SMART_THRESHOLD] = {
679 .out_num = 2,
680 .out_sizes = { 4, 8, },
681 },
682 [ND_CMD_DIMM_FLAGS] = {
683 .out_num = 2,
684 .out_sizes = { 4, 4 },
685 },
686 [ND_CMD_GET_CONFIG_SIZE] = {
687 .out_num = 3,
688 .out_sizes = { 4, 4, 4, },
689 },
690 [ND_CMD_GET_CONFIG_DATA] = {
691 .in_num = 2,
692 .in_sizes = { 4, 4, },
693 .out_num = 2,
694 .out_sizes = { 4, UINT_MAX, },
695 },
696 [ND_CMD_SET_CONFIG_DATA] = {
697 .in_num = 3,
698 .in_sizes = { 4, 4, UINT_MAX, },
699 .out_num = 1,
700 .out_sizes = { 4, },
701 },
702 [ND_CMD_VENDOR] = {
703 .in_num = 3,
704 .in_sizes = { 4, 4, UINT_MAX, },
705 .out_num = 3,
706 .out_sizes = { 4, 4, UINT_MAX, },
707 },
Dan Williams31eca762016-04-28 16:23:43 -0700708 [ND_CMD_CALL] = {
709 .in_num = 2,
710 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
711 .out_num = 1,
712 .out_sizes = { UINT_MAX, },
713 },
Dan Williams62232e452015-06-08 14:27:06 -0400714};
715
716const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
717{
718 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
719 return &__nd_cmd_dimm_descs[cmd];
720 return NULL;
721}
722EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
723
724static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
725 [ND_CMD_IMPLEMENTED] = { },
726 [ND_CMD_ARS_CAP] = {
727 .in_num = 2,
728 .in_sizes = { 8, 8, },
Dan Williams4577b062016-02-17 13:08:58 -0800729 .out_num = 4,
730 .out_sizes = { 4, 4, 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400731 },
732 [ND_CMD_ARS_START] = {
Dan Williams4577b062016-02-17 13:08:58 -0800733 .in_num = 5,
734 .in_sizes = { 8, 8, 2, 1, 5, },
735 .out_num = 2,
736 .out_sizes = { 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400737 },
738 [ND_CMD_ARS_STATUS] = {
Dan Williams747ffe12016-02-19 15:21:14 -0800739 .out_num = 3,
740 .out_sizes = { 4, 4, UINT_MAX, },
Dan Williams62232e452015-06-08 14:27:06 -0400741 },
Dan Williamsd4f32362016-03-03 16:08:54 -0800742 [ND_CMD_CLEAR_ERROR] = {
743 .in_num = 2,
744 .in_sizes = { 8, 8, },
745 .out_num = 3,
746 .out_sizes = { 4, 4, 8, },
747 },
Dan Williams31eca762016-04-28 16:23:43 -0700748 [ND_CMD_CALL] = {
749 .in_num = 2,
750 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
751 .out_num = 1,
752 .out_sizes = { UINT_MAX, },
753 },
Dan Williams62232e452015-06-08 14:27:06 -0400754};
755
756const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
757{
758 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
759 return &__nd_cmd_bus_descs[cmd];
760 return NULL;
761}
762EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
763
764u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
765 const struct nd_cmd_desc *desc, int idx, void *buf)
766{
767 if (idx >= desc->in_num)
768 return UINT_MAX;
769
770 if (desc->in_sizes[idx] < UINT_MAX)
771 return desc->in_sizes[idx];
772
773 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
774 struct nd_cmd_set_config_hdr *hdr = buf;
775
776 return hdr->in_length;
777 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
778 struct nd_cmd_vendor_hdr *hdr = buf;
779
780 return hdr->in_length;
Dan Williams31eca762016-04-28 16:23:43 -0700781 } else if (cmd == ND_CMD_CALL) {
782 struct nd_cmd_pkg *pkg = buf;
783
784 return pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400785 }
786
787 return UINT_MAX;
788}
789EXPORT_SYMBOL_GPL(nd_cmd_in_size);
790
791u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
792 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800793 const u32 *out_field, unsigned long remainder)
Dan Williams62232e452015-06-08 14:27:06 -0400794{
795 if (idx >= desc->out_num)
796 return UINT_MAX;
797
798 if (desc->out_sizes[idx] < UINT_MAX)
799 return desc->out_sizes[idx];
800
801 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
802 return in_field[1];
803 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
804 return out_field[1];
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800805 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
806 /*
807 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
808 * "Size of Output Buffer in bytes, including this
809 * field."
810 */
811 if (out_field[1] < 4)
812 return 0;
813 /*
814 * ACPI 6.1 is ambiguous if 'status' is included in the
815 * output size. If we encounter an output size that
816 * overshoots the remainder by 4 bytes, assume it was
817 * including 'status'.
818 */
Vishal Verma286e8772018-08-10 13:23:15 -0600819 if (out_field[1] - 4 == remainder)
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800820 return remainder;
Vishal Verma286e8772018-08-10 13:23:15 -0600821 return out_field[1] - 8;
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800822 } else if (cmd == ND_CMD_CALL) {
Dan Williams31eca762016-04-28 16:23:43 -0700823 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
824
825 return pkg->nd_size_out;
826 }
827
Dan Williams62232e452015-06-08 14:27:06 -0400828
829 return UINT_MAX;
830}
831EXPORT_SYMBOL_GPL(nd_cmd_out_size);
832
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400833void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400834{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400835 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
836
Dan Williamseaf96152015-05-01 13:11:27 -0400837 do {
838 if (nvdimm_bus->probe_active == 0)
839 break;
840 nvdimm_bus_unlock(&nvdimm_bus->dev);
841 wait_event(nvdimm_bus->probe_wait,
842 nvdimm_bus->probe_active == 0);
843 nvdimm_bus_lock(&nvdimm_bus->dev);
844 } while (true);
845}
846
Dave Jiang006358b2017-04-07 15:33:31 -0700847static int nd_pmem_forget_poison_check(struct device *dev, void *data)
Dan Williamsd4f32362016-03-03 16:08:54 -0800848{
Dave Jiang006358b2017-04-07 15:33:31 -0700849 struct nd_cmd_clear_error *clear_err =
850 (struct nd_cmd_clear_error *)data;
851 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
852 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
853 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
854 struct nd_namespace_common *ndns = NULL;
855 struct nd_namespace_io *nsio;
856 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
857
858 if (nd_dax || !dev->driver)
859 return 0;
860
861 start = clear_err->address;
862 end = clear_err->address + clear_err->cleared - 1;
863
864 if (nd_btt || nd_pfn || nd_dax) {
865 if (nd_btt)
866 ndns = nd_btt->ndns;
867 else if (nd_pfn)
868 ndns = nd_pfn->ndns;
869 else if (nd_dax)
870 ndns = nd_dax->nd_pfn.ndns;
871
872 if (!ndns)
873 return 0;
874 } else
875 ndns = to_ndns(dev);
876
877 nsio = to_nd_namespace_io(&ndns->dev);
878 pstart = nsio->res.start + offset;
879 pend = nsio->res.end - end_trunc;
880
881 if ((pstart >= start) && (pend <= end))
Dan Williamsd4f32362016-03-03 16:08:54 -0800882 return -EBUSY;
Dave Jiang006358b2017-04-07 15:33:31 -0700883
Dan Williamsd4f32362016-03-03 16:08:54 -0800884 return 0;
Dave Jiang006358b2017-04-07 15:33:31 -0700885
886}
887
888static int nd_ns_forget_poison_check(struct device *dev, void *data)
889{
890 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800891}
892
Dan Williamseaf96152015-05-01 13:11:27 -0400893/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800894static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
Dave Jiang006358b2017-04-07 15:33:31 -0700895 struct nvdimm *nvdimm, unsigned int cmd, void *data)
Dan Williamseaf96152015-05-01 13:11:27 -0400896{
Dan Williams87bf5722016-02-22 21:50:31 -0800897 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
898
899 /* ask the bus provider if it would like to block this request */
900 if (nd_desc->clear_to_send) {
901 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
902
903 if (rc)
904 return rc;
905 }
Dan Williamseaf96152015-05-01 13:11:27 -0400906
Dan Williamsd4f32362016-03-03 16:08:54 -0800907 /* require clear error to go through the pmem driver */
908 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
Dave Jiang006358b2017-04-07 15:33:31 -0700909 return device_for_each_child(&nvdimm_bus->dev, data,
910 nd_ns_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800911
Dan Williamseaf96152015-05-01 13:11:27 -0400912 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
913 return 0;
914
Dan Williams87bf5722016-02-22 21:50:31 -0800915 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400916 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400917 if (atomic_read(&nvdimm->busy))
918 return -EBUSY;
919 return 0;
920}
921
Dan Williams62232e452015-06-08 14:27:06 -0400922static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
923 int read_only, unsigned int ioctl_cmd, unsigned long arg)
924{
925 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
Dan Williams62232e452015-06-08 14:27:06 -0400926 static char out_env[ND_CMD_MAX_ENVELOPE];
927 static char in_env[ND_CMD_MAX_ENVELOPE];
928 const struct nd_cmd_desc *desc = NULL;
929 unsigned int cmd = _IOC_NR(ioctl_cmd);
Dan Williams62232e452015-06-08 14:27:06 -0400930 struct device *dev = &nvdimm_bus->dev;
Dan Williams58738c42017-08-31 15:41:55 -0700931 void __user *p = (void __user *) arg;
Dan Williams62232e452015-06-08 14:27:06 -0400932 const char *cmd_name, *dimm_name;
Dan Williams58738c42017-08-31 15:41:55 -0700933 u32 in_len = 0, out_len = 0;
934 unsigned int func = cmd;
Dan Williamse3654ec2016-04-28 16:17:07 -0700935 unsigned long cmd_mask;
Dan Williams58738c42017-08-31 15:41:55 -0700936 struct nd_cmd_pkg pkg;
Dave Jiang006358b2017-04-07 15:33:31 -0700937 int rc, i, cmd_rc;
Dan Williams58738c42017-08-31 15:41:55 -0700938 u64 buf_len = 0;
939 void *buf;
Dan Williams62232e452015-06-08 14:27:06 -0400940
941 if (nvdimm) {
942 desc = nd_cmd_dimm_desc(cmd);
943 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700944 cmd_mask = nvdimm->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400945 dimm_name = dev_name(&nvdimm->dev);
946 } else {
947 desc = nd_cmd_bus_desc(cmd);
948 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700949 cmd_mask = nd_desc->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400950 dimm_name = "bus";
951 }
952
Dan Williams31eca762016-04-28 16:23:43 -0700953 if (cmd == ND_CMD_CALL) {
954 if (copy_from_user(&pkg, p, sizeof(pkg)))
955 return -EFAULT;
956 }
957
Dan Williams62232e452015-06-08 14:27:06 -0400958 if (!desc || (desc->out_num + desc->in_num == 0) ||
Dan Williamse3654ec2016-04-28 16:17:07 -0700959 !test_bit(cmd, &cmd_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400960 return -ENOTTY;
961
962 /* fail write commands (when read-only) */
963 if (read_only)
Jerry Hoemann07accfa2016-01-06 16:03:41 -0700964 switch (cmd) {
965 case ND_CMD_VENDOR:
966 case ND_CMD_SET_CONFIG_DATA:
967 case ND_CMD_ARS_START:
Dan Williamsd4f32362016-03-03 16:08:54 -0800968 case ND_CMD_CLEAR_ERROR:
Dan Williams31eca762016-04-28 16:23:43 -0700969 case ND_CMD_CALL:
Dan Williams62232e452015-06-08 14:27:06 -0400970 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
971 nvdimm ? nvdimm_cmd_name(cmd)
972 : nvdimm_bus_cmd_name(cmd));
973 return -EPERM;
974 default:
975 break;
976 }
977
978 /* process an input envelope */
979 for (i = 0; i < desc->in_num; i++) {
980 u32 in_size, copy;
981
982 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
983 if (in_size == UINT_MAX) {
984 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
985 __func__, dimm_name, cmd_name, i);
986 return -ENXIO;
987 }
Dan Williams62232e452015-06-08 14:27:06 -0400988 if (in_len < sizeof(in_env))
989 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
990 else
991 copy = 0;
992 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
993 return -EFAULT;
994 in_len += in_size;
995 }
996
Dan Williams31eca762016-04-28 16:23:43 -0700997 if (cmd == ND_CMD_CALL) {
Jerry Hoemann53b85a42017-06-30 20:41:22 -0700998 func = pkg.nd_command;
Dan Williams426824d2018-03-05 16:39:31 -0800999 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1000 dimm_name, pkg.nd_command,
Dan Williams31eca762016-04-28 16:23:43 -07001001 in_len, out_len, buf_len);
Dan Williams31eca762016-04-28 16:23:43 -07001002 }
1003
Dan Williams62232e452015-06-08 14:27:06 -04001004 /* process an output envelope */
1005 for (i = 0; i < desc->out_num; i++) {
1006 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
Dan Williamsefda1b5d2016-12-06 09:10:12 -08001007 (u32 *) in_env, (u32 *) out_env, 0);
Dan Williams62232e452015-06-08 14:27:06 -04001008 u32 copy;
1009
1010 if (out_size == UINT_MAX) {
Dan Williams426824d2018-03-05 16:39:31 -08001011 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1012 dimm_name, cmd_name, i);
Dan Williams62232e452015-06-08 14:27:06 -04001013 return -EFAULT;
1014 }
Dan Williams62232e452015-06-08 14:27:06 -04001015 if (out_len < sizeof(out_env))
1016 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1017 else
1018 copy = 0;
1019 if (copy && copy_from_user(&out_env[out_len],
1020 p + in_len + out_len, copy))
1021 return -EFAULT;
1022 out_len += out_size;
1023 }
1024
Dan Williams58738c42017-08-31 15:41:55 -07001025 buf_len = (u64) out_len + (u64) in_len;
Dan Williams62232e452015-06-08 14:27:06 -04001026 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
Dan Williams426824d2018-03-05 16:39:31 -08001027 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1028 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
Dan Williams62232e452015-06-08 14:27:06 -04001029 return -EINVAL;
1030 }
1031
1032 buf = vmalloc(buf_len);
1033 if (!buf)
1034 return -ENOMEM;
1035
1036 if (copy_from_user(buf, p, buf_len)) {
1037 rc = -EFAULT;
1038 goto out;
1039 }
1040
Dan Williamseaf96152015-05-01 13:11:27 -04001041 nvdimm_bus_lock(&nvdimm_bus->dev);
Jerry Hoemann53b85a42017-06-30 20:41:22 -07001042 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
Dan Williamseaf96152015-05-01 13:11:27 -04001043 if (rc)
1044 goto out_unlock;
1045
Dave Jiang006358b2017-04-07 15:33:31 -07001046 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
Dan Williams62232e452015-06-08 14:27:06 -04001047 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -04001048 goto out_unlock;
Dave Jiang006358b2017-04-07 15:33:31 -07001049
1050 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1051 struct nd_cmd_clear_error *clear_err = buf;
Dave Jiang006358b2017-04-07 15:33:31 -07001052
Dan Williams23f49842017-04-29 15:24:03 -07001053 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1054 clear_err->cleared);
Dave Jiang006358b2017-04-07 15:33:31 -07001055 }
Dan Williams0beb2012017-04-07 09:47:24 -07001056 nvdimm_bus_unlock(&nvdimm_bus->dev);
1057
Dan Williams62232e452015-06-08 14:27:06 -04001058 if (copy_to_user(p, buf, buf_len))
1059 rc = -EFAULT;
Dan Williams0beb2012017-04-07 09:47:24 -07001060
1061 vfree(buf);
1062 return rc;
1063
Dan Williamseaf96152015-05-01 13:11:27 -04001064 out_unlock:
1065 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -04001066 out:
1067 vfree(buf);
1068 return rc;
1069}
1070
Dan Williams45def222015-04-26 19:26:48 -04001071static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1072{
Dan Williams62232e452015-06-08 14:27:06 -04001073 long id = (long) file->private_data;
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001074 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001075 struct nvdimm_bus *nvdimm_bus;
1076
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001077 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001078 mutex_lock(&nvdimm_bus_list_mutex);
1079 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1080 if (nvdimm_bus->id == id) {
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001081 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001082 break;
1083 }
1084 }
1085 mutex_unlock(&nvdimm_bus_list_mutex);
1086
1087 return rc;
1088}
1089
1090static int match_dimm(struct device *dev, void *data)
1091{
1092 long id = (long) data;
1093
1094 if (is_nvdimm(dev)) {
1095 struct nvdimm *nvdimm = to_nvdimm(dev);
1096
1097 return nvdimm->id == id;
1098 }
1099
1100 return 0;
1101}
1102
1103static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1104{
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001105 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001106 struct nvdimm_bus *nvdimm_bus;
1107
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001108 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001109 mutex_lock(&nvdimm_bus_list_mutex);
1110 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1111 struct device *dev = device_find_child(&nvdimm_bus->dev,
1112 file->private_data, match_dimm);
1113 struct nvdimm *nvdimm;
1114
1115 if (!dev)
1116 continue;
1117
1118 nvdimm = to_nvdimm(dev);
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001119 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001120 put_device(dev);
1121 break;
1122 }
1123 mutex_unlock(&nvdimm_bus_list_mutex);
1124
1125 return rc;
1126}
1127
1128static int nd_open(struct inode *inode, struct file *file)
1129{
1130 long minor = iminor(inode);
1131
1132 file->private_data = (void *) minor;
1133 return 0;
Dan Williams45def222015-04-26 19:26:48 -04001134}
1135
1136static const struct file_operations nvdimm_bus_fops = {
1137 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -04001138 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -04001139 .unlocked_ioctl = nd_ioctl,
1140 .compat_ioctl = nd_ioctl,
1141 .llseek = noop_llseek,
1142};
1143
Dan Williams62232e452015-06-08 14:27:06 -04001144static const struct file_operations nvdimm_fops = {
1145 .owner = THIS_MODULE,
1146 .open = nd_open,
1147 .unlocked_ioctl = nvdimm_ioctl,
1148 .compat_ioctl = nvdimm_ioctl,
1149 .llseek = noop_llseek,
1150};
1151
Dan Williams45def222015-04-26 19:26:48 -04001152int __init nvdimm_bus_init(void)
1153{
1154 int rc;
1155
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001156 rc = bus_register(&nvdimm_bus_type);
1157 if (rc)
1158 return rc;
1159
Dan Williams45def222015-04-26 19:26:48 -04001160 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1161 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -04001162 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -04001163 nvdimm_bus_major = rc;
1164
Dan Williams62232e452015-06-08 14:27:06 -04001165 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1166 if (rc < 0)
1167 goto err_dimm_chrdev;
1168 nvdimm_major = rc;
1169
Dan Williams45def222015-04-26 19:26:48 -04001170 nd_class = class_create(THIS_MODULE, "nd");
Axel Lindaa1dee2015-06-28 17:00:57 +08001171 if (IS_ERR(nd_class)) {
1172 rc = PTR_ERR(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001173 goto err_class;
Axel Lindaa1dee2015-06-28 17:00:57 +08001174 }
Dan Williams45def222015-04-26 19:26:48 -04001175
Dan Williams18515942016-07-22 23:46:08 -07001176 rc = driver_register(&nd_bus_driver.drv);
1177 if (rc)
1178 goto err_nd_bus;
1179
Dan Williams45def222015-04-26 19:26:48 -04001180 return 0;
1181
Dan Williams18515942016-07-22 23:46:08 -07001182 err_nd_bus:
1183 class_destroy(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001184 err_class:
Dan Williams62232e452015-06-08 14:27:06 -04001185 unregister_chrdev(nvdimm_major, "dimmctl");
1186 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -04001187 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001188 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001189 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -04001190
1191 return rc;
1192}
1193
Dan Williams4d88a972015-05-31 14:41:48 -04001194void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -04001195{
Dan Williams18515942016-07-22 23:46:08 -07001196 driver_unregister(&nd_bus_driver.drv);
Dan Williams45def222015-04-26 19:26:48 -04001197 class_destroy(nd_class);
1198 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001199 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001200 bus_unregister(&nvdimm_bus_type);
Dan Williams18515942016-07-22 23:46:08 -07001201 ida_destroy(&nd_ida);
Dan Williams45def222015-04-26 19:26:48 -04001202}