blob: f1fb39921236eafe80505965d86a16d1a44778de [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{
57 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
58 to_nd_device_type(dev));
59}
60
Dan Williams3d880022015-05-31 15:02:11 -040061static struct module *to_bus_provider(struct device *dev)
62{
63 /* pin bus providers while regions are enabled */
Dan Williamsc9e582a2017-05-29 23:12:19 -070064 if (is_nd_region(dev)) {
Dan Williams3d880022015-05-31 15:02:11 -040065 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
66
Dan Williamsbc9775d2016-07-21 20:03:19 -070067 return nvdimm_bus->nd_desc->module;
Dan Williams3d880022015-05-31 15:02:11 -040068 }
69 return NULL;
70}
71
Dan Williamseaf96152015-05-01 13:11:27 -040072static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
73{
74 nvdimm_bus_lock(&nvdimm_bus->dev);
75 nvdimm_bus->probe_active++;
76 nvdimm_bus_unlock(&nvdimm_bus->dev);
77}
78
79static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
80{
81 nvdimm_bus_lock(&nvdimm_bus->dev);
82 if (--nvdimm_bus->probe_active == 0)
83 wake_up(&nvdimm_bus->probe_wait);
84 nvdimm_bus_unlock(&nvdimm_bus->dev);
85}
86
Dan Williams4d88a972015-05-31 14:41:48 -040087static int nvdimm_bus_probe(struct device *dev)
88{
89 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -040090 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040091 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
92 int rc;
93
Dan Williams3d880022015-05-31 15:02:11 -040094 if (!try_module_get(provider))
95 return -ENXIO;
96
Dan Williams3f468332018-06-01 14:10:58 -070097 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
98 dev->driver->name, dev_name(dev));
99
Dan Williamseaf96152015-05-01 13:11:27 -0400100 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -0400101 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400102 if (rc == 0)
103 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400104 else
105 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400106 nvdimm_bus_probe_end(nvdimm_bus);
107
Dan Williams3f468332018-06-01 14:10:58 -0700108 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
Dan Williams4d88a972015-05-31 14:41:48 -0400109 dev_name(dev), rc);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400110
Dan Williams3d880022015-05-31 15:02:11 -0400111 if (rc != 0)
112 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400113 return rc;
114}
115
116static int nvdimm_bus_remove(struct device *dev)
117{
118 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -0400119 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400120 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700121 int rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -0400122
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700123 if (nd_drv->remove)
124 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400125 nd_region_disable(nvdimm_bus, dev);
126
Dan Williams4d88a972015-05-31 14:41:48 -0400127 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
128 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400129 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400130 return rc;
131}
132
Dan Williams476f8482016-07-09 00:12:52 -0700133static void nvdimm_bus_shutdown(struct device *dev)
134{
135 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
136 struct nd_device_driver *nd_drv = NULL;
137
138 if (dev->driver)
139 nd_drv = to_nd_device_driver(dev->driver);
140
141 if (nd_drv && nd_drv->shutdown) {
142 nd_drv->shutdown(dev);
143 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
144 dev->driver->name, dev_name(dev));
145 }
146}
147
Dan Williams71999462016-02-18 10:29:49 -0800148void nd_device_notify(struct device *dev, enum nvdimm_event event)
149{
150 device_lock(dev);
151 if (dev->driver) {
152 struct nd_device_driver *nd_drv;
153
154 nd_drv = to_nd_device_driver(dev->driver);
155 if (nd_drv->notify)
156 nd_drv->notify(dev, event);
157 }
158 device_unlock(dev);
159}
160EXPORT_SYMBOL(nd_device_notify);
161
162void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
163{
164 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
165
166 if (!nvdimm_bus)
167 return;
168
169 /* caller is responsible for holding a reference on the device */
170 nd_device_notify(&nd_region->dev, event);
171}
172EXPORT_SYMBOL_GPL(nvdimm_region_notify);
173
Dan Williams23f49842017-04-29 15:24:03 -0700174struct clear_badblocks_context {
175 resource_size_t phys, cleared;
176};
177
178static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
179{
180 struct clear_badblocks_context *ctx = data;
181 struct nd_region *nd_region;
182 resource_size_t ndr_end;
183 sector_t sector;
184
185 /* make sure device is a region */
186 if (!is_nd_pmem(dev))
187 return 0;
188
189 nd_region = to_nd_region(dev);
190 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
191
192 /* make sure we are in the region */
193 if (ctx->phys < nd_region->ndr_start
194 || (ctx->phys + ctx->cleared) > ndr_end)
195 return 0;
196
197 sector = (ctx->phys - nd_region->ndr_start) / 512;
198 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
199
Toshi Kani975750a2017-06-12 16:25:11 -0600200 if (nd_region->bb_state)
201 sysfs_notify_dirent(nd_region->bb_state);
202
Dan Williams23f49842017-04-29 15:24:03 -0700203 return 0;
204}
205
206static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
207 phys_addr_t phys, u64 cleared)
208{
209 struct clear_badblocks_context ctx = {
210 .phys = phys,
211 .cleared = cleared,
212 };
213
214 device_for_each_child(&nvdimm_bus->dev, &ctx,
215 nvdimm_clear_badblocks_region);
216}
217
218static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
219 phys_addr_t phys, u64 cleared)
220{
221 if (cleared > 0)
Dave Jiangaa9ad442017-08-23 12:48:26 -0700222 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
Dan Williams23f49842017-04-29 15:24:03 -0700223
224 if (cleared > 0 && cleared / 512)
225 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
226}
227
Dan Williams59e64732016-03-08 07:16:07 -0800228long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
229 unsigned int len)
230{
231 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
232 struct nvdimm_bus_descriptor *nd_desc;
233 struct nd_cmd_clear_error clear_err;
234 struct nd_cmd_ars_cap ars_cap;
235 u32 clear_err_unit, mask;
Vishal Verma0930a752017-08-30 19:36:02 -0600236 unsigned int noio_flag;
Dan Williams59e64732016-03-08 07:16:07 -0800237 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;
Vishal Verma0930a752017-08-30 19:36:02 -0600253 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800254 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
255 sizeof(ars_cap), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600256 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800257 if (rc < 0)
258 return rc;
259 if (cmd_rc < 0)
260 return cmd_rc;
261 clear_err_unit = ars_cap.clear_err_unit;
262 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
263 return -ENXIO;
264
265 mask = clear_err_unit - 1;
266 if ((phys | len) & mask)
267 return -ENXIO;
268 memset(&clear_err, 0, sizeof(clear_err));
269 clear_err.address = phys;
270 clear_err.length = len;
Vishal Verma0930a752017-08-30 19:36:02 -0600271 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800272 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
273 sizeof(clear_err), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600274 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800275 if (rc < 0)
276 return rc;
277 if (cmd_rc < 0)
278 return cmd_rc;
Vishal Vermae0461142016-09-30 17:19:31 -0600279
Dan Williams23f49842017-04-29 15:24:03 -0700280 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
Toshi Kani8d13c022017-04-27 16:57:05 -0600281
Dan Williams59e64732016-03-08 07:16:07 -0800282 return clear_err.cleared;
283}
284EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
285
Dan Williams18515942016-07-22 23:46:08 -0700286static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
287
Dan Williams4d88a972015-05-31 14:41:48 -0400288static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400289 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400290 .uevent = nvdimm_bus_uevent,
291 .match = nvdimm_bus_match,
292 .probe = nvdimm_bus_probe,
293 .remove = nvdimm_bus_remove,
Dan Williams476f8482016-07-09 00:12:52 -0700294 .shutdown = nvdimm_bus_shutdown,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400295};
296
Dan Williams18515942016-07-22 23:46:08 -0700297static void nvdimm_bus_release(struct device *dev)
298{
299 struct nvdimm_bus *nvdimm_bus;
300
301 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
302 ida_simple_remove(&nd_ida, nvdimm_bus->id);
303 kfree(nvdimm_bus);
304}
305
306static bool is_nvdimm_bus(struct device *dev)
307{
308 return dev->release == nvdimm_bus_release;
309}
310
311struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
312{
313 struct device *dev;
314
315 for (dev = nd_dev; dev; dev = dev->parent)
316 if (is_nvdimm_bus(dev))
317 break;
318 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
319 if (dev)
320 return to_nvdimm_bus(dev);
321 return NULL;
322}
323
324struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
325{
326 struct nvdimm_bus *nvdimm_bus;
327
328 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
329 WARN_ON(!is_nvdimm_bus(dev));
330 return nvdimm_bus;
331}
332EXPORT_SYMBOL_GPL(to_nvdimm_bus);
333
334struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
335 struct nvdimm_bus_descriptor *nd_desc)
336{
337 struct nvdimm_bus *nvdimm_bus;
338 int rc;
339
340 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
341 if (!nvdimm_bus)
342 return NULL;
343 INIT_LIST_HEAD(&nvdimm_bus->list);
344 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
Dan Williams18515942016-07-22 23:46:08 -0700345 init_waitqueue_head(&nvdimm_bus->probe_wait);
346 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
347 mutex_init(&nvdimm_bus->reconfig_mutex);
Dave Jiangaa9ad442017-08-23 12:48:26 -0700348 badrange_init(&nvdimm_bus->badrange);
Dan Williams18515942016-07-22 23:46:08 -0700349 if (nvdimm_bus->id < 0) {
350 kfree(nvdimm_bus);
351 return NULL;
352 }
353 nvdimm_bus->nd_desc = nd_desc;
354 nvdimm_bus->dev.parent = parent;
355 nvdimm_bus->dev.release = nvdimm_bus_release;
356 nvdimm_bus->dev.groups = nd_desc->attr_groups;
357 nvdimm_bus->dev.bus = &nvdimm_bus_type;
Oliver O'Halloran1ff19f42018-04-06 15:21:13 +1000358 nvdimm_bus->dev.of_node = nd_desc->of_node;
Dan Williams18515942016-07-22 23:46:08 -0700359 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
360 rc = device_register(&nvdimm_bus->dev);
361 if (rc) {
362 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
363 goto err;
364 }
365
366 return nvdimm_bus;
367 err:
368 put_device(&nvdimm_bus->dev);
369 return NULL;
370}
371EXPORT_SYMBOL_GPL(nvdimm_bus_register);
372
373void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
374{
375 if (!nvdimm_bus)
376 return;
377 device_unregister(&nvdimm_bus->dev);
378}
379EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
380
381static int child_unregister(struct device *dev, void *data)
382{
383 /*
384 * the singular ndctl class device per bus needs to be
385 * "device_destroy"ed, so skip it here
386 *
387 * i.e. remove classless children
388 */
389 if (dev->class)
390 /* pass */;
391 else
392 nd_device_unregister(dev, ND_SYNC);
393 return 0;
394}
395
Dave Jiangaa9ad442017-08-23 12:48:26 -0700396static void free_badrange_list(struct list_head *badrange_list)
Dan Williams18515942016-07-22 23:46:08 -0700397{
Dave Jiangaa9ad442017-08-23 12:48:26 -0700398 struct badrange_entry *bre, *next;
Dan Williams18515942016-07-22 23:46:08 -0700399
Dave Jiangaa9ad442017-08-23 12:48:26 -0700400 list_for_each_entry_safe(bre, next, badrange_list, list) {
401 list_del(&bre->list);
402 kfree(bre);
Dan Williams18515942016-07-22 23:46:08 -0700403 }
Dave Jiangaa9ad442017-08-23 12:48:26 -0700404 list_del_init(badrange_list);
Dan Williams18515942016-07-22 23:46:08 -0700405}
406
407static int nd_bus_remove(struct device *dev)
408{
409 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
410
411 mutex_lock(&nvdimm_bus_list_mutex);
412 list_del_init(&nvdimm_bus->list);
413 mutex_unlock(&nvdimm_bus_list_mutex);
414
415 nd_synchronize();
416 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
417
Dave Jiangaa9ad442017-08-23 12:48:26 -0700418 spin_lock(&nvdimm_bus->badrange.lock);
419 free_badrange_list(&nvdimm_bus->badrange.list);
420 spin_unlock(&nvdimm_bus->badrange.lock);
Dan Williams18515942016-07-22 23:46:08 -0700421
422 nvdimm_bus_destroy_ndctl(nvdimm_bus);
423
424 return 0;
425}
426
427static int nd_bus_probe(struct device *dev)
428{
429 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
430 int rc;
431
432 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
433 if (rc)
434 return rc;
435
436 mutex_lock(&nvdimm_bus_list_mutex);
437 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
438 mutex_unlock(&nvdimm_bus_list_mutex);
439
440 /* enable bus provider attributes to look up their local context */
441 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
442
443 return 0;
444}
445
446static struct nd_device_driver nd_bus_driver = {
447 .probe = nd_bus_probe,
448 .remove = nd_bus_remove,
449 .drv = {
450 .name = "nd_bus",
451 .suppress_bind_attrs = true,
452 .bus = &nvdimm_bus_type,
453 .owner = THIS_MODULE,
454 .mod_name = KBUILD_MODNAME,
455 },
456};
457
458static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
459{
460 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
461
462 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
463 return true;
464
465 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
466}
467
Dan Williams4d88a972015-05-31 14:41:48 -0400468static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
469
470void nd_synchronize(void)
471{
472 async_synchronize_full_domain(&nd_async_domain);
473}
474EXPORT_SYMBOL_GPL(nd_synchronize);
475
476static void nd_async_device_register(void *d, async_cookie_t cookie)
477{
478 struct device *dev = d;
479
480 if (device_add(dev) != 0) {
481 dev_err(dev, "%s: failed\n", __func__);
482 put_device(dev);
483 }
484 put_device(dev);
Alexander Duyckb6eae0f2018-09-25 13:53:02 -0700485 if (dev->parent)
486 put_device(dev->parent);
Dan Williams4d88a972015-05-31 14:41:48 -0400487}
488
489static void nd_async_device_unregister(void *d, async_cookie_t cookie)
490{
491 struct device *dev = d;
492
Dan Williams0ba1c632015-05-30 12:35:36 -0400493 /* flush bus operations before delete */
494 nvdimm_bus_lock(dev);
495 nvdimm_bus_unlock(dev);
496
Dan Williams4d88a972015-05-31 14:41:48 -0400497 device_unregister(dev);
498 put_device(dev);
499}
500
Dan Williams8c2f7e82015-06-25 04:20:04 -0400501void __nd_device_register(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400502{
Dan Williamscd034122016-03-11 10:15:36 -0800503 if (!dev)
504 return;
Alexander Duyck1a091d12018-09-25 13:53:07 -0700505
506 /*
507 * Ensure that region devices always have their NUMA node set as
508 * early as possible. This way we are able to make certain that
509 * any memory associated with the creation and the creation
510 * itself of the region is associated with the correct node.
511 */
512 if (is_nd_region(dev))
513 set_dev_node(dev, to_nd_region(dev)->numa_node);
514
Dan Williams4d88a972015-05-31 14:41:48 -0400515 dev->bus = &nvdimm_bus_type;
Alexander Duyckb6eae0f2018-09-25 13:53:02 -0700516 if (dev->parent)
517 get_device(dev->parent);
Dan Williams4d88a972015-05-31 14:41:48 -0400518 get_device(dev);
519 async_schedule_domain(nd_async_device_register, dev,
520 &nd_async_domain);
521}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400522
523void nd_device_register(struct device *dev)
524{
525 device_initialize(dev);
526 __nd_device_register(dev);
527}
Dan Williams4d88a972015-05-31 14:41:48 -0400528EXPORT_SYMBOL(nd_device_register);
529
530void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
531{
532 switch (mode) {
533 case ND_ASYNC:
534 get_device(dev);
535 async_schedule_domain(nd_async_device_unregister, dev,
536 &nd_async_domain);
537 break;
538 case ND_SYNC:
539 nd_synchronize();
540 device_unregister(dev);
541 break;
542 }
543}
544EXPORT_SYMBOL(nd_device_unregister);
545
546/**
547 * __nd_driver_register() - register a region or a namespace driver
548 * @nd_drv: driver to register
549 * @owner: automatically set by nd_driver_register() macro
550 * @mod_name: automatically set by nd_driver_register() macro
551 */
552int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
553 const char *mod_name)
554{
555 struct device_driver *drv = &nd_drv->drv;
556
557 if (!nd_drv->type) {
558 pr_debug("driver type bitmask not set (%pf)\n",
559 __builtin_return_address(0));
560 return -EINVAL;
561 }
562
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700563 if (!nd_drv->probe) {
564 pr_debug("%s ->probe() must be specified\n", mod_name);
Dan Williams4d88a972015-05-31 14:41:48 -0400565 return -EINVAL;
566 }
567
568 drv->bus = &nvdimm_bus_type;
569 drv->owner = owner;
570 drv->mod_name = mod_name;
571
572 return driver_register(drv);
573}
574EXPORT_SYMBOL(__nd_driver_register);
575
Dan Williams58138822015-06-23 20:08:34 -0400576int nvdimm_revalidate_disk(struct gendisk *disk)
577{
Dan Williams52c44d92016-06-15 19:43:07 -0700578 struct device *dev = disk_to_dev(disk)->parent;
Dan Williams58138822015-06-23 20:08:34 -0400579 struct nd_region *nd_region = to_nd_region(dev->parent);
Robert Elliott254a4cd2018-05-31 18:36:36 -0500580 int disk_ro = get_disk_ro(disk);
Dan Williams58138822015-06-23 20:08:34 -0400581
Robert Elliott254a4cd2018-05-31 18:36:36 -0500582 /*
583 * Upgrade to read-only if the region is read-only preserve as
584 * read-only if the disk is already read-only.
585 */
586 if (disk_ro || nd_region->ro == disk_ro)
Dan Williams58138822015-06-23 20:08:34 -0400587 return 0;
588
Robert Elliott254a4cd2018-05-31 18:36:36 -0500589 dev_info(dev, "%s read-only, marking %s read-only\n",
590 dev_name(&nd_region->dev), disk->disk_name);
591 set_disk_ro(disk, 1);
Dan Williams58138822015-06-23 20:08:34 -0400592
593 return 0;
594
595}
596EXPORT_SYMBOL(nvdimm_revalidate_disk);
597
Dan Williams4d88a972015-05-31 14:41:48 -0400598static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
599 char *buf)
600{
601 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
602 to_nd_device_type(dev));
603}
604static DEVICE_ATTR_RO(modalias);
605
606static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
607 char *buf)
608{
609 return sprintf(buf, "%s\n", dev->type->name);
610}
611static DEVICE_ATTR_RO(devtype);
612
613static struct attribute *nd_device_attributes[] = {
614 &dev_attr_modalias.attr,
615 &dev_attr_devtype.attr,
616 NULL,
617};
618
619/**
620 * nd_device_attribute_group - generic attributes for all devices on an nd bus
621 */
622struct attribute_group nd_device_attribute_group = {
623 .attrs = nd_device_attributes,
624};
625EXPORT_SYMBOL_GPL(nd_device_attribute_group);
626
Toshi Kani74ae66c2015-06-19 12:18:34 -0600627static ssize_t numa_node_show(struct device *dev,
628 struct device_attribute *attr, char *buf)
629{
630 return sprintf(buf, "%d\n", dev_to_node(dev));
631}
632static DEVICE_ATTR_RO(numa_node);
633
634static struct attribute *nd_numa_attributes[] = {
635 &dev_attr_numa_node.attr,
636 NULL,
637};
638
639static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
640 int n)
641{
642 if (!IS_ENABLED(CONFIG_NUMA))
643 return 0;
644
645 return a->mode;
646}
647
648/**
649 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
650 */
651struct attribute_group nd_numa_attribute_group = {
652 .attrs = nd_numa_attributes,
653 .is_visible = nd_numa_attr_visible,
654};
655EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
656
Dan Williams45def222015-04-26 19:26:48 -0400657int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
658{
659 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
660 struct device *dev;
661
662 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
663 "ndctl%d", nvdimm_bus->id);
664
Dan Williams42588952016-05-27 13:28:31 -0700665 if (IS_ERR(dev))
Dan Williams45def222015-04-26 19:26:48 -0400666 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
667 nvdimm_bus->id, PTR_ERR(dev));
Dan Williams42588952016-05-27 13:28:31 -0700668 return PTR_ERR_OR_ZERO(dev);
Dan Williams45def222015-04-26 19:26:48 -0400669}
670
671void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
672{
673 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
674}
675
Dan Williams62232e452015-06-08 14:27:06 -0400676static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
677 [ND_CMD_IMPLEMENTED] = { },
678 [ND_CMD_SMART] = {
679 .out_num = 2,
Dan Williams21129112016-04-07 19:58:44 -0700680 .out_sizes = { 4, 128, },
Dan Williams62232e452015-06-08 14:27:06 -0400681 },
682 [ND_CMD_SMART_THRESHOLD] = {
683 .out_num = 2,
684 .out_sizes = { 4, 8, },
685 },
686 [ND_CMD_DIMM_FLAGS] = {
687 .out_num = 2,
688 .out_sizes = { 4, 4 },
689 },
690 [ND_CMD_GET_CONFIG_SIZE] = {
691 .out_num = 3,
692 .out_sizes = { 4, 4, 4, },
693 },
694 [ND_CMD_GET_CONFIG_DATA] = {
695 .in_num = 2,
696 .in_sizes = { 4, 4, },
697 .out_num = 2,
698 .out_sizes = { 4, UINT_MAX, },
699 },
700 [ND_CMD_SET_CONFIG_DATA] = {
701 .in_num = 3,
702 .in_sizes = { 4, 4, UINT_MAX, },
703 .out_num = 1,
704 .out_sizes = { 4, },
705 },
706 [ND_CMD_VENDOR] = {
707 .in_num = 3,
708 .in_sizes = { 4, 4, UINT_MAX, },
709 .out_num = 3,
710 .out_sizes = { 4, 4, UINT_MAX, },
711 },
Dan Williams31eca762016-04-28 16:23:43 -0700712 [ND_CMD_CALL] = {
713 .in_num = 2,
714 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
715 .out_num = 1,
716 .out_sizes = { UINT_MAX, },
717 },
Dan Williams62232e452015-06-08 14:27:06 -0400718};
719
720const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
721{
722 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
723 return &__nd_cmd_dimm_descs[cmd];
724 return NULL;
725}
726EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
727
728static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
729 [ND_CMD_IMPLEMENTED] = { },
730 [ND_CMD_ARS_CAP] = {
731 .in_num = 2,
732 .in_sizes = { 8, 8, },
Dan Williams4577b062016-02-17 13:08:58 -0800733 .out_num = 4,
734 .out_sizes = { 4, 4, 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400735 },
736 [ND_CMD_ARS_START] = {
Dan Williams4577b062016-02-17 13:08:58 -0800737 .in_num = 5,
738 .in_sizes = { 8, 8, 2, 1, 5, },
739 .out_num = 2,
740 .out_sizes = { 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400741 },
742 [ND_CMD_ARS_STATUS] = {
Dan Williams747ffe12016-02-19 15:21:14 -0800743 .out_num = 3,
744 .out_sizes = { 4, 4, UINT_MAX, },
Dan Williams62232e452015-06-08 14:27:06 -0400745 },
Dan Williamsd4f32362016-03-03 16:08:54 -0800746 [ND_CMD_CLEAR_ERROR] = {
747 .in_num = 2,
748 .in_sizes = { 8, 8, },
749 .out_num = 3,
750 .out_sizes = { 4, 4, 8, },
751 },
Dan Williams31eca762016-04-28 16:23:43 -0700752 [ND_CMD_CALL] = {
753 .in_num = 2,
754 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
755 .out_num = 1,
756 .out_sizes = { UINT_MAX, },
757 },
Dan Williams62232e452015-06-08 14:27:06 -0400758};
759
760const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
761{
762 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
763 return &__nd_cmd_bus_descs[cmd];
764 return NULL;
765}
766EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
767
768u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
769 const struct nd_cmd_desc *desc, int idx, void *buf)
770{
771 if (idx >= desc->in_num)
772 return UINT_MAX;
773
774 if (desc->in_sizes[idx] < UINT_MAX)
775 return desc->in_sizes[idx];
776
777 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
778 struct nd_cmd_set_config_hdr *hdr = buf;
779
780 return hdr->in_length;
781 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
782 struct nd_cmd_vendor_hdr *hdr = buf;
783
784 return hdr->in_length;
Dan Williams31eca762016-04-28 16:23:43 -0700785 } else if (cmd == ND_CMD_CALL) {
786 struct nd_cmd_pkg *pkg = buf;
787
788 return pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400789 }
790
791 return UINT_MAX;
792}
793EXPORT_SYMBOL_GPL(nd_cmd_in_size);
794
795u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
796 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800797 const u32 *out_field, unsigned long remainder)
Dan Williams62232e452015-06-08 14:27:06 -0400798{
799 if (idx >= desc->out_num)
800 return UINT_MAX;
801
802 if (desc->out_sizes[idx] < UINT_MAX)
803 return desc->out_sizes[idx];
804
805 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
806 return in_field[1];
807 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
808 return out_field[1];
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800809 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
810 /*
811 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
812 * "Size of Output Buffer in bytes, including this
813 * field."
814 */
815 if (out_field[1] < 4)
816 return 0;
817 /*
818 * ACPI 6.1 is ambiguous if 'status' is included in the
819 * output size. If we encounter an output size that
820 * overshoots the remainder by 4 bytes, assume it was
821 * including 'status'.
822 */
Vishal Verma286e8772018-08-10 13:23:15 -0600823 if (out_field[1] - 4 == remainder)
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800824 return remainder;
Vishal Verma286e8772018-08-10 13:23:15 -0600825 return out_field[1] - 8;
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800826 } else if (cmd == ND_CMD_CALL) {
Dan Williams31eca762016-04-28 16:23:43 -0700827 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
828
829 return pkg->nd_size_out;
830 }
831
Dan Williams62232e452015-06-08 14:27:06 -0400832
833 return UINT_MAX;
834}
835EXPORT_SYMBOL_GPL(nd_cmd_out_size);
836
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400837void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400838{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400839 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
840
Dan Williamseaf96152015-05-01 13:11:27 -0400841 do {
842 if (nvdimm_bus->probe_active == 0)
843 break;
844 nvdimm_bus_unlock(&nvdimm_bus->dev);
845 wait_event(nvdimm_bus->probe_wait,
846 nvdimm_bus->probe_active == 0);
847 nvdimm_bus_lock(&nvdimm_bus->dev);
848 } while (true);
849}
850
Dave Jiang006358b2017-04-07 15:33:31 -0700851static int nd_pmem_forget_poison_check(struct device *dev, void *data)
Dan Williamsd4f32362016-03-03 16:08:54 -0800852{
Dave Jiang006358b2017-04-07 15:33:31 -0700853 struct nd_cmd_clear_error *clear_err =
854 (struct nd_cmd_clear_error *)data;
855 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
856 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
857 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
858 struct nd_namespace_common *ndns = NULL;
859 struct nd_namespace_io *nsio;
860 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
861
862 if (nd_dax || !dev->driver)
863 return 0;
864
865 start = clear_err->address;
866 end = clear_err->address + clear_err->cleared - 1;
867
868 if (nd_btt || nd_pfn || nd_dax) {
869 if (nd_btt)
870 ndns = nd_btt->ndns;
871 else if (nd_pfn)
872 ndns = nd_pfn->ndns;
873 else if (nd_dax)
874 ndns = nd_dax->nd_pfn.ndns;
875
876 if (!ndns)
877 return 0;
878 } else
879 ndns = to_ndns(dev);
880
881 nsio = to_nd_namespace_io(&ndns->dev);
882 pstart = nsio->res.start + offset;
883 pend = nsio->res.end - end_trunc;
884
885 if ((pstart >= start) && (pend <= end))
Dan Williamsd4f32362016-03-03 16:08:54 -0800886 return -EBUSY;
Dave Jiang006358b2017-04-07 15:33:31 -0700887
Dan Williamsd4f32362016-03-03 16:08:54 -0800888 return 0;
Dave Jiang006358b2017-04-07 15:33:31 -0700889
890}
891
892static int nd_ns_forget_poison_check(struct device *dev, void *data)
893{
894 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800895}
896
Dan Williamseaf96152015-05-01 13:11:27 -0400897/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800898static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
Dave Jiang006358b2017-04-07 15:33:31 -0700899 struct nvdimm *nvdimm, unsigned int cmd, void *data)
Dan Williamseaf96152015-05-01 13:11:27 -0400900{
Dan Williams87bf5722016-02-22 21:50:31 -0800901 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
902
903 /* ask the bus provider if it would like to block this request */
904 if (nd_desc->clear_to_send) {
905 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
906
907 if (rc)
908 return rc;
909 }
Dan Williamseaf96152015-05-01 13:11:27 -0400910
Dan Williamsd4f32362016-03-03 16:08:54 -0800911 /* require clear error to go through the pmem driver */
912 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
Dave Jiang006358b2017-04-07 15:33:31 -0700913 return device_for_each_child(&nvdimm_bus->dev, data,
914 nd_ns_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800915
Dan Williamseaf96152015-05-01 13:11:27 -0400916 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
917 return 0;
918
Dan Williams87bf5722016-02-22 21:50:31 -0800919 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400920 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400921 if (atomic_read(&nvdimm->busy))
922 return -EBUSY;
923 return 0;
924}
925
Dan Williams62232e452015-06-08 14:27:06 -0400926static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
927 int read_only, unsigned int ioctl_cmd, unsigned long arg)
928{
929 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
Dan Williams62232e452015-06-08 14:27:06 -0400930 static char out_env[ND_CMD_MAX_ENVELOPE];
931 static char in_env[ND_CMD_MAX_ENVELOPE];
932 const struct nd_cmd_desc *desc = NULL;
933 unsigned int cmd = _IOC_NR(ioctl_cmd);
Dan Williams62232e452015-06-08 14:27:06 -0400934 struct device *dev = &nvdimm_bus->dev;
Dan Williams58738c42017-08-31 15:41:55 -0700935 void __user *p = (void __user *) arg;
Dan Williams62232e452015-06-08 14:27:06 -0400936 const char *cmd_name, *dimm_name;
Dan Williams58738c42017-08-31 15:41:55 -0700937 u32 in_len = 0, out_len = 0;
938 unsigned int func = cmd;
Dan Williamse3654ec2016-04-28 16:17:07 -0700939 unsigned long cmd_mask;
Dan Williams58738c42017-08-31 15:41:55 -0700940 struct nd_cmd_pkg pkg;
Dave Jiang006358b2017-04-07 15:33:31 -0700941 int rc, i, cmd_rc;
Dan Williams58738c42017-08-31 15:41:55 -0700942 u64 buf_len = 0;
943 void *buf;
Dan Williams62232e452015-06-08 14:27:06 -0400944
945 if (nvdimm) {
946 desc = nd_cmd_dimm_desc(cmd);
947 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700948 cmd_mask = nvdimm->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400949 dimm_name = dev_name(&nvdimm->dev);
950 } else {
951 desc = nd_cmd_bus_desc(cmd);
952 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700953 cmd_mask = nd_desc->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400954 dimm_name = "bus";
955 }
956
Dan Williams31eca762016-04-28 16:23:43 -0700957 if (cmd == ND_CMD_CALL) {
958 if (copy_from_user(&pkg, p, sizeof(pkg)))
959 return -EFAULT;
960 }
961
Dan Williams62232e452015-06-08 14:27:06 -0400962 if (!desc || (desc->out_num + desc->in_num == 0) ||
Dan Williamse3654ec2016-04-28 16:17:07 -0700963 !test_bit(cmd, &cmd_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400964 return -ENOTTY;
965
966 /* fail write commands (when read-only) */
967 if (read_only)
Jerry Hoemann07accfa2016-01-06 16:03:41 -0700968 switch (cmd) {
969 case ND_CMD_VENDOR:
970 case ND_CMD_SET_CONFIG_DATA:
971 case ND_CMD_ARS_START:
Dan Williamsd4f32362016-03-03 16:08:54 -0800972 case ND_CMD_CLEAR_ERROR:
Dan Williams31eca762016-04-28 16:23:43 -0700973 case ND_CMD_CALL:
Dan Williams62232e452015-06-08 14:27:06 -0400974 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
975 nvdimm ? nvdimm_cmd_name(cmd)
976 : nvdimm_bus_cmd_name(cmd));
977 return -EPERM;
978 default:
979 break;
980 }
981
982 /* process an input envelope */
983 for (i = 0; i < desc->in_num; i++) {
984 u32 in_size, copy;
985
986 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
987 if (in_size == UINT_MAX) {
988 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
989 __func__, dimm_name, cmd_name, i);
990 return -ENXIO;
991 }
Dan Williams62232e452015-06-08 14:27:06 -0400992 if (in_len < sizeof(in_env))
993 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
994 else
995 copy = 0;
996 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
997 return -EFAULT;
998 in_len += in_size;
999 }
1000
Dan Williams31eca762016-04-28 16:23:43 -07001001 if (cmd == ND_CMD_CALL) {
Jerry Hoemann53b85a42017-06-30 20:41:22 -07001002 func = pkg.nd_command;
Dan Williams426824d2018-03-05 16:39:31 -08001003 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1004 dimm_name, pkg.nd_command,
Dan Williams31eca762016-04-28 16:23:43 -07001005 in_len, out_len, buf_len);
Dan Williams31eca762016-04-28 16:23:43 -07001006 }
1007
Dan Williams62232e452015-06-08 14:27:06 -04001008 /* process an output envelope */
1009 for (i = 0; i < desc->out_num; i++) {
1010 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
Dan Williamsefda1b5d2016-12-06 09:10:12 -08001011 (u32 *) in_env, (u32 *) out_env, 0);
Dan Williams62232e452015-06-08 14:27:06 -04001012 u32 copy;
1013
1014 if (out_size == UINT_MAX) {
Dan Williams426824d2018-03-05 16:39:31 -08001015 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1016 dimm_name, cmd_name, i);
Dan Williams62232e452015-06-08 14:27:06 -04001017 return -EFAULT;
1018 }
Dan Williams62232e452015-06-08 14:27:06 -04001019 if (out_len < sizeof(out_env))
1020 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1021 else
1022 copy = 0;
1023 if (copy && copy_from_user(&out_env[out_len],
1024 p + in_len + out_len, copy))
1025 return -EFAULT;
1026 out_len += out_size;
1027 }
1028
Dan Williams58738c42017-08-31 15:41:55 -07001029 buf_len = (u64) out_len + (u64) in_len;
Dan Williams62232e452015-06-08 14:27:06 -04001030 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
Dan Williams426824d2018-03-05 16:39:31 -08001031 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1032 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
Dan Williams62232e452015-06-08 14:27:06 -04001033 return -EINVAL;
1034 }
1035
1036 buf = vmalloc(buf_len);
1037 if (!buf)
1038 return -ENOMEM;
1039
1040 if (copy_from_user(buf, p, buf_len)) {
1041 rc = -EFAULT;
1042 goto out;
1043 }
1044
Dan Williamseaf96152015-05-01 13:11:27 -04001045 nvdimm_bus_lock(&nvdimm_bus->dev);
Jerry Hoemann53b85a42017-06-30 20:41:22 -07001046 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
Dan Williamseaf96152015-05-01 13:11:27 -04001047 if (rc)
1048 goto out_unlock;
1049
Dave Jiang006358b2017-04-07 15:33:31 -07001050 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
Dan Williams62232e452015-06-08 14:27:06 -04001051 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -04001052 goto out_unlock;
Dave Jiang006358b2017-04-07 15:33:31 -07001053
1054 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1055 struct nd_cmd_clear_error *clear_err = buf;
Dave Jiang006358b2017-04-07 15:33:31 -07001056
Dan Williams23f49842017-04-29 15:24:03 -07001057 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1058 clear_err->cleared);
Dave Jiang006358b2017-04-07 15:33:31 -07001059 }
Dan Williams0beb2012017-04-07 09:47:24 -07001060 nvdimm_bus_unlock(&nvdimm_bus->dev);
1061
Dan Williams62232e452015-06-08 14:27:06 -04001062 if (copy_to_user(p, buf, buf_len))
1063 rc = -EFAULT;
Dan Williams0beb2012017-04-07 09:47:24 -07001064
1065 vfree(buf);
1066 return rc;
1067
Dan Williamseaf96152015-05-01 13:11:27 -04001068 out_unlock:
1069 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -04001070 out:
1071 vfree(buf);
1072 return rc;
1073}
1074
Dan Williams45def222015-04-26 19:26:48 -04001075static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1076{
Dan Williams62232e452015-06-08 14:27:06 -04001077 long id = (long) file->private_data;
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001078 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001079 struct nvdimm_bus *nvdimm_bus;
1080
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001081 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001082 mutex_lock(&nvdimm_bus_list_mutex);
1083 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1084 if (nvdimm_bus->id == id) {
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001085 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001086 break;
1087 }
1088 }
1089 mutex_unlock(&nvdimm_bus_list_mutex);
1090
1091 return rc;
1092}
1093
1094static int match_dimm(struct device *dev, void *data)
1095{
1096 long id = (long) data;
1097
1098 if (is_nvdimm(dev)) {
1099 struct nvdimm *nvdimm = to_nvdimm(dev);
1100
1101 return nvdimm->id == id;
1102 }
1103
1104 return 0;
1105}
1106
1107static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1108{
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001109 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001110 struct nvdimm_bus *nvdimm_bus;
1111
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001112 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001113 mutex_lock(&nvdimm_bus_list_mutex);
1114 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1115 struct device *dev = device_find_child(&nvdimm_bus->dev,
1116 file->private_data, match_dimm);
1117 struct nvdimm *nvdimm;
1118
1119 if (!dev)
1120 continue;
1121
1122 nvdimm = to_nvdimm(dev);
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001123 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001124 put_device(dev);
1125 break;
1126 }
1127 mutex_unlock(&nvdimm_bus_list_mutex);
1128
1129 return rc;
1130}
1131
1132static int nd_open(struct inode *inode, struct file *file)
1133{
1134 long minor = iminor(inode);
1135
1136 file->private_data = (void *) minor;
1137 return 0;
Dan Williams45def222015-04-26 19:26:48 -04001138}
1139
1140static const struct file_operations nvdimm_bus_fops = {
1141 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -04001142 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -04001143 .unlocked_ioctl = nd_ioctl,
1144 .compat_ioctl = nd_ioctl,
1145 .llseek = noop_llseek,
1146};
1147
Dan Williams62232e452015-06-08 14:27:06 -04001148static const struct file_operations nvdimm_fops = {
1149 .owner = THIS_MODULE,
1150 .open = nd_open,
1151 .unlocked_ioctl = nvdimm_ioctl,
1152 .compat_ioctl = nvdimm_ioctl,
1153 .llseek = noop_llseek,
1154};
1155
Dan Williams45def222015-04-26 19:26:48 -04001156int __init nvdimm_bus_init(void)
1157{
1158 int rc;
1159
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001160 rc = bus_register(&nvdimm_bus_type);
1161 if (rc)
1162 return rc;
1163
Dan Williams45def222015-04-26 19:26:48 -04001164 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1165 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -04001166 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -04001167 nvdimm_bus_major = rc;
1168
Dan Williams62232e452015-06-08 14:27:06 -04001169 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1170 if (rc < 0)
1171 goto err_dimm_chrdev;
1172 nvdimm_major = rc;
1173
Dan Williams45def222015-04-26 19:26:48 -04001174 nd_class = class_create(THIS_MODULE, "nd");
Axel Lindaa1dee2015-06-28 17:00:57 +08001175 if (IS_ERR(nd_class)) {
1176 rc = PTR_ERR(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001177 goto err_class;
Axel Lindaa1dee2015-06-28 17:00:57 +08001178 }
Dan Williams45def222015-04-26 19:26:48 -04001179
Dan Williams18515942016-07-22 23:46:08 -07001180 rc = driver_register(&nd_bus_driver.drv);
1181 if (rc)
1182 goto err_nd_bus;
1183
Dan Williams45def222015-04-26 19:26:48 -04001184 return 0;
1185
Dan Williams18515942016-07-22 23:46:08 -07001186 err_nd_bus:
1187 class_destroy(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001188 err_class:
Dan Williams62232e452015-06-08 14:27:06 -04001189 unregister_chrdev(nvdimm_major, "dimmctl");
1190 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -04001191 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001192 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001193 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -04001194
1195 return rc;
1196}
1197
Dan Williams4d88a972015-05-31 14:41:48 -04001198void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -04001199{
Dan Williams18515942016-07-22 23:46:08 -07001200 driver_unregister(&nd_bus_driver.drv);
Dan Williams45def222015-04-26 19:26:48 -04001201 class_destroy(nd_class);
1202 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001203 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001204 bus_unregister(&nvdimm_bus_type);
Dan Williams18515942016-07-22 23:46:08 -07001205 ida_destroy(&nd_ida);
Dan Williams45def222015-04-26 19:26:48 -04001206}