blob: 2eb6a6cfe9e4cf61c014a1b96c17626120dbb84d [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>
Alexander Duyckaf87b9a2019-01-22 10:39:47 -080026#include <linux/cpu.h>
Dan Williams45def222015-04-26 19:26:48 -040027#include <linux/fs.h>
28#include <linux/io.h>
Dan Williams62232e452015-06-08 14:27:06 -040029#include <linux/mm.h>
Dan Williams4d88a972015-05-31 14:41:48 -040030#include <linux/nd.h>
Dan Williams45def222015-04-26 19:26:48 -040031#include "nd-core.h"
Dan Williams4d88a972015-05-31 14:41:48 -040032#include "nd.h"
Dave Jiang006358b2017-04-07 15:33:31 -070033#include "pfn.h"
Dan Williams45def222015-04-26 19:26:48 -040034
Dan Williams62232e452015-06-08 14:27:06 -040035int nvdimm_major;
Dan Williams45def222015-04-26 19:26:48 -040036static int nvdimm_bus_major;
37static struct class *nd_class;
Dan Williams18515942016-07-22 23:46:08 -070038static DEFINE_IDA(nd_ida);
Dan Williams45def222015-04-26 19:26:48 -040039
Dan Williams4d88a972015-05-31 14:41:48 -040040static int to_nd_device_type(struct device *dev)
41{
42 if (is_nvdimm(dev))
43 return ND_DEVICE_DIMM;
Dan Williamsc9e582a2017-05-29 23:12:19 -070044 else if (is_memory(dev))
Dan Williams3d880022015-05-31 15:02:11 -040045 return ND_DEVICE_REGION_PMEM;
46 else if (is_nd_blk(dev))
47 return ND_DEVICE_REGION_BLK;
Dan Williamscd034122016-03-11 10:15:36 -080048 else if (is_nd_dax(dev))
49 return ND_DEVICE_DAX_PMEM;
Dan Williamsc9e582a2017-05-29 23:12:19 -070050 else if (is_nd_region(dev->parent))
Dan Williams3d880022015-05-31 15:02:11 -040051 return nd_region_to_nstype(to_nd_region(dev->parent));
Dan Williams4d88a972015-05-31 14:41:48 -040052
53 return 0;
54}
55
56static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
57{
58 return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
59 to_nd_device_type(dev));
60}
61
Dan Williams3d880022015-05-31 15:02:11 -040062static struct module *to_bus_provider(struct device *dev)
63{
64 /* pin bus providers while regions are enabled */
Dan Williamsc9e582a2017-05-29 23:12:19 -070065 if (is_nd_region(dev)) {
Dan Williams3d880022015-05-31 15:02:11 -040066 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
67
Dan Williamsbc9775d2016-07-21 20:03:19 -070068 return nvdimm_bus->nd_desc->module;
Dan Williams3d880022015-05-31 15:02:11 -040069 }
70 return NULL;
71}
72
Dan Williamseaf96152015-05-01 13:11:27 -040073static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
74{
75 nvdimm_bus_lock(&nvdimm_bus->dev);
76 nvdimm_bus->probe_active++;
77 nvdimm_bus_unlock(&nvdimm_bus->dev);
78}
79
80static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
81{
82 nvdimm_bus_lock(&nvdimm_bus->dev);
83 if (--nvdimm_bus->probe_active == 0)
84 wake_up(&nvdimm_bus->probe_wait);
85 nvdimm_bus_unlock(&nvdimm_bus->dev);
86}
87
Dan Williams4d88a972015-05-31 14:41:48 -040088static int nvdimm_bus_probe(struct device *dev)
89{
90 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -040091 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040092 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
93 int rc;
94
Dan Williams3d880022015-05-31 15:02:11 -040095 if (!try_module_get(provider))
96 return -ENXIO;
97
Dan Williams3f468332018-06-01 14:10:58 -070098 dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
99 dev->driver->name, dev_name(dev));
100
Dan Williamseaf96152015-05-01 13:11:27 -0400101 nvdimm_bus_probe_start(nvdimm_bus);
Dan Williams4d88a972015-05-31 14:41:48 -0400102 rc = nd_drv->probe(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400103 if (rc == 0)
104 nd_region_probe_success(nvdimm_bus, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400105 else
106 nd_region_disable(nvdimm_bus, dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400107 nvdimm_bus_probe_end(nvdimm_bus);
108
Dan Williams3f468332018-06-01 14:10:58 -0700109 dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
Dan Williams4d88a972015-05-31 14:41:48 -0400110 dev_name(dev), rc);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400111
Dan Williams3d880022015-05-31 15:02:11 -0400112 if (rc != 0)
113 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400114 return rc;
115}
116
117static int nvdimm_bus_remove(struct device *dev)
118{
119 struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
Dan Williams3d880022015-05-31 15:02:11 -0400120 struct module *provider = to_bus_provider(dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400121 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700122 int rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -0400123
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700124 if (nd_drv->remove)
125 rc = nd_drv->remove(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400126 nd_region_disable(nvdimm_bus, dev);
127
Dan Williams4d88a972015-05-31 14:41:48 -0400128 dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name,
129 dev_name(dev), rc);
Dan Williams3d880022015-05-31 15:02:11 -0400130 module_put(provider);
Dan Williams4d88a972015-05-31 14:41:48 -0400131 return rc;
132}
133
Dan Williams476f8482016-07-09 00:12:52 -0700134static void nvdimm_bus_shutdown(struct device *dev)
135{
136 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
137 struct nd_device_driver *nd_drv = NULL;
138
139 if (dev->driver)
140 nd_drv = to_nd_device_driver(dev->driver);
141
142 if (nd_drv && nd_drv->shutdown) {
143 nd_drv->shutdown(dev);
144 dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
145 dev->driver->name, dev_name(dev));
146 }
147}
148
Dan Williams71999462016-02-18 10:29:49 -0800149void nd_device_notify(struct device *dev, enum nvdimm_event event)
150{
151 device_lock(dev);
152 if (dev->driver) {
153 struct nd_device_driver *nd_drv;
154
155 nd_drv = to_nd_device_driver(dev->driver);
156 if (nd_drv->notify)
157 nd_drv->notify(dev, event);
158 }
159 device_unlock(dev);
160}
161EXPORT_SYMBOL(nd_device_notify);
162
163void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
164{
165 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
166
167 if (!nvdimm_bus)
168 return;
169
170 /* caller is responsible for holding a reference on the device */
171 nd_device_notify(&nd_region->dev, event);
172}
173EXPORT_SYMBOL_GPL(nvdimm_region_notify);
174
Dan Williams23f49842017-04-29 15:24:03 -0700175struct clear_badblocks_context {
176 resource_size_t phys, cleared;
177};
178
179static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
180{
181 struct clear_badblocks_context *ctx = data;
182 struct nd_region *nd_region;
183 resource_size_t ndr_end;
184 sector_t sector;
185
186 /* make sure device is a region */
187 if (!is_nd_pmem(dev))
188 return 0;
189
190 nd_region = to_nd_region(dev);
191 ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
192
193 /* make sure we are in the region */
194 if (ctx->phys < nd_region->ndr_start
195 || (ctx->phys + ctx->cleared) > ndr_end)
196 return 0;
197
198 sector = (ctx->phys - nd_region->ndr_start) / 512;
199 badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
200
Toshi Kani975750a2017-06-12 16:25:11 -0600201 if (nd_region->bb_state)
202 sysfs_notify_dirent(nd_region->bb_state);
203
Dan Williams23f49842017-04-29 15:24:03 -0700204 return 0;
205}
206
207static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
208 phys_addr_t phys, u64 cleared)
209{
210 struct clear_badblocks_context ctx = {
211 .phys = phys,
212 .cleared = cleared,
213 };
214
215 device_for_each_child(&nvdimm_bus->dev, &ctx,
216 nvdimm_clear_badblocks_region);
217}
218
219static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
220 phys_addr_t phys, u64 cleared)
221{
222 if (cleared > 0)
Dave Jiangaa9ad442017-08-23 12:48:26 -0700223 badrange_forget(&nvdimm_bus->badrange, phys, cleared);
Dan Williams23f49842017-04-29 15:24:03 -0700224
225 if (cleared > 0 && cleared / 512)
226 nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
227}
228
Dan Williams59e64732016-03-08 07:16:07 -0800229long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
230 unsigned int len)
231{
232 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
233 struct nvdimm_bus_descriptor *nd_desc;
234 struct nd_cmd_clear_error clear_err;
235 struct nd_cmd_ars_cap ars_cap;
236 u32 clear_err_unit, mask;
Vishal Verma0930a752017-08-30 19:36:02 -0600237 unsigned int noio_flag;
Dan Williams59e64732016-03-08 07:16:07 -0800238 int cmd_rc, rc;
239
240 if (!nvdimm_bus)
241 return -ENXIO;
242
243 nd_desc = nvdimm_bus->nd_desc;
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700244 /*
245 * if ndctl does not exist, it's PMEM_LEGACY and
246 * we want to just pretend everything is handled.
247 */
Dan Williams59e64732016-03-08 07:16:07 -0800248 if (!nd_desc->ndctl)
Dave Jiang1e8b8d92016-09-09 09:10:08 -0700249 return len;
Dan Williams59e64732016-03-08 07:16:07 -0800250
251 memset(&ars_cap, 0, sizeof(ars_cap));
252 ars_cap.address = phys;
253 ars_cap.length = len;
Vishal Verma0930a752017-08-30 19:36:02 -0600254 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800255 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
256 sizeof(ars_cap), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600257 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800258 if (rc < 0)
259 return rc;
260 if (cmd_rc < 0)
261 return cmd_rc;
262 clear_err_unit = ars_cap.clear_err_unit;
263 if (!clear_err_unit || !is_power_of_2(clear_err_unit))
264 return -ENXIO;
265
266 mask = clear_err_unit - 1;
267 if ((phys | len) & mask)
268 return -ENXIO;
269 memset(&clear_err, 0, sizeof(clear_err));
270 clear_err.address = phys;
271 clear_err.length = len;
Vishal Verma0930a752017-08-30 19:36:02 -0600272 noio_flag = memalloc_noio_save();
Dan Williams59e64732016-03-08 07:16:07 -0800273 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
274 sizeof(clear_err), &cmd_rc);
Vishal Verma0930a752017-08-30 19:36:02 -0600275 memalloc_noio_restore(noio_flag);
Dan Williams59e64732016-03-08 07:16:07 -0800276 if (rc < 0)
277 return rc;
278 if (cmd_rc < 0)
279 return cmd_rc;
Vishal Vermae0461142016-09-30 17:19:31 -0600280
Dan Williams23f49842017-04-29 15:24:03 -0700281 nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
Toshi Kani8d13c022017-04-27 16:57:05 -0600282
Dan Williams59e64732016-03-08 07:16:07 -0800283 return clear_err.cleared;
284}
285EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
286
Dan Williams18515942016-07-22 23:46:08 -0700287static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
288
Dan Williams4d88a972015-05-31 14:41:48 -0400289static struct bus_type nvdimm_bus_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400290 .name = "nd",
Dan Williams4d88a972015-05-31 14:41:48 -0400291 .uevent = nvdimm_bus_uevent,
292 .match = nvdimm_bus_match,
293 .probe = nvdimm_bus_probe,
294 .remove = nvdimm_bus_remove,
Dan Williams476f8482016-07-09 00:12:52 -0700295 .shutdown = nvdimm_bus_shutdown,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400296};
297
Dan Williams18515942016-07-22 23:46:08 -0700298static void nvdimm_bus_release(struct device *dev)
299{
300 struct nvdimm_bus *nvdimm_bus;
301
302 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
303 ida_simple_remove(&nd_ida, nvdimm_bus->id);
304 kfree(nvdimm_bus);
305}
306
307static bool is_nvdimm_bus(struct device *dev)
308{
309 return dev->release == nvdimm_bus_release;
310}
311
312struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
313{
314 struct device *dev;
315
316 for (dev = nd_dev; dev; dev = dev->parent)
317 if (is_nvdimm_bus(dev))
318 break;
319 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
320 if (dev)
321 return to_nvdimm_bus(dev);
322 return NULL;
323}
324
325struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
326{
327 struct nvdimm_bus *nvdimm_bus;
328
329 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
330 WARN_ON(!is_nvdimm_bus(dev));
331 return nvdimm_bus;
332}
333EXPORT_SYMBOL_GPL(to_nvdimm_bus);
334
Dave Jiangf2989392018-12-05 23:39:29 -0800335struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm)
336{
337 return to_nvdimm_bus(nvdimm->dev.parent);
338}
339EXPORT_SYMBOL_GPL(nvdimm_to_bus);
340
Dan Williams18515942016-07-22 23:46:08 -0700341struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
342 struct nvdimm_bus_descriptor *nd_desc)
343{
344 struct nvdimm_bus *nvdimm_bus;
345 int rc;
346
347 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
348 if (!nvdimm_bus)
349 return NULL;
350 INIT_LIST_HEAD(&nvdimm_bus->list);
351 INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
Dan Williams18515942016-07-22 23:46:08 -0700352 init_waitqueue_head(&nvdimm_bus->probe_wait);
353 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
Dan Williams18515942016-07-22 23:46:08 -0700354 if (nvdimm_bus->id < 0) {
355 kfree(nvdimm_bus);
356 return NULL;
357 }
Ocean He9bf3aa42018-08-03 08:08:33 -0400358 mutex_init(&nvdimm_bus->reconfig_mutex);
359 badrange_init(&nvdimm_bus->badrange);
Dan Williams18515942016-07-22 23:46:08 -0700360 nvdimm_bus->nd_desc = nd_desc;
361 nvdimm_bus->dev.parent = parent;
362 nvdimm_bus->dev.release = nvdimm_bus_release;
363 nvdimm_bus->dev.groups = nd_desc->attr_groups;
364 nvdimm_bus->dev.bus = &nvdimm_bus_type;
Oliver O'Halloran1ff19f42018-04-06 15:21:13 +1000365 nvdimm_bus->dev.of_node = nd_desc->of_node;
Dan Williams18515942016-07-22 23:46:08 -0700366 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
367 rc = device_register(&nvdimm_bus->dev);
368 if (rc) {
369 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
370 goto err;
371 }
372
373 return nvdimm_bus;
374 err:
375 put_device(&nvdimm_bus->dev);
376 return NULL;
377}
378EXPORT_SYMBOL_GPL(nvdimm_bus_register);
379
380void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
381{
382 if (!nvdimm_bus)
383 return;
384 device_unregister(&nvdimm_bus->dev);
385}
386EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
387
388static int child_unregister(struct device *dev, void *data)
389{
390 /*
391 * the singular ndctl class device per bus needs to be
392 * "device_destroy"ed, so skip it here
393 *
394 * i.e. remove classless children
395 */
396 if (dev->class)
Dave Jiang7d988092018-12-13 15:36:18 -0700397 return 0;
398
399 if (is_nvdimm(dev)) {
400 struct nvdimm *nvdimm = to_nvdimm(dev);
401 bool dev_put = false;
402
403 /* We are shutting down. Make state frozen artificially. */
404 nvdimm_bus_lock(dev);
405 nvdimm->sec.state = NVDIMM_SECURITY_FROZEN;
406 if (test_and_clear_bit(NDD_WORK_PENDING, &nvdimm->flags))
407 dev_put = true;
408 nvdimm_bus_unlock(dev);
409 cancel_delayed_work_sync(&nvdimm->dwork);
410 if (dev_put)
411 put_device(dev);
412 }
413 nd_device_unregister(dev, ND_SYNC);
414
Dan Williams18515942016-07-22 23:46:08 -0700415 return 0;
416}
417
Dave Jiangaa9ad442017-08-23 12:48:26 -0700418static void free_badrange_list(struct list_head *badrange_list)
Dan Williams18515942016-07-22 23:46:08 -0700419{
Dave Jiangaa9ad442017-08-23 12:48:26 -0700420 struct badrange_entry *bre, *next;
Dan Williams18515942016-07-22 23:46:08 -0700421
Dave Jiangaa9ad442017-08-23 12:48:26 -0700422 list_for_each_entry_safe(bre, next, badrange_list, list) {
423 list_del(&bre->list);
424 kfree(bre);
Dan Williams18515942016-07-22 23:46:08 -0700425 }
Dave Jiangaa9ad442017-08-23 12:48:26 -0700426 list_del_init(badrange_list);
Dan Williams18515942016-07-22 23:46:08 -0700427}
428
429static int nd_bus_remove(struct device *dev)
430{
431 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
432
433 mutex_lock(&nvdimm_bus_list_mutex);
434 list_del_init(&nvdimm_bus->list);
435 mutex_unlock(&nvdimm_bus_list_mutex);
436
437 nd_synchronize();
438 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
439
Dave Jiangaa9ad442017-08-23 12:48:26 -0700440 spin_lock(&nvdimm_bus->badrange.lock);
441 free_badrange_list(&nvdimm_bus->badrange.list);
442 spin_unlock(&nvdimm_bus->badrange.lock);
Dan Williams18515942016-07-22 23:46:08 -0700443
444 nvdimm_bus_destroy_ndctl(nvdimm_bus);
445
446 return 0;
447}
448
449static int nd_bus_probe(struct device *dev)
450{
451 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
452 int rc;
453
454 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
455 if (rc)
456 return rc;
457
458 mutex_lock(&nvdimm_bus_list_mutex);
459 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
460 mutex_unlock(&nvdimm_bus_list_mutex);
461
462 /* enable bus provider attributes to look up their local context */
463 dev_set_drvdata(dev, nvdimm_bus->nd_desc);
464
465 return 0;
466}
467
468static struct nd_device_driver nd_bus_driver = {
469 .probe = nd_bus_probe,
470 .remove = nd_bus_remove,
471 .drv = {
472 .name = "nd_bus",
473 .suppress_bind_attrs = true,
474 .bus = &nvdimm_bus_type,
475 .owner = THIS_MODULE,
476 .mod_name = KBUILD_MODNAME,
477 },
478};
479
480static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
481{
482 struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
483
484 if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
485 return true;
486
487 return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
488}
489
Dan Williams4d88a972015-05-31 14:41:48 -0400490static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
491
492void nd_synchronize(void)
493{
494 async_synchronize_full_domain(&nd_async_domain);
495}
496EXPORT_SYMBOL_GPL(nd_synchronize);
497
498static void nd_async_device_register(void *d, async_cookie_t cookie)
499{
500 struct device *dev = d;
501
502 if (device_add(dev) != 0) {
503 dev_err(dev, "%s: failed\n", __func__);
504 put_device(dev);
505 }
506 put_device(dev);
Alexander Duyckb6eae0f2018-09-25 13:53:02 -0700507 if (dev->parent)
508 put_device(dev->parent);
Dan Williams4d88a972015-05-31 14:41:48 -0400509}
510
511static void nd_async_device_unregister(void *d, async_cookie_t cookie)
512{
513 struct device *dev = d;
514
Dan Williams0ba1c632015-05-30 12:35:36 -0400515 /* flush bus operations before delete */
516 nvdimm_bus_lock(dev);
517 nvdimm_bus_unlock(dev);
518
Dan Williams4d88a972015-05-31 14:41:48 -0400519 device_unregister(dev);
520 put_device(dev);
521}
522
Dan Williams8c2f7e82015-06-25 04:20:04 -0400523void __nd_device_register(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400524{
Dan Williamscd034122016-03-11 10:15:36 -0800525 if (!dev)
526 return;
Alexander Duyck1a091d12018-09-25 13:53:07 -0700527
528 /*
529 * Ensure that region devices always have their NUMA node set as
530 * early as possible. This way we are able to make certain that
531 * any memory associated with the creation and the creation
532 * itself of the region is associated with the correct node.
533 */
534 if (is_nd_region(dev))
535 set_dev_node(dev, to_nd_region(dev)->numa_node);
536
Dan Williams4d88a972015-05-31 14:41:48 -0400537 dev->bus = &nvdimm_bus_type;
Alexander Duyckaf87b9a2019-01-22 10:39:47 -0800538 if (dev->parent) {
Alexander Duyckb6eae0f2018-09-25 13:53:02 -0700539 get_device(dev->parent);
Alexander Duyckaf87b9a2019-01-22 10:39:47 -0800540 if (dev_to_node(dev) == NUMA_NO_NODE)
541 set_dev_node(dev, dev_to_node(dev->parent));
542 }
Dan Williams4d88a972015-05-31 14:41:48 -0400543 get_device(dev);
Alexander Duyckaf87b9a2019-01-22 10:39:47 -0800544
545 async_schedule_dev_domain(nd_async_device_register, dev,
546 &nd_async_domain);
Dan Williams4d88a972015-05-31 14:41:48 -0400547}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400548
549void nd_device_register(struct device *dev)
550{
551 device_initialize(dev);
552 __nd_device_register(dev);
553}
Dan Williams4d88a972015-05-31 14:41:48 -0400554EXPORT_SYMBOL(nd_device_register);
555
556void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
557{
558 switch (mode) {
559 case ND_ASYNC:
560 get_device(dev);
561 async_schedule_domain(nd_async_device_unregister, dev,
562 &nd_async_domain);
563 break;
564 case ND_SYNC:
565 nd_synchronize();
566 device_unregister(dev);
567 break;
568 }
569}
570EXPORT_SYMBOL(nd_device_unregister);
571
572/**
573 * __nd_driver_register() - register a region or a namespace driver
574 * @nd_drv: driver to register
575 * @owner: automatically set by nd_driver_register() macro
576 * @mod_name: automatically set by nd_driver_register() macro
577 */
578int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
579 const char *mod_name)
580{
581 struct device_driver *drv = &nd_drv->drv;
582
583 if (!nd_drv->type) {
Sakari Ailusd75f7732019-03-25 21:32:28 +0200584 pr_debug("driver type bitmask not set (%ps)\n",
Dan Williams4d88a972015-05-31 14:41:48 -0400585 __builtin_return_address(0));
586 return -EINVAL;
587 }
588
Dan Williams6cf9c5b2016-05-18 09:13:13 -0700589 if (!nd_drv->probe) {
590 pr_debug("%s ->probe() must be specified\n", mod_name);
Dan Williams4d88a972015-05-31 14:41:48 -0400591 return -EINVAL;
592 }
593
594 drv->bus = &nvdimm_bus_type;
595 drv->owner = owner;
596 drv->mod_name = mod_name;
597
598 return driver_register(drv);
599}
600EXPORT_SYMBOL(__nd_driver_register);
601
Dan Williams58138822015-06-23 20:08:34 -0400602int nvdimm_revalidate_disk(struct gendisk *disk)
603{
Dan Williams52c44d92016-06-15 19:43:07 -0700604 struct device *dev = disk_to_dev(disk)->parent;
Dan Williams58138822015-06-23 20:08:34 -0400605 struct nd_region *nd_region = to_nd_region(dev->parent);
Robert Elliott254a4cd2018-05-31 18:36:36 -0500606 int disk_ro = get_disk_ro(disk);
Dan Williams58138822015-06-23 20:08:34 -0400607
Robert Elliott254a4cd2018-05-31 18:36:36 -0500608 /*
609 * Upgrade to read-only if the region is read-only preserve as
610 * read-only if the disk is already read-only.
611 */
612 if (disk_ro || nd_region->ro == disk_ro)
Dan Williams58138822015-06-23 20:08:34 -0400613 return 0;
614
Robert Elliott254a4cd2018-05-31 18:36:36 -0500615 dev_info(dev, "%s read-only, marking %s read-only\n",
616 dev_name(&nd_region->dev), disk->disk_name);
617 set_disk_ro(disk, 1);
Dan Williams58138822015-06-23 20:08:34 -0400618
619 return 0;
620
621}
622EXPORT_SYMBOL(nvdimm_revalidate_disk);
623
Dan Williams4d88a972015-05-31 14:41:48 -0400624static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
625 char *buf)
626{
627 return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
628 to_nd_device_type(dev));
629}
630static DEVICE_ATTR_RO(modalias);
631
632static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
633 char *buf)
634{
635 return sprintf(buf, "%s\n", dev->type->name);
636}
637static DEVICE_ATTR_RO(devtype);
638
639static struct attribute *nd_device_attributes[] = {
640 &dev_attr_modalias.attr,
641 &dev_attr_devtype.attr,
642 NULL,
643};
644
Qian Caic01dafa2019-05-16 12:04:53 -0400645/*
Dan Williams4d88a972015-05-31 14:41:48 -0400646 * nd_device_attribute_group - generic attributes for all devices on an nd bus
647 */
648struct attribute_group nd_device_attribute_group = {
649 .attrs = nd_device_attributes,
650};
651EXPORT_SYMBOL_GPL(nd_device_attribute_group);
652
Toshi Kani74ae66c2015-06-19 12:18:34 -0600653static ssize_t numa_node_show(struct device *dev,
654 struct device_attribute *attr, char *buf)
655{
656 return sprintf(buf, "%d\n", dev_to_node(dev));
657}
658static DEVICE_ATTR_RO(numa_node);
659
660static struct attribute *nd_numa_attributes[] = {
661 &dev_attr_numa_node.attr,
662 NULL,
663};
664
665static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
666 int n)
667{
668 if (!IS_ENABLED(CONFIG_NUMA))
669 return 0;
670
671 return a->mode;
672}
673
Qian Caic01dafa2019-05-16 12:04:53 -0400674/*
Toshi Kani74ae66c2015-06-19 12:18:34 -0600675 * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
676 */
677struct attribute_group nd_numa_attribute_group = {
678 .attrs = nd_numa_attributes,
679 .is_visible = nd_numa_attr_visible,
680};
681EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
682
Dan Williams45def222015-04-26 19:26:48 -0400683int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
684{
685 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
686 struct device *dev;
687
688 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
689 "ndctl%d", nvdimm_bus->id);
690
Dan Williams42588952016-05-27 13:28:31 -0700691 if (IS_ERR(dev))
Dan Williams45def222015-04-26 19:26:48 -0400692 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
693 nvdimm_bus->id, PTR_ERR(dev));
Dan Williams42588952016-05-27 13:28:31 -0700694 return PTR_ERR_OR_ZERO(dev);
Dan Williams45def222015-04-26 19:26:48 -0400695}
696
697void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
698{
699 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
700}
701
Dan Williams62232e452015-06-08 14:27:06 -0400702static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
703 [ND_CMD_IMPLEMENTED] = { },
704 [ND_CMD_SMART] = {
705 .out_num = 2,
Dan Williams21129112016-04-07 19:58:44 -0700706 .out_sizes = { 4, 128, },
Dan Williams62232e452015-06-08 14:27:06 -0400707 },
708 [ND_CMD_SMART_THRESHOLD] = {
709 .out_num = 2,
710 .out_sizes = { 4, 8, },
711 },
712 [ND_CMD_DIMM_FLAGS] = {
713 .out_num = 2,
714 .out_sizes = { 4, 4 },
715 },
716 [ND_CMD_GET_CONFIG_SIZE] = {
717 .out_num = 3,
718 .out_sizes = { 4, 4, 4, },
719 },
720 [ND_CMD_GET_CONFIG_DATA] = {
721 .in_num = 2,
722 .in_sizes = { 4, 4, },
723 .out_num = 2,
724 .out_sizes = { 4, UINT_MAX, },
725 },
726 [ND_CMD_SET_CONFIG_DATA] = {
727 .in_num = 3,
728 .in_sizes = { 4, 4, UINT_MAX, },
729 .out_num = 1,
730 .out_sizes = { 4, },
731 },
732 [ND_CMD_VENDOR] = {
733 .in_num = 3,
734 .in_sizes = { 4, 4, UINT_MAX, },
735 .out_num = 3,
736 .out_sizes = { 4, 4, UINT_MAX, },
737 },
Dan Williams31eca762016-04-28 16:23:43 -0700738 [ND_CMD_CALL] = {
739 .in_num = 2,
740 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
741 .out_num = 1,
742 .out_sizes = { UINT_MAX, },
743 },
Dan Williams62232e452015-06-08 14:27:06 -0400744};
745
746const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
747{
748 if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
749 return &__nd_cmd_dimm_descs[cmd];
750 return NULL;
751}
752EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
753
754static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
755 [ND_CMD_IMPLEMENTED] = { },
756 [ND_CMD_ARS_CAP] = {
757 .in_num = 2,
758 .in_sizes = { 8, 8, },
Dan Williams4577b062016-02-17 13:08:58 -0800759 .out_num = 4,
760 .out_sizes = { 4, 4, 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400761 },
762 [ND_CMD_ARS_START] = {
Dan Williams4577b062016-02-17 13:08:58 -0800763 .in_num = 5,
764 .in_sizes = { 8, 8, 2, 1, 5, },
765 .out_num = 2,
766 .out_sizes = { 4, 4, },
Dan Williams62232e452015-06-08 14:27:06 -0400767 },
768 [ND_CMD_ARS_STATUS] = {
Dan Williams747ffe12016-02-19 15:21:14 -0800769 .out_num = 3,
770 .out_sizes = { 4, 4, UINT_MAX, },
Dan Williams62232e452015-06-08 14:27:06 -0400771 },
Dan Williamsd4f32362016-03-03 16:08:54 -0800772 [ND_CMD_CLEAR_ERROR] = {
773 .in_num = 2,
774 .in_sizes = { 8, 8, },
775 .out_num = 3,
776 .out_sizes = { 4, 4, 8, },
777 },
Dan Williams31eca762016-04-28 16:23:43 -0700778 [ND_CMD_CALL] = {
779 .in_num = 2,
780 .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
781 .out_num = 1,
782 .out_sizes = { UINT_MAX, },
783 },
Dan Williams62232e452015-06-08 14:27:06 -0400784};
785
786const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
787{
788 if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
789 return &__nd_cmd_bus_descs[cmd];
790 return NULL;
791}
792EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
793
794u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
795 const struct nd_cmd_desc *desc, int idx, void *buf)
796{
797 if (idx >= desc->in_num)
798 return UINT_MAX;
799
800 if (desc->in_sizes[idx] < UINT_MAX)
801 return desc->in_sizes[idx];
802
803 if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
804 struct nd_cmd_set_config_hdr *hdr = buf;
805
806 return hdr->in_length;
807 } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
808 struct nd_cmd_vendor_hdr *hdr = buf;
809
810 return hdr->in_length;
Dan Williams31eca762016-04-28 16:23:43 -0700811 } else if (cmd == ND_CMD_CALL) {
812 struct nd_cmd_pkg *pkg = buf;
813
814 return pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400815 }
816
817 return UINT_MAX;
818}
819EXPORT_SYMBOL_GPL(nd_cmd_in_size);
820
821u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
822 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800823 const u32 *out_field, unsigned long remainder)
Dan Williams62232e452015-06-08 14:27:06 -0400824{
825 if (idx >= desc->out_num)
826 return UINT_MAX;
827
828 if (desc->out_sizes[idx] < UINT_MAX)
829 return desc->out_sizes[idx];
830
831 if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
832 return in_field[1];
833 else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
834 return out_field[1];
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800835 else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
836 /*
837 * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
838 * "Size of Output Buffer in bytes, including this
839 * field."
840 */
841 if (out_field[1] < 4)
842 return 0;
843 /*
844 * ACPI 6.1 is ambiguous if 'status' is included in the
845 * output size. If we encounter an output size that
846 * overshoots the remainder by 4 bytes, assume it was
847 * including 'status'.
848 */
Vishal Verma286e8772018-08-10 13:23:15 -0600849 if (out_field[1] - 4 == remainder)
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800850 return remainder;
Vishal Verma286e8772018-08-10 13:23:15 -0600851 return out_field[1] - 8;
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800852 } else if (cmd == ND_CMD_CALL) {
Dan Williams31eca762016-04-28 16:23:43 -0700853 struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
854
855 return pkg->nd_size_out;
856 }
857
Dan Williams62232e452015-06-08 14:27:06 -0400858
859 return UINT_MAX;
860}
861EXPORT_SYMBOL_GPL(nd_cmd_out_size);
862
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400863void wait_nvdimm_bus_probe_idle(struct device *dev)
Dan Williamseaf96152015-05-01 13:11:27 -0400864{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400865 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
866
Dan Williamseaf96152015-05-01 13:11:27 -0400867 do {
868 if (nvdimm_bus->probe_active == 0)
869 break;
870 nvdimm_bus_unlock(&nvdimm_bus->dev);
871 wait_event(nvdimm_bus->probe_wait,
872 nvdimm_bus->probe_active == 0);
873 nvdimm_bus_lock(&nvdimm_bus->dev);
874 } while (true);
875}
876
Dave Jiang006358b2017-04-07 15:33:31 -0700877static int nd_pmem_forget_poison_check(struct device *dev, void *data)
Dan Williamsd4f32362016-03-03 16:08:54 -0800878{
Dave Jiang006358b2017-04-07 15:33:31 -0700879 struct nd_cmd_clear_error *clear_err =
880 (struct nd_cmd_clear_error *)data;
881 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
882 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
883 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
884 struct nd_namespace_common *ndns = NULL;
885 struct nd_namespace_io *nsio;
886 resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
887
888 if (nd_dax || !dev->driver)
889 return 0;
890
891 start = clear_err->address;
892 end = clear_err->address + clear_err->cleared - 1;
893
894 if (nd_btt || nd_pfn || nd_dax) {
895 if (nd_btt)
896 ndns = nd_btt->ndns;
897 else if (nd_pfn)
898 ndns = nd_pfn->ndns;
899 else if (nd_dax)
900 ndns = nd_dax->nd_pfn.ndns;
901
902 if (!ndns)
903 return 0;
904 } else
905 ndns = to_ndns(dev);
906
907 nsio = to_nd_namespace_io(&ndns->dev);
908 pstart = nsio->res.start + offset;
909 pend = nsio->res.end - end_trunc;
910
911 if ((pstart >= start) && (pend <= end))
Dan Williamsd4f32362016-03-03 16:08:54 -0800912 return -EBUSY;
Dave Jiang006358b2017-04-07 15:33:31 -0700913
Dan Williamsd4f32362016-03-03 16:08:54 -0800914 return 0;
Dave Jiang006358b2017-04-07 15:33:31 -0700915
916}
917
918static int nd_ns_forget_poison_check(struct device *dev, void *data)
919{
920 return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800921}
922
Dan Williamseaf96152015-05-01 13:11:27 -0400923/* set_config requires an idle interleave set */
Dan Williams87bf5722016-02-22 21:50:31 -0800924static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
Dave Jiang006358b2017-04-07 15:33:31 -0700925 struct nvdimm *nvdimm, unsigned int cmd, void *data)
Dan Williamseaf96152015-05-01 13:11:27 -0400926{
Dan Williams87bf5722016-02-22 21:50:31 -0800927 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
928
929 /* ask the bus provider if it would like to block this request */
930 if (nd_desc->clear_to_send) {
Dave Jiangb3ed2ce2018-12-04 10:31:11 -0800931 int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd, data);
Dan Williams87bf5722016-02-22 21:50:31 -0800932
933 if (rc)
934 return rc;
935 }
Dan Williamseaf96152015-05-01 13:11:27 -0400936
Dan Williamsd4f32362016-03-03 16:08:54 -0800937 /* require clear error to go through the pmem driver */
938 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
Dave Jiang006358b2017-04-07 15:33:31 -0700939 return device_for_each_child(&nvdimm_bus->dev, data,
940 nd_ns_forget_poison_check);
Dan Williamsd4f32362016-03-03 16:08:54 -0800941
Dan Williamseaf96152015-05-01 13:11:27 -0400942 if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
943 return 0;
944
Dan Williams87bf5722016-02-22 21:50:31 -0800945 /* prevent label manipulation while the kernel owns label updates */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400946 wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400947 if (atomic_read(&nvdimm->busy))
948 return -EBUSY;
949 return 0;
950}
951
Dan Williams62232e452015-06-08 14:27:06 -0400952static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
953 int read_only, unsigned int ioctl_cmd, unsigned long arg)
954{
955 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
Dan Williams62232e452015-06-08 14:27:06 -0400956 static char out_env[ND_CMD_MAX_ENVELOPE];
957 static char in_env[ND_CMD_MAX_ENVELOPE];
958 const struct nd_cmd_desc *desc = NULL;
959 unsigned int cmd = _IOC_NR(ioctl_cmd);
Dan Williams62232e452015-06-08 14:27:06 -0400960 struct device *dev = &nvdimm_bus->dev;
Dan Williams58738c42017-08-31 15:41:55 -0700961 void __user *p = (void __user *) arg;
Dan Williams62232e452015-06-08 14:27:06 -0400962 const char *cmd_name, *dimm_name;
Dan Williams58738c42017-08-31 15:41:55 -0700963 u32 in_len = 0, out_len = 0;
964 unsigned int func = cmd;
Dan Williamse3654ec2016-04-28 16:17:07 -0700965 unsigned long cmd_mask;
Dan Williams58738c42017-08-31 15:41:55 -0700966 struct nd_cmd_pkg pkg;
Dave Jiang006358b2017-04-07 15:33:31 -0700967 int rc, i, cmd_rc;
Dan Williams58738c42017-08-31 15:41:55 -0700968 u64 buf_len = 0;
969 void *buf;
Dan Williams62232e452015-06-08 14:27:06 -0400970
971 if (nvdimm) {
972 desc = nd_cmd_dimm_desc(cmd);
973 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700974 cmd_mask = nvdimm->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400975 dimm_name = dev_name(&nvdimm->dev);
976 } else {
977 desc = nd_cmd_bus_desc(cmd);
978 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700979 cmd_mask = nd_desc->cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400980 dimm_name = "bus";
981 }
982
Dan Williams31eca762016-04-28 16:23:43 -0700983 if (cmd == ND_CMD_CALL) {
984 if (copy_from_user(&pkg, p, sizeof(pkg)))
985 return -EFAULT;
986 }
987
Dan Williams62232e452015-06-08 14:27:06 -0400988 if (!desc || (desc->out_num + desc->in_num == 0) ||
Dan Williamse3654ec2016-04-28 16:17:07 -0700989 !test_bit(cmd, &cmd_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400990 return -ENOTTY;
991
992 /* fail write commands (when read-only) */
993 if (read_only)
Jerry Hoemann07accfa2016-01-06 16:03:41 -0700994 switch (cmd) {
995 case ND_CMD_VENDOR:
996 case ND_CMD_SET_CONFIG_DATA:
997 case ND_CMD_ARS_START:
Dan Williamsd4f32362016-03-03 16:08:54 -0800998 case ND_CMD_CLEAR_ERROR:
Dan Williams31eca762016-04-28 16:23:43 -0700999 case ND_CMD_CALL:
Dan Williams62232e452015-06-08 14:27:06 -04001000 dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
1001 nvdimm ? nvdimm_cmd_name(cmd)
1002 : nvdimm_bus_cmd_name(cmd));
1003 return -EPERM;
1004 default:
1005 break;
1006 }
1007
1008 /* process an input envelope */
1009 for (i = 0; i < desc->in_num; i++) {
1010 u32 in_size, copy;
1011
1012 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
1013 if (in_size == UINT_MAX) {
1014 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
1015 __func__, dimm_name, cmd_name, i);
1016 return -ENXIO;
1017 }
Dan Williams62232e452015-06-08 14:27:06 -04001018 if (in_len < sizeof(in_env))
1019 copy = min_t(u32, sizeof(in_env) - in_len, in_size);
1020 else
1021 copy = 0;
1022 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
1023 return -EFAULT;
1024 in_len += in_size;
1025 }
1026
Dan Williams31eca762016-04-28 16:23:43 -07001027 if (cmd == ND_CMD_CALL) {
Jerry Hoemann53b85a42017-06-30 20:41:22 -07001028 func = pkg.nd_command;
Dan Williams426824d2018-03-05 16:39:31 -08001029 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
1030 dimm_name, pkg.nd_command,
Dan Williams31eca762016-04-28 16:23:43 -07001031 in_len, out_len, buf_len);
Dan Williams31eca762016-04-28 16:23:43 -07001032 }
1033
Dan Williams62232e452015-06-08 14:27:06 -04001034 /* process an output envelope */
1035 for (i = 0; i < desc->out_num; i++) {
1036 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
Dan Williamsefda1b5d2016-12-06 09:10:12 -08001037 (u32 *) in_env, (u32 *) out_env, 0);
Dan Williams62232e452015-06-08 14:27:06 -04001038 u32 copy;
1039
1040 if (out_size == UINT_MAX) {
Dan Williams426824d2018-03-05 16:39:31 -08001041 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
1042 dimm_name, cmd_name, i);
Dan Williams62232e452015-06-08 14:27:06 -04001043 return -EFAULT;
1044 }
Dan Williams62232e452015-06-08 14:27:06 -04001045 if (out_len < sizeof(out_env))
1046 copy = min_t(u32, sizeof(out_env) - out_len, out_size);
1047 else
1048 copy = 0;
1049 if (copy && copy_from_user(&out_env[out_len],
1050 p + in_len + out_len, copy))
1051 return -EFAULT;
1052 out_len += out_size;
1053 }
1054
Dan Williams58738c42017-08-31 15:41:55 -07001055 buf_len = (u64) out_len + (u64) in_len;
Dan Williams62232e452015-06-08 14:27:06 -04001056 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
Dan Williams426824d2018-03-05 16:39:31 -08001057 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
1058 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
Dan Williams62232e452015-06-08 14:27:06 -04001059 return -EINVAL;
1060 }
1061
1062 buf = vmalloc(buf_len);
1063 if (!buf)
1064 return -ENOMEM;
1065
1066 if (copy_from_user(buf, p, buf_len)) {
1067 rc = -EFAULT;
1068 goto out;
1069 }
1070
Dan Williamseaf96152015-05-01 13:11:27 -04001071 nvdimm_bus_lock(&nvdimm_bus->dev);
Jerry Hoemann53b85a42017-06-30 20:41:22 -07001072 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
Dan Williamseaf96152015-05-01 13:11:27 -04001073 if (rc)
1074 goto out_unlock;
1075
Dave Jiang006358b2017-04-07 15:33:31 -07001076 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
Dan Williams62232e452015-06-08 14:27:06 -04001077 if (rc < 0)
Dan Williamseaf96152015-05-01 13:11:27 -04001078 goto out_unlock;
Dave Jiang006358b2017-04-07 15:33:31 -07001079
1080 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
1081 struct nd_cmd_clear_error *clear_err = buf;
Dave Jiang006358b2017-04-07 15:33:31 -07001082
Dan Williams23f49842017-04-29 15:24:03 -07001083 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
1084 clear_err->cleared);
Dave Jiang006358b2017-04-07 15:33:31 -07001085 }
Dan Williams0beb2012017-04-07 09:47:24 -07001086 nvdimm_bus_unlock(&nvdimm_bus->dev);
1087
Dan Williams62232e452015-06-08 14:27:06 -04001088 if (copy_to_user(p, buf, buf_len))
1089 rc = -EFAULT;
Dan Williams0beb2012017-04-07 09:47:24 -07001090
1091 vfree(buf);
1092 return rc;
1093
Dan Williamseaf96152015-05-01 13:11:27 -04001094 out_unlock:
1095 nvdimm_bus_unlock(&nvdimm_bus->dev);
Dan Williams62232e452015-06-08 14:27:06 -04001096 out:
1097 vfree(buf);
1098 return rc;
1099}
1100
Dan Williams45def222015-04-26 19:26:48 -04001101static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1102{
Dan Williams62232e452015-06-08 14:27:06 -04001103 long id = (long) file->private_data;
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001104 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001105 struct nvdimm_bus *nvdimm_bus;
1106
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001107 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001108 mutex_lock(&nvdimm_bus_list_mutex);
1109 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1110 if (nvdimm_bus->id == id) {
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001111 rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001112 break;
1113 }
1114 }
1115 mutex_unlock(&nvdimm_bus_list_mutex);
1116
1117 return rc;
1118}
1119
1120static int match_dimm(struct device *dev, void *data)
1121{
1122 long id = (long) data;
1123
1124 if (is_nvdimm(dev)) {
1125 struct nvdimm *nvdimm = to_nvdimm(dev);
1126
1127 return nvdimm->id == id;
1128 }
1129
1130 return 0;
1131}
1132
1133static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1134{
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001135 int rc = -ENXIO, ro;
Dan Williams62232e452015-06-08 14:27:06 -04001136 struct nvdimm_bus *nvdimm_bus;
1137
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001138 ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
Dan Williams62232e452015-06-08 14:27:06 -04001139 mutex_lock(&nvdimm_bus_list_mutex);
1140 list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
1141 struct device *dev = device_find_child(&nvdimm_bus->dev,
1142 file->private_data, match_dimm);
1143 struct nvdimm *nvdimm;
1144
1145 if (!dev)
1146 continue;
1147
1148 nvdimm = to_nvdimm(dev);
Jerry Hoemann4dc0e7be2016-01-06 16:03:39 -07001149 rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
Dan Williams62232e452015-06-08 14:27:06 -04001150 put_device(dev);
1151 break;
1152 }
1153 mutex_unlock(&nvdimm_bus_list_mutex);
1154
1155 return rc;
1156}
1157
1158static int nd_open(struct inode *inode, struct file *file)
1159{
1160 long minor = iminor(inode);
1161
1162 file->private_data = (void *) minor;
1163 return 0;
Dan Williams45def222015-04-26 19:26:48 -04001164}
1165
1166static const struct file_operations nvdimm_bus_fops = {
1167 .owner = THIS_MODULE,
Dan Williams62232e452015-06-08 14:27:06 -04001168 .open = nd_open,
Dan Williams45def222015-04-26 19:26:48 -04001169 .unlocked_ioctl = nd_ioctl,
1170 .compat_ioctl = nd_ioctl,
1171 .llseek = noop_llseek,
1172};
1173
Dan Williams62232e452015-06-08 14:27:06 -04001174static const struct file_operations nvdimm_fops = {
1175 .owner = THIS_MODULE,
1176 .open = nd_open,
1177 .unlocked_ioctl = nvdimm_ioctl,
1178 .compat_ioctl = nvdimm_ioctl,
1179 .llseek = noop_llseek,
1180};
1181
Dan Williams45def222015-04-26 19:26:48 -04001182int __init nvdimm_bus_init(void)
1183{
1184 int rc;
1185
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001186 rc = bus_register(&nvdimm_bus_type);
1187 if (rc)
1188 return rc;
1189
Dan Williams45def222015-04-26 19:26:48 -04001190 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
1191 if (rc < 0)
Dan Williams62232e452015-06-08 14:27:06 -04001192 goto err_bus_chrdev;
Dan Williams45def222015-04-26 19:26:48 -04001193 nvdimm_bus_major = rc;
1194
Dan Williams62232e452015-06-08 14:27:06 -04001195 rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
1196 if (rc < 0)
1197 goto err_dimm_chrdev;
1198 nvdimm_major = rc;
1199
Dan Williams45def222015-04-26 19:26:48 -04001200 nd_class = class_create(THIS_MODULE, "nd");
Axel Lindaa1dee2015-06-28 17:00:57 +08001201 if (IS_ERR(nd_class)) {
1202 rc = PTR_ERR(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001203 goto err_class;
Axel Lindaa1dee2015-06-28 17:00:57 +08001204 }
Dan Williams45def222015-04-26 19:26:48 -04001205
Dan Williams18515942016-07-22 23:46:08 -07001206 rc = driver_register(&nd_bus_driver.drv);
1207 if (rc)
1208 goto err_nd_bus;
1209
Dan Williams45def222015-04-26 19:26:48 -04001210 return 0;
1211
Dan Williams18515942016-07-22 23:46:08 -07001212 err_nd_bus:
1213 class_destroy(nd_class);
Dan Williams45def222015-04-26 19:26:48 -04001214 err_class:
Dan Williams62232e452015-06-08 14:27:06 -04001215 unregister_chrdev(nvdimm_major, "dimmctl");
1216 err_dimm_chrdev:
Dan Williams45def222015-04-26 19:26:48 -04001217 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001218 err_bus_chrdev:
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001219 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -04001220
1221 return rc;
1222}
1223
Dan Williams4d88a972015-05-31 14:41:48 -04001224void nvdimm_bus_exit(void)
Dan Williams45def222015-04-26 19:26:48 -04001225{
Dan Williams18515942016-07-22 23:46:08 -07001226 driver_unregister(&nd_bus_driver.drv);
Dan Williams45def222015-04-26 19:26:48 -04001227 class_destroy(nd_class);
1228 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williams62232e452015-06-08 14:27:06 -04001229 unregister_chrdev(nvdimm_major, "dimmctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001230 bus_unregister(&nvdimm_bus_type);
Dan Williams18515942016-07-22 23:46:08 -07001231 ida_destroy(&nd_ida);
Dan Williams45def222015-04-26 19:26:48 -04001232}