Commit | Line | Data |
---|---|---|
45def22c DW |
1 | /* |
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 | |
aa9ad44a | 14 | #include <linux/libnvdimm.h> |
0930a750 | 15 | #include <linux/sched/mm.h> |
62232e45 | 16 | #include <linux/vmalloc.h> |
45def22c | 17 | #include <linux/uaccess.h> |
3d88002e | 18 | #include <linux/module.h> |
8c2f7e86 | 19 | #include <linux/blkdev.h> |
45def22c | 20 | #include <linux/fcntl.h> |
e6dfb2de | 21 | #include <linux/async.h> |
8c2f7e86 | 22 | #include <linux/genhd.h> |
62232e45 | 23 | #include <linux/ndctl.h> |
4d88a97a | 24 | #include <linux/sched.h> |
45def22c | 25 | #include <linux/slab.h> |
af87b9a7 | 26 | #include <linux/cpu.h> |
45def22c DW |
27 | #include <linux/fs.h> |
28 | #include <linux/io.h> | |
62232e45 | 29 | #include <linux/mm.h> |
4d88a97a | 30 | #include <linux/nd.h> |
45def22c | 31 | #include "nd-core.h" |
4d88a97a | 32 | #include "nd.h" |
006358b3 | 33 | #include "pfn.h" |
45def22c | 34 | |
62232e45 | 35 | int nvdimm_major; |
45def22c DW |
36 | static int nvdimm_bus_major; |
37 | static struct class *nd_class; | |
18515942 | 38 | static DEFINE_IDA(nd_ida); |
45def22c | 39 | |
4d88a97a DW |
40 | static int to_nd_device_type(struct device *dev) |
41 | { | |
42 | if (is_nvdimm(dev)) | |
43 | return ND_DEVICE_DIMM; | |
c9e582aa | 44 | else if (is_memory(dev)) |
3d88002e DW |
45 | return ND_DEVICE_REGION_PMEM; |
46 | else if (is_nd_blk(dev)) | |
47 | return ND_DEVICE_REGION_BLK; | |
cd03412a DW |
48 | else if (is_nd_dax(dev)) |
49 | return ND_DEVICE_DAX_PMEM; | |
c9e582aa | 50 | else if (is_nd_region(dev->parent)) |
3d88002e | 51 | return nd_region_to_nstype(to_nd_region(dev->parent)); |
4d88a97a DW |
52 | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static 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 | ||
3d88002e DW |
62 | static struct module *to_bus_provider(struct device *dev) |
63 | { | |
64 | /* pin bus providers while regions are enabled */ | |
c9e582aa | 65 | if (is_nd_region(dev)) { |
3d88002e DW |
66 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
67 | ||
bc9775d8 | 68 | return nvdimm_bus->nd_desc->module; |
3d88002e DW |
69 | } |
70 | return NULL; | |
71 | } | |
72 | ||
eaf96153 DW |
73 | static 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 | ||
80 | static 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 | ||
4d88a97a DW |
88 | static int nvdimm_bus_probe(struct device *dev) |
89 | { | |
90 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); | |
3d88002e | 91 | struct module *provider = to_bus_provider(dev); |
4d88a97a DW |
92 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
93 | int rc; | |
94 | ||
3d88002e DW |
95 | if (!try_module_get(provider)) |
96 | return -ENXIO; | |
97 | ||
3f46833d DW |
98 | dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n", |
99 | dev->driver->name, dev_name(dev)); | |
100 | ||
eaf96153 | 101 | nvdimm_bus_probe_start(nvdimm_bus); |
4d88a97a | 102 | rc = nd_drv->probe(dev); |
eaf96153 DW |
103 | if (rc == 0) |
104 | nd_region_probe_success(nvdimm_bus, dev); | |
bf9bccc1 DW |
105 | else |
106 | nd_region_disable(nvdimm_bus, dev); | |
eaf96153 DW |
107 | nvdimm_bus_probe_end(nvdimm_bus); |
108 | ||
3f46833d | 109 | dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name, |
4d88a97a | 110 | dev_name(dev), rc); |
8c2f7e86 | 111 | |
3d88002e DW |
112 | if (rc != 0) |
113 | module_put(provider); | |
4d88a97a DW |
114 | return rc; |
115 | } | |
116 | ||
117 | static int nvdimm_bus_remove(struct device *dev) | |
118 | { | |
119 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); | |
3d88002e | 120 | struct module *provider = to_bus_provider(dev); |
4d88a97a | 121 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
6cf9c5ba | 122 | int rc = 0; |
4d88a97a | 123 | |
6cf9c5ba DW |
124 | if (nd_drv->remove) |
125 | rc = nd_drv->remove(dev); | |
eaf96153 DW |
126 | nd_region_disable(nvdimm_bus, dev); |
127 | ||
4d88a97a DW |
128 | dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name, |
129 | dev_name(dev), rc); | |
3d88002e | 130 | module_put(provider); |
4d88a97a DW |
131 | return rc; |
132 | } | |
133 | ||
476f848a DW |
134 | static 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 | ||
71999466 DW |
149 | void 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 | } | |
161 | EXPORT_SYMBOL(nd_device_notify); | |
162 | ||
163 | void 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 | } | |
173 | EXPORT_SYMBOL_GPL(nvdimm_region_notify); | |
174 | ||
23f49844 DW |
175 | struct clear_badblocks_context { |
176 | resource_size_t phys, cleared; | |
177 | }; | |
178 | ||
179 | static 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 | ||
975750a9 TK |
201 | if (nd_region->bb_state) |
202 | sysfs_notify_dirent(nd_region->bb_state); | |
203 | ||
23f49844 DW |
204 | return 0; |
205 | } | |
206 | ||
207 | static 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 | ||
219 | static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus, | |
220 | phys_addr_t phys, u64 cleared) | |
221 | { | |
222 | if (cleared > 0) | |
aa9ad44a | 223 | badrange_forget(&nvdimm_bus->badrange, phys, cleared); |
23f49844 DW |
224 | |
225 | if (cleared > 0 && cleared / 512) | |
226 | nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared); | |
227 | } | |
228 | ||
59e64739 DW |
229 | long 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; | |
0930a750 | 237 | unsigned int noio_flag; |
59e64739 DW |
238 | int cmd_rc, rc; |
239 | ||
240 | if (!nvdimm_bus) | |
241 | return -ENXIO; | |
242 | ||
243 | nd_desc = nvdimm_bus->nd_desc; | |
1e8b8d96 DJ |
244 | /* |
245 | * if ndctl does not exist, it's PMEM_LEGACY and | |
246 | * we want to just pretend everything is handled. | |
247 | */ | |
59e64739 | 248 | if (!nd_desc->ndctl) |
1e8b8d96 | 249 | return len; |
59e64739 DW |
250 | |
251 | memset(&ars_cap, 0, sizeof(ars_cap)); | |
252 | ars_cap.address = phys; | |
253 | ars_cap.length = len; | |
0930a750 | 254 | noio_flag = memalloc_noio_save(); |
59e64739 DW |
255 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap, |
256 | sizeof(ars_cap), &cmd_rc); | |
0930a750 | 257 | memalloc_noio_restore(noio_flag); |
59e64739 DW |
258 | 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; | |
0930a750 | 272 | noio_flag = memalloc_noio_save(); |
59e64739 DW |
273 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err, |
274 | sizeof(clear_err), &cmd_rc); | |
0930a750 | 275 | memalloc_noio_restore(noio_flag); |
59e64739 DW |
276 | if (rc < 0) |
277 | return rc; | |
278 | if (cmd_rc < 0) | |
279 | return cmd_rc; | |
e046114a | 280 | |
23f49844 | 281 | nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared); |
8d13c029 | 282 | |
59e64739 DW |
283 | return clear_err.cleared; |
284 | } | |
285 | EXPORT_SYMBOL_GPL(nvdimm_clear_poison); | |
286 | ||
18515942 DW |
287 | static int nvdimm_bus_match(struct device *dev, struct device_driver *drv); |
288 | ||
4d88a97a | 289 | static struct bus_type nvdimm_bus_type = { |
e6dfb2de | 290 | .name = "nd", |
4d88a97a DW |
291 | .uevent = nvdimm_bus_uevent, |
292 | .match = nvdimm_bus_match, | |
293 | .probe = nvdimm_bus_probe, | |
294 | .remove = nvdimm_bus_remove, | |
476f848a | 295 | .shutdown = nvdimm_bus_shutdown, |
4d88a97a DW |
296 | }; |
297 | ||
18515942 DW |
298 | static 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 | ||
307 | static bool is_nvdimm_bus(struct device *dev) | |
308 | { | |
309 | return dev->release == nvdimm_bus_release; | |
310 | } | |
311 | ||
312 | struct 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 | ||
325 | struct 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 | } | |
333 | EXPORT_SYMBOL_GPL(to_nvdimm_bus); | |
334 | ||
f2989396 DJ |
335 | struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm) |
336 | { | |
337 | return to_nvdimm_bus(nvdimm->dev.parent); | |
338 | } | |
339 | EXPORT_SYMBOL_GPL(nvdimm_to_bus); | |
340 | ||
18515942 DW |
341 | struct 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); | |
18515942 DW |
352 | init_waitqueue_head(&nvdimm_bus->probe_wait); |
353 | nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); | |
18515942 DW |
354 | if (nvdimm_bus->id < 0) { |
355 | kfree(nvdimm_bus); | |
356 | return NULL; | |
357 | } | |
9bf3aa44 OH |
358 | mutex_init(&nvdimm_bus->reconfig_mutex); |
359 | badrange_init(&nvdimm_bus->badrange); | |
18515942 DW |
360 | 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; | |
1ff19f48 | 365 | nvdimm_bus->dev.of_node = nd_desc->of_node; |
18515942 DW |
366 | 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 | } | |
378 | EXPORT_SYMBOL_GPL(nvdimm_bus_register); | |
379 | ||
380 | void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus) | |
381 | { | |
382 | if (!nvdimm_bus) | |
383 | return; | |
384 | device_unregister(&nvdimm_bus->dev); | |
385 | } | |
386 | EXPORT_SYMBOL_GPL(nvdimm_bus_unregister); | |
387 | ||
388 | static 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) | |
7d988097 DJ |
397 | 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 | ||
18515942 DW |
415 | return 0; |
416 | } | |
417 | ||
aa9ad44a | 418 | static void free_badrange_list(struct list_head *badrange_list) |
18515942 | 419 | { |
aa9ad44a | 420 | struct badrange_entry *bre, *next; |
18515942 | 421 | |
aa9ad44a DJ |
422 | list_for_each_entry_safe(bre, next, badrange_list, list) { |
423 | list_del(&bre->list); | |
424 | kfree(bre); | |
18515942 | 425 | } |
aa9ad44a | 426 | list_del_init(badrange_list); |
18515942 DW |
427 | } |
428 | ||
429 | static 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 | ||
aa9ad44a DJ |
440 | spin_lock(&nvdimm_bus->badrange.lock); |
441 | free_badrange_list(&nvdimm_bus->badrange.list); | |
442 | spin_unlock(&nvdimm_bus->badrange.lock); | |
18515942 DW |
443 | |
444 | nvdimm_bus_destroy_ndctl(nvdimm_bus); | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
449 | static 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 | ||
468 | static 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 | }, | |
4d88a97a DW |
478 | }; |
479 | ||
18515942 DW |
480 | static 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 | ||
4d88a97a DW |
490 | static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain); |
491 | ||
492 | void nd_synchronize(void) | |
493 | { | |
494 | async_synchronize_full_domain(&nd_async_domain); | |
495 | } | |
496 | EXPORT_SYMBOL_GPL(nd_synchronize); | |
497 | ||
498 | static 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); | |
b6eae0f6 AD |
507 | if (dev->parent) |
508 | put_device(dev->parent); | |
4d88a97a DW |
509 | } |
510 | ||
511 | static void nd_async_device_unregister(void *d, async_cookie_t cookie) | |
512 | { | |
513 | struct device *dev = d; | |
514 | ||
0ba1c634 DW |
515 | /* flush bus operations before delete */ |
516 | nvdimm_bus_lock(dev); | |
517 | nvdimm_bus_unlock(dev); | |
518 | ||
4d88a97a DW |
519 | device_unregister(dev); |
520 | put_device(dev); | |
521 | } | |
522 | ||
8c2f7e86 | 523 | void __nd_device_register(struct device *dev) |
4d88a97a | 524 | { |
cd03412a DW |
525 | if (!dev) |
526 | return; | |
1a091d16 AD |
527 | |
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 | ||
4d88a97a | 537 | dev->bus = &nvdimm_bus_type; |
af87b9a7 | 538 | if (dev->parent) { |
b6eae0f6 | 539 | get_device(dev->parent); |
af87b9a7 AD |
540 | if (dev_to_node(dev) == NUMA_NO_NODE) |
541 | set_dev_node(dev, dev_to_node(dev->parent)); | |
542 | } | |
4d88a97a | 543 | get_device(dev); |
af87b9a7 AD |
544 | |
545 | async_schedule_dev_domain(nd_async_device_register, dev, | |
546 | &nd_async_domain); | |
4d88a97a | 547 | } |
8c2f7e86 DW |
548 | |
549 | void nd_device_register(struct device *dev) | |
550 | { | |
551 | device_initialize(dev); | |
552 | __nd_device_register(dev); | |
553 | } | |
4d88a97a DW |
554 | EXPORT_SYMBOL(nd_device_register); |
555 | ||
556 | void 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 | } | |
570 | EXPORT_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 | */ | |
578 | int __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) { | |
d75f773c | 584 | pr_debug("driver type bitmask not set (%ps)\n", |
4d88a97a DW |
585 | __builtin_return_address(0)); |
586 | return -EINVAL; | |
587 | } | |
588 | ||
6cf9c5ba DW |
589 | if (!nd_drv->probe) { |
590 | pr_debug("%s ->probe() must be specified\n", mod_name); | |
4d88a97a DW |
591 | 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 | } | |
600 | EXPORT_SYMBOL(__nd_driver_register); | |
601 | ||
58138820 DW |
602 | int nvdimm_revalidate_disk(struct gendisk *disk) |
603 | { | |
52c44d93 | 604 | struct device *dev = disk_to_dev(disk)->parent; |
58138820 | 605 | struct nd_region *nd_region = to_nd_region(dev->parent); |
254a4cd5 | 606 | int disk_ro = get_disk_ro(disk); |
58138820 | 607 | |
254a4cd5 RE |
608 | /* |
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) | |
58138820 DW |
613 | return 0; |
614 | ||
254a4cd5 RE |
615 | 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); | |
58138820 DW |
618 | |
619 | return 0; | |
620 | ||
621 | } | |
622 | EXPORT_SYMBOL(nvdimm_revalidate_disk); | |
623 | ||
4d88a97a DW |
624 | static 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 | } | |
630 | static DEVICE_ATTR_RO(modalias); | |
631 | ||
632 | static 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 | } | |
637 | static DEVICE_ATTR_RO(devtype); | |
638 | ||
639 | static struct attribute *nd_device_attributes[] = { | |
640 | &dev_attr_modalias.attr, | |
641 | &dev_attr_devtype.attr, | |
642 | NULL, | |
643 | }; | |
644 | ||
645 | /** | |
646 | * nd_device_attribute_group - generic attributes for all devices on an nd bus | |
647 | */ | |
648 | struct attribute_group nd_device_attribute_group = { | |
649 | .attrs = nd_device_attributes, | |
e6dfb2de | 650 | }; |
4d88a97a | 651 | EXPORT_SYMBOL_GPL(nd_device_attribute_group); |
e6dfb2de | 652 | |
74ae66c3 TK |
653 | static 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 | } | |
658 | static DEVICE_ATTR_RO(numa_node); | |
659 | ||
660 | static struct attribute *nd_numa_attributes[] = { | |
661 | &dev_attr_numa_node.attr, | |
662 | NULL, | |
663 | }; | |
664 | ||
665 | static 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 | ||
674 | /** | |
675 | * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus | |
676 | */ | |
677 | struct attribute_group nd_numa_attribute_group = { | |
678 | .attrs = nd_numa_attributes, | |
679 | .is_visible = nd_numa_attr_visible, | |
680 | }; | |
681 | EXPORT_SYMBOL_GPL(nd_numa_attribute_group); | |
682 | ||
45def22c DW |
683 | int 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 | ||
42588958 | 691 | if (IS_ERR(dev)) |
45def22c DW |
692 | dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n", |
693 | nvdimm_bus->id, PTR_ERR(dev)); | |
42588958 | 694 | return PTR_ERR_OR_ZERO(dev); |
45def22c DW |
695 | } |
696 | ||
697 | void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus) | |
698 | { | |
699 | device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id)); | |
700 | } | |
701 | ||
62232e45 DW |
702 | static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = { |
703 | [ND_CMD_IMPLEMENTED] = { }, | |
704 | [ND_CMD_SMART] = { | |
705 | .out_num = 2, | |
21129112 | 706 | .out_sizes = { 4, 128, }, |
62232e45 DW |
707 | }, |
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 | }, | |
31eca76b DW |
738 | [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 | }, | |
62232e45 DW |
744 | }; |
745 | ||
746 | const 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 | } | |
752 | EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc); | |
753 | ||
754 | static 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, }, | |
4577b066 DW |
759 | .out_num = 4, |
760 | .out_sizes = { 4, 4, 4, 4, }, | |
62232e45 DW |
761 | }, |
762 | [ND_CMD_ARS_START] = { | |
4577b066 DW |
763 | .in_num = 5, |
764 | .in_sizes = { 8, 8, 2, 1, 5, }, | |
765 | .out_num = 2, | |
766 | .out_sizes = { 4, 4, }, | |
62232e45 DW |
767 | }, |
768 | [ND_CMD_ARS_STATUS] = { | |
747ffe11 DW |
769 | .out_num = 3, |
770 | .out_sizes = { 4, 4, UINT_MAX, }, | |
62232e45 | 771 | }, |
d4f32367 DW |
772 | [ND_CMD_CLEAR_ERROR] = { |
773 | .in_num = 2, | |
774 | .in_sizes = { 8, 8, }, | |
775 | .out_num = 3, | |
776 | .out_sizes = { 4, 4, 8, }, | |
777 | }, | |
31eca76b DW |
778 | [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 | }, | |
62232e45 DW |
784 | }; |
785 | ||
786 | const 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 | } | |
792 | EXPORT_SYMBOL_GPL(nd_cmd_bus_desc); | |
793 | ||
794 | u32 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; | |
31eca76b DW |
811 | } else if (cmd == ND_CMD_CALL) { |
812 | struct nd_cmd_pkg *pkg = buf; | |
813 | ||
814 | return pkg->nd_size_in; | |
62232e45 DW |
815 | } |
816 | ||
817 | return UINT_MAX; | |
818 | } | |
819 | EXPORT_SYMBOL_GPL(nd_cmd_in_size); | |
820 | ||
821 | u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd, | |
822 | const struct nd_cmd_desc *desc, int idx, const u32 *in_field, | |
efda1b5d | 823 | const u32 *out_field, unsigned long remainder) |
62232e45 DW |
824 | { |
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]; | |
efda1b5d DW |
835 | 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 | */ | |
286e8771 | 849 | if (out_field[1] - 4 == remainder) |
efda1b5d | 850 | return remainder; |
286e8771 | 851 | return out_field[1] - 8; |
efda1b5d | 852 | } else if (cmd == ND_CMD_CALL) { |
31eca76b DW |
853 | struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field; |
854 | ||
855 | return pkg->nd_size_out; | |
856 | } | |
857 | ||
62232e45 DW |
858 | |
859 | return UINT_MAX; | |
860 | } | |
861 | EXPORT_SYMBOL_GPL(nd_cmd_out_size); | |
862 | ||
bf9bccc1 | 863 | void wait_nvdimm_bus_probe_idle(struct device *dev) |
eaf96153 | 864 | { |
bf9bccc1 DW |
865 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
866 | ||
eaf96153 DW |
867 | 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 | ||
006358b3 | 877 | static int nd_pmem_forget_poison_check(struct device *dev, void *data) |
d4f32367 | 878 | { |
006358b3 DJ |
879 | 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)) | |
d4f32367 | 912 | return -EBUSY; |
006358b3 | 913 | |
d4f32367 | 914 | return 0; |
006358b3 DJ |
915 | |
916 | } | |
917 | ||
918 | static 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); | |
d4f32367 DW |
921 | } |
922 | ||
eaf96153 | 923 | /* set_config requires an idle interleave set */ |
87bf572e | 924 | static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus, |
006358b3 | 925 | struct nvdimm *nvdimm, unsigned int cmd, void *data) |
eaf96153 | 926 | { |
87bf572e DW |
927 | 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) { | |
b3ed2ce0 | 931 | int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd, data); |
87bf572e DW |
932 | |
933 | if (rc) | |
934 | return rc; | |
935 | } | |
eaf96153 | 936 | |
d4f32367 DW |
937 | /* require clear error to go through the pmem driver */ |
938 | if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR) | |
006358b3 DJ |
939 | return device_for_each_child(&nvdimm_bus->dev, data, |
940 | nd_ns_forget_poison_check); | |
d4f32367 | 941 | |
eaf96153 DW |
942 | if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA) |
943 | return 0; | |
944 | ||
87bf572e | 945 | /* prevent label manipulation while the kernel owns label updates */ |
bf9bccc1 | 946 | wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev); |
eaf96153 DW |
947 | if (atomic_read(&nvdimm->busy)) |
948 | return -EBUSY; | |
949 | return 0; | |
950 | } | |
951 | ||
62232e45 DW |
952 | static 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; | |
62232e45 DW |
956 | 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); | |
62232e45 | 960 | struct device *dev = &nvdimm_bus->dev; |
58738c49 | 961 | void __user *p = (void __user *) arg; |
62232e45 | 962 | const char *cmd_name, *dimm_name; |
58738c49 DW |
963 | u32 in_len = 0, out_len = 0; |
964 | unsigned int func = cmd; | |
e3654eca | 965 | unsigned long cmd_mask; |
58738c49 | 966 | struct nd_cmd_pkg pkg; |
006358b3 | 967 | int rc, i, cmd_rc; |
58738c49 DW |
968 | u64 buf_len = 0; |
969 | void *buf; | |
62232e45 DW |
970 | |
971 | if (nvdimm) { | |
972 | desc = nd_cmd_dimm_desc(cmd); | |
973 | cmd_name = nvdimm_cmd_name(cmd); | |
e3654eca | 974 | cmd_mask = nvdimm->cmd_mask; |
62232e45 DW |
975 | dimm_name = dev_name(&nvdimm->dev); |
976 | } else { | |
977 | desc = nd_cmd_bus_desc(cmd); | |
978 | cmd_name = nvdimm_bus_cmd_name(cmd); | |
e3654eca | 979 | cmd_mask = nd_desc->cmd_mask; |
62232e45 DW |
980 | dimm_name = "bus"; |
981 | } | |
982 | ||
31eca76b DW |
983 | if (cmd == ND_CMD_CALL) { |
984 | if (copy_from_user(&pkg, p, sizeof(pkg))) | |
985 | return -EFAULT; | |
986 | } | |
987 | ||
62232e45 | 988 | if (!desc || (desc->out_num + desc->in_num == 0) || |
e3654eca | 989 | !test_bit(cmd, &cmd_mask)) |
62232e45 DW |
990 | return -ENOTTY; |
991 | ||
992 | /* fail write commands (when read-only) */ | |
993 | if (read_only) | |
07accfa9 JH |
994 | switch (cmd) { |
995 | case ND_CMD_VENDOR: | |
996 | case ND_CMD_SET_CONFIG_DATA: | |
997 | case ND_CMD_ARS_START: | |
d4f32367 | 998 | case ND_CMD_CLEAR_ERROR: |
31eca76b | 999 | case ND_CMD_CALL: |
62232e45 DW |
1000 | 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 | } | |
62232e45 DW |
1018 | 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 | ||
31eca76b | 1027 | if (cmd == ND_CMD_CALL) { |
53b85a44 | 1028 | func = pkg.nd_command; |
426824d6 DW |
1029 | dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n", |
1030 | dimm_name, pkg.nd_command, | |
31eca76b | 1031 | in_len, out_len, buf_len); |
31eca76b DW |
1032 | } |
1033 | ||
62232e45 DW |
1034 | /* 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, | |
efda1b5d | 1037 | (u32 *) in_env, (u32 *) out_env, 0); |
62232e45 DW |
1038 | u32 copy; |
1039 | ||
1040 | if (out_size == UINT_MAX) { | |
426824d6 DW |
1041 | dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n", |
1042 | dimm_name, cmd_name, i); | |
62232e45 DW |
1043 | return -EFAULT; |
1044 | } | |
62232e45 DW |
1045 | 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 | ||
58738c49 | 1055 | buf_len = (u64) out_len + (u64) in_len; |
62232e45 | 1056 | if (buf_len > ND_IOCTL_MAX_BUFLEN) { |
426824d6 DW |
1057 | dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name, |
1058 | cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN); | |
62232e45 DW |
1059 | 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 | ||
eaf96153 | 1071 | nvdimm_bus_lock(&nvdimm_bus->dev); |
53b85a44 | 1072 | rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf); |
eaf96153 DW |
1073 | if (rc) |
1074 | goto out_unlock; | |
1075 | ||
006358b3 | 1076 | rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc); |
62232e45 | 1077 | if (rc < 0) |
eaf96153 | 1078 | goto out_unlock; |
006358b3 DJ |
1079 | |
1080 | if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) { | |
1081 | struct nd_cmd_clear_error *clear_err = buf; | |
006358b3 | 1082 | |
23f49844 DW |
1083 | nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address, |
1084 | clear_err->cleared); | |
006358b3 | 1085 | } |
0beb2012 DW |
1086 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
1087 | ||
62232e45 DW |
1088 | if (copy_to_user(p, buf, buf_len)) |
1089 | rc = -EFAULT; | |
0beb2012 DW |
1090 | |
1091 | vfree(buf); | |
1092 | return rc; | |
1093 | ||
eaf96153 DW |
1094 | out_unlock: |
1095 | nvdimm_bus_unlock(&nvdimm_bus->dev); | |
62232e45 DW |
1096 | out: |
1097 | vfree(buf); | |
1098 | return rc; | |
1099 | } | |
1100 | ||
45def22c DW |
1101 | static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
1102 | { | |
62232e45 | 1103 | long id = (long) file->private_data; |
4dc0e7be | 1104 | int rc = -ENXIO, ro; |
62232e45 DW |
1105 | struct nvdimm_bus *nvdimm_bus; |
1106 | ||
4dc0e7be | 1107 | ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); |
62232e45 DW |
1108 | mutex_lock(&nvdimm_bus_list_mutex); |
1109 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { | |
1110 | if (nvdimm_bus->id == id) { | |
4dc0e7be | 1111 | rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg); |
62232e45 DW |
1112 | break; |
1113 | } | |
1114 | } | |
1115 | mutex_unlock(&nvdimm_bus_list_mutex); | |
1116 | ||
1117 | return rc; | |
1118 | } | |
1119 | ||
1120 | static 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 | ||
1133 | static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
1134 | { | |
4dc0e7be | 1135 | int rc = -ENXIO, ro; |
62232e45 DW |
1136 | struct nvdimm_bus *nvdimm_bus; |
1137 | ||
4dc0e7be | 1138 | ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); |
62232e45 DW |
1139 | 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); | |
4dc0e7be | 1149 | rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg); |
62232e45 DW |
1150 | put_device(dev); |
1151 | break; | |
1152 | } | |
1153 | mutex_unlock(&nvdimm_bus_list_mutex); | |
1154 | ||
1155 | return rc; | |
1156 | } | |
1157 | ||
1158 | static 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; | |
45def22c DW |
1164 | } |
1165 | ||
1166 | static const struct file_operations nvdimm_bus_fops = { | |
1167 | .owner = THIS_MODULE, | |
62232e45 | 1168 | .open = nd_open, |
45def22c DW |
1169 | .unlocked_ioctl = nd_ioctl, |
1170 | .compat_ioctl = nd_ioctl, | |
1171 | .llseek = noop_llseek, | |
1172 | }; | |
1173 | ||
62232e45 DW |
1174 | static 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 | ||
45def22c DW |
1182 | int __init nvdimm_bus_init(void) |
1183 | { | |
1184 | int rc; | |
1185 | ||
e6dfb2de DW |
1186 | rc = bus_register(&nvdimm_bus_type); |
1187 | if (rc) | |
1188 | return rc; | |
1189 | ||
45def22c DW |
1190 | rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops); |
1191 | if (rc < 0) | |
62232e45 | 1192 | goto err_bus_chrdev; |
45def22c DW |
1193 | nvdimm_bus_major = rc; |
1194 | ||
62232e45 DW |
1195 | rc = register_chrdev(0, "dimmctl", &nvdimm_fops); |
1196 | if (rc < 0) | |
1197 | goto err_dimm_chrdev; | |
1198 | nvdimm_major = rc; | |
1199 | ||
45def22c | 1200 | nd_class = class_create(THIS_MODULE, "nd"); |
daa1dee4 AL |
1201 | if (IS_ERR(nd_class)) { |
1202 | rc = PTR_ERR(nd_class); | |
45def22c | 1203 | goto err_class; |
daa1dee4 | 1204 | } |
45def22c | 1205 | |
18515942 DW |
1206 | rc = driver_register(&nd_bus_driver.drv); |
1207 | if (rc) | |
1208 | goto err_nd_bus; | |
1209 | ||
45def22c DW |
1210 | return 0; |
1211 | ||
18515942 DW |
1212 | err_nd_bus: |
1213 | class_destroy(nd_class); | |
45def22c | 1214 | err_class: |
62232e45 DW |
1215 | unregister_chrdev(nvdimm_major, "dimmctl"); |
1216 | err_dimm_chrdev: | |
45def22c | 1217 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
62232e45 | 1218 | err_bus_chrdev: |
e6dfb2de | 1219 | bus_unregister(&nvdimm_bus_type); |
45def22c DW |
1220 | |
1221 | return rc; | |
1222 | } | |
1223 | ||
4d88a97a | 1224 | void nvdimm_bus_exit(void) |
45def22c | 1225 | { |
18515942 | 1226 | driver_unregister(&nd_bus_driver.drv); |
45def22c DW |
1227 | class_destroy(nd_class); |
1228 | unregister_chrdev(nvdimm_bus_major, "ndctl"); | |
62232e45 | 1229 | unregister_chrdev(nvdimm_major, "dimmctl"); |
e6dfb2de | 1230 | bus_unregister(&nvdimm_bus_type); |
18515942 | 1231 | ida_destroy(&nd_ida); |
45def22c | 1232 | } |