]>
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 | |
62232e45 | 14 | #include <linux/vmalloc.h> |
45def22c | 15 | #include <linux/uaccess.h> |
3d88002e | 16 | #include <linux/module.h> |
45def22c | 17 | #include <linux/fcntl.h> |
e6dfb2de | 18 | #include <linux/async.h> |
62232e45 | 19 | #include <linux/ndctl.h> |
4d88a97a | 20 | #include <linux/sched.h> |
45def22c DW |
21 | #include <linux/slab.h> |
22 | #include <linux/fs.h> | |
23 | #include <linux/io.h> | |
62232e45 | 24 | #include <linux/mm.h> |
4d88a97a | 25 | #include <linux/nd.h> |
45def22c | 26 | #include "nd-core.h" |
4d88a97a | 27 | #include "nd.h" |
45def22c | 28 | |
62232e45 | 29 | int nvdimm_major; |
45def22c DW |
30 | static int nvdimm_bus_major; |
31 | static struct class *nd_class; | |
32 | ||
4d88a97a DW |
33 | static int to_nd_device_type(struct device *dev) |
34 | { | |
35 | if (is_nvdimm(dev)) | |
36 | return ND_DEVICE_DIMM; | |
3d88002e DW |
37 | else if (is_nd_pmem(dev)) |
38 | return ND_DEVICE_REGION_PMEM; | |
39 | else if (is_nd_blk(dev)) | |
40 | return ND_DEVICE_REGION_BLK; | |
41 | else if (is_nd_pmem(dev->parent) || is_nd_blk(dev->parent)) | |
42 | return nd_region_to_nstype(to_nd_region(dev->parent)); | |
4d88a97a DW |
43 | |
44 | return 0; | |
45 | } | |
46 | ||
47 | static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env) | |
48 | { | |
49 | return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT, | |
50 | to_nd_device_type(dev)); | |
51 | } | |
52 | ||
53 | static int nvdimm_bus_match(struct device *dev, struct device_driver *drv) | |
54 | { | |
55 | struct nd_device_driver *nd_drv = to_nd_device_driver(drv); | |
56 | ||
57 | return test_bit(to_nd_device_type(dev), &nd_drv->type); | |
58 | } | |
59 | ||
3d88002e DW |
60 | static struct module *to_bus_provider(struct device *dev) |
61 | { | |
62 | /* pin bus providers while regions are enabled */ | |
63 | if (is_nd_pmem(dev) || is_nd_blk(dev)) { | |
64 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); | |
65 | ||
66 | return nvdimm_bus->module; | |
67 | } | |
68 | return NULL; | |
69 | } | |
70 | ||
eaf96153 DW |
71 | static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus) |
72 | { | |
73 | nvdimm_bus_lock(&nvdimm_bus->dev); | |
74 | nvdimm_bus->probe_active++; | |
75 | nvdimm_bus_unlock(&nvdimm_bus->dev); | |
76 | } | |
77 | ||
78 | static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus) | |
79 | { | |
80 | nvdimm_bus_lock(&nvdimm_bus->dev); | |
81 | if (--nvdimm_bus->probe_active == 0) | |
82 | wake_up(&nvdimm_bus->probe_wait); | |
83 | nvdimm_bus_unlock(&nvdimm_bus->dev); | |
84 | } | |
85 | ||
4d88a97a DW |
86 | static int nvdimm_bus_probe(struct device *dev) |
87 | { | |
88 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); | |
3d88002e | 89 | struct module *provider = to_bus_provider(dev); |
4d88a97a DW |
90 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
91 | int rc; | |
92 | ||
3d88002e DW |
93 | if (!try_module_get(provider)) |
94 | return -ENXIO; | |
95 | ||
eaf96153 | 96 | nvdimm_bus_probe_start(nvdimm_bus); |
4d88a97a | 97 | rc = nd_drv->probe(dev); |
eaf96153 DW |
98 | if (rc == 0) |
99 | nd_region_probe_success(nvdimm_bus, dev); | |
100 | nvdimm_bus_probe_end(nvdimm_bus); | |
101 | ||
4d88a97a DW |
102 | dev_dbg(&nvdimm_bus->dev, "%s.probe(%s) = %d\n", dev->driver->name, |
103 | dev_name(dev), rc); | |
3d88002e DW |
104 | if (rc != 0) |
105 | module_put(provider); | |
4d88a97a DW |
106 | return rc; |
107 | } | |
108 | ||
109 | static int nvdimm_bus_remove(struct device *dev) | |
110 | { | |
111 | struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver); | |
3d88002e | 112 | struct module *provider = to_bus_provider(dev); |
4d88a97a DW |
113 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
114 | int rc; | |
115 | ||
116 | rc = nd_drv->remove(dev); | |
eaf96153 DW |
117 | nd_region_disable(nvdimm_bus, dev); |
118 | ||
4d88a97a DW |
119 | dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name, |
120 | dev_name(dev), rc); | |
3d88002e | 121 | module_put(provider); |
4d88a97a DW |
122 | return rc; |
123 | } | |
124 | ||
125 | static struct bus_type nvdimm_bus_type = { | |
e6dfb2de | 126 | .name = "nd", |
4d88a97a DW |
127 | .uevent = nvdimm_bus_uevent, |
128 | .match = nvdimm_bus_match, | |
129 | .probe = nvdimm_bus_probe, | |
130 | .remove = nvdimm_bus_remove, | |
131 | }; | |
132 | ||
133 | static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain); | |
134 | ||
135 | void nd_synchronize(void) | |
136 | { | |
137 | async_synchronize_full_domain(&nd_async_domain); | |
138 | } | |
139 | EXPORT_SYMBOL_GPL(nd_synchronize); | |
140 | ||
141 | static void nd_async_device_register(void *d, async_cookie_t cookie) | |
142 | { | |
143 | struct device *dev = d; | |
144 | ||
145 | if (device_add(dev) != 0) { | |
146 | dev_err(dev, "%s: failed\n", __func__); | |
147 | put_device(dev); | |
148 | } | |
149 | put_device(dev); | |
150 | } | |
151 | ||
152 | static void nd_async_device_unregister(void *d, async_cookie_t cookie) | |
153 | { | |
154 | struct device *dev = d; | |
155 | ||
156 | device_unregister(dev); | |
157 | put_device(dev); | |
158 | } | |
159 | ||
160 | void nd_device_register(struct device *dev) | |
161 | { | |
162 | dev->bus = &nvdimm_bus_type; | |
163 | device_initialize(dev); | |
164 | get_device(dev); | |
165 | async_schedule_domain(nd_async_device_register, dev, | |
166 | &nd_async_domain); | |
167 | } | |
168 | EXPORT_SYMBOL(nd_device_register); | |
169 | ||
170 | void nd_device_unregister(struct device *dev, enum nd_async_mode mode) | |
171 | { | |
172 | switch (mode) { | |
173 | case ND_ASYNC: | |
174 | get_device(dev); | |
175 | async_schedule_domain(nd_async_device_unregister, dev, | |
176 | &nd_async_domain); | |
177 | break; | |
178 | case ND_SYNC: | |
179 | nd_synchronize(); | |
180 | device_unregister(dev); | |
181 | break; | |
182 | } | |
183 | } | |
184 | EXPORT_SYMBOL(nd_device_unregister); | |
185 | ||
186 | /** | |
187 | * __nd_driver_register() - register a region or a namespace driver | |
188 | * @nd_drv: driver to register | |
189 | * @owner: automatically set by nd_driver_register() macro | |
190 | * @mod_name: automatically set by nd_driver_register() macro | |
191 | */ | |
192 | int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner, | |
193 | const char *mod_name) | |
194 | { | |
195 | struct device_driver *drv = &nd_drv->drv; | |
196 | ||
197 | if (!nd_drv->type) { | |
198 | pr_debug("driver type bitmask not set (%pf)\n", | |
199 | __builtin_return_address(0)); | |
200 | return -EINVAL; | |
201 | } | |
202 | ||
203 | if (!nd_drv->probe || !nd_drv->remove) { | |
204 | pr_debug("->probe() and ->remove() must be specified\n"); | |
205 | return -EINVAL; | |
206 | } | |
207 | ||
208 | drv->bus = &nvdimm_bus_type; | |
209 | drv->owner = owner; | |
210 | drv->mod_name = mod_name; | |
211 | ||
212 | return driver_register(drv); | |
213 | } | |
214 | EXPORT_SYMBOL(__nd_driver_register); | |
215 | ||
216 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, | |
217 | char *buf) | |
218 | { | |
219 | return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n", | |
220 | to_nd_device_type(dev)); | |
221 | } | |
222 | static DEVICE_ATTR_RO(modalias); | |
223 | ||
224 | static ssize_t devtype_show(struct device *dev, struct device_attribute *attr, | |
225 | char *buf) | |
226 | { | |
227 | return sprintf(buf, "%s\n", dev->type->name); | |
228 | } | |
229 | static DEVICE_ATTR_RO(devtype); | |
230 | ||
231 | static struct attribute *nd_device_attributes[] = { | |
232 | &dev_attr_modalias.attr, | |
233 | &dev_attr_devtype.attr, | |
234 | NULL, | |
235 | }; | |
236 | ||
237 | /** | |
238 | * nd_device_attribute_group - generic attributes for all devices on an nd bus | |
239 | */ | |
240 | struct attribute_group nd_device_attribute_group = { | |
241 | .attrs = nd_device_attributes, | |
e6dfb2de | 242 | }; |
4d88a97a | 243 | EXPORT_SYMBOL_GPL(nd_device_attribute_group); |
e6dfb2de | 244 | |
45def22c DW |
245 | int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus) |
246 | { | |
247 | dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id); | |
248 | struct device *dev; | |
249 | ||
250 | dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus, | |
251 | "ndctl%d", nvdimm_bus->id); | |
252 | ||
253 | if (IS_ERR(dev)) { | |
254 | dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n", | |
255 | nvdimm_bus->id, PTR_ERR(dev)); | |
256 | return PTR_ERR(dev); | |
257 | } | |
258 | return 0; | |
259 | } | |
260 | ||
261 | void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus) | |
262 | { | |
263 | device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id)); | |
264 | } | |
265 | ||
62232e45 DW |
266 | static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = { |
267 | [ND_CMD_IMPLEMENTED] = { }, | |
268 | [ND_CMD_SMART] = { | |
269 | .out_num = 2, | |
270 | .out_sizes = { 4, 8, }, | |
271 | }, | |
272 | [ND_CMD_SMART_THRESHOLD] = { | |
273 | .out_num = 2, | |
274 | .out_sizes = { 4, 8, }, | |
275 | }, | |
276 | [ND_CMD_DIMM_FLAGS] = { | |
277 | .out_num = 2, | |
278 | .out_sizes = { 4, 4 }, | |
279 | }, | |
280 | [ND_CMD_GET_CONFIG_SIZE] = { | |
281 | .out_num = 3, | |
282 | .out_sizes = { 4, 4, 4, }, | |
283 | }, | |
284 | [ND_CMD_GET_CONFIG_DATA] = { | |
285 | .in_num = 2, | |
286 | .in_sizes = { 4, 4, }, | |
287 | .out_num = 2, | |
288 | .out_sizes = { 4, UINT_MAX, }, | |
289 | }, | |
290 | [ND_CMD_SET_CONFIG_DATA] = { | |
291 | .in_num = 3, | |
292 | .in_sizes = { 4, 4, UINT_MAX, }, | |
293 | .out_num = 1, | |
294 | .out_sizes = { 4, }, | |
295 | }, | |
296 | [ND_CMD_VENDOR] = { | |
297 | .in_num = 3, | |
298 | .in_sizes = { 4, 4, UINT_MAX, }, | |
299 | .out_num = 3, | |
300 | .out_sizes = { 4, 4, UINT_MAX, }, | |
301 | }, | |
302 | }; | |
303 | ||
304 | const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd) | |
305 | { | |
306 | if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs)) | |
307 | return &__nd_cmd_dimm_descs[cmd]; | |
308 | return NULL; | |
309 | } | |
310 | EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc); | |
311 | ||
312 | static const struct nd_cmd_desc __nd_cmd_bus_descs[] = { | |
313 | [ND_CMD_IMPLEMENTED] = { }, | |
314 | [ND_CMD_ARS_CAP] = { | |
315 | .in_num = 2, | |
316 | .in_sizes = { 8, 8, }, | |
317 | .out_num = 2, | |
318 | .out_sizes = { 4, 4, }, | |
319 | }, | |
320 | [ND_CMD_ARS_START] = { | |
321 | .in_num = 4, | |
322 | .in_sizes = { 8, 8, 2, 6, }, | |
323 | .out_num = 1, | |
324 | .out_sizes = { 4, }, | |
325 | }, | |
326 | [ND_CMD_ARS_STATUS] = { | |
327 | .out_num = 2, | |
328 | .out_sizes = { 4, UINT_MAX, }, | |
329 | }, | |
330 | }; | |
331 | ||
332 | const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd) | |
333 | { | |
334 | if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs)) | |
335 | return &__nd_cmd_bus_descs[cmd]; | |
336 | return NULL; | |
337 | } | |
338 | EXPORT_SYMBOL_GPL(nd_cmd_bus_desc); | |
339 | ||
340 | u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd, | |
341 | const struct nd_cmd_desc *desc, int idx, void *buf) | |
342 | { | |
343 | if (idx >= desc->in_num) | |
344 | return UINT_MAX; | |
345 | ||
346 | if (desc->in_sizes[idx] < UINT_MAX) | |
347 | return desc->in_sizes[idx]; | |
348 | ||
349 | if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) { | |
350 | struct nd_cmd_set_config_hdr *hdr = buf; | |
351 | ||
352 | return hdr->in_length; | |
353 | } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) { | |
354 | struct nd_cmd_vendor_hdr *hdr = buf; | |
355 | ||
356 | return hdr->in_length; | |
357 | } | |
358 | ||
359 | return UINT_MAX; | |
360 | } | |
361 | EXPORT_SYMBOL_GPL(nd_cmd_in_size); | |
362 | ||
363 | u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd, | |
364 | const struct nd_cmd_desc *desc, int idx, const u32 *in_field, | |
365 | const u32 *out_field) | |
366 | { | |
367 | if (idx >= desc->out_num) | |
368 | return UINT_MAX; | |
369 | ||
370 | if (desc->out_sizes[idx] < UINT_MAX) | |
371 | return desc->out_sizes[idx]; | |
372 | ||
373 | if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1) | |
374 | return in_field[1]; | |
375 | else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) | |
376 | return out_field[1]; | |
377 | else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 1) | |
378 | return ND_CMD_ARS_STATUS_MAX; | |
379 | ||
380 | return UINT_MAX; | |
381 | } | |
382 | EXPORT_SYMBOL_GPL(nd_cmd_out_size); | |
383 | ||
eaf96153 DW |
384 | static void wait_nvdimm_bus_probe_idle(struct nvdimm_bus *nvdimm_bus) |
385 | { | |
386 | do { | |
387 | if (nvdimm_bus->probe_active == 0) | |
388 | break; | |
389 | nvdimm_bus_unlock(&nvdimm_bus->dev); | |
390 | wait_event(nvdimm_bus->probe_wait, | |
391 | nvdimm_bus->probe_active == 0); | |
392 | nvdimm_bus_lock(&nvdimm_bus->dev); | |
393 | } while (true); | |
394 | } | |
395 | ||
396 | /* set_config requires an idle interleave set */ | |
397 | static int nd_cmd_clear_to_send(struct nvdimm *nvdimm, unsigned int cmd) | |
398 | { | |
399 | struct nvdimm_bus *nvdimm_bus; | |
400 | ||
401 | if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA) | |
402 | return 0; | |
403 | ||
404 | nvdimm_bus = walk_to_nvdimm_bus(&nvdimm->dev); | |
405 | wait_nvdimm_bus_probe_idle(nvdimm_bus); | |
406 | ||
407 | if (atomic_read(&nvdimm->busy)) | |
408 | return -EBUSY; | |
409 | return 0; | |
410 | } | |
411 | ||
62232e45 DW |
412 | static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, |
413 | int read_only, unsigned int ioctl_cmd, unsigned long arg) | |
414 | { | |
415 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; | |
416 | size_t buf_len = 0, in_len = 0, out_len = 0; | |
417 | static char out_env[ND_CMD_MAX_ENVELOPE]; | |
418 | static char in_env[ND_CMD_MAX_ENVELOPE]; | |
419 | const struct nd_cmd_desc *desc = NULL; | |
420 | unsigned int cmd = _IOC_NR(ioctl_cmd); | |
421 | void __user *p = (void __user *) arg; | |
422 | struct device *dev = &nvdimm_bus->dev; | |
423 | const char *cmd_name, *dimm_name; | |
424 | unsigned long dsm_mask; | |
425 | void *buf; | |
426 | int rc, i; | |
427 | ||
428 | if (nvdimm) { | |
429 | desc = nd_cmd_dimm_desc(cmd); | |
430 | cmd_name = nvdimm_cmd_name(cmd); | |
431 | dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0; | |
432 | dimm_name = dev_name(&nvdimm->dev); | |
433 | } else { | |
434 | desc = nd_cmd_bus_desc(cmd); | |
435 | cmd_name = nvdimm_bus_cmd_name(cmd); | |
436 | dsm_mask = nd_desc->dsm_mask; | |
437 | dimm_name = "bus"; | |
438 | } | |
439 | ||
440 | if (!desc || (desc->out_num + desc->in_num == 0) || | |
441 | !test_bit(cmd, &dsm_mask)) | |
442 | return -ENOTTY; | |
443 | ||
444 | /* fail write commands (when read-only) */ | |
445 | if (read_only) | |
446 | switch (ioctl_cmd) { | |
447 | case ND_IOCTL_VENDOR: | |
448 | case ND_IOCTL_SET_CONFIG_DATA: | |
449 | case ND_IOCTL_ARS_START: | |
450 | dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n", | |
451 | nvdimm ? nvdimm_cmd_name(cmd) | |
452 | : nvdimm_bus_cmd_name(cmd)); | |
453 | return -EPERM; | |
454 | default: | |
455 | break; | |
456 | } | |
457 | ||
458 | /* process an input envelope */ | |
459 | for (i = 0; i < desc->in_num; i++) { | |
460 | u32 in_size, copy; | |
461 | ||
462 | in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env); | |
463 | if (in_size == UINT_MAX) { | |
464 | dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n", | |
465 | __func__, dimm_name, cmd_name, i); | |
466 | return -ENXIO; | |
467 | } | |
468 | if (!access_ok(VERIFY_READ, p + in_len, in_size)) | |
469 | return -EFAULT; | |
470 | if (in_len < sizeof(in_env)) | |
471 | copy = min_t(u32, sizeof(in_env) - in_len, in_size); | |
472 | else | |
473 | copy = 0; | |
474 | if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) | |
475 | return -EFAULT; | |
476 | in_len += in_size; | |
477 | } | |
478 | ||
479 | /* process an output envelope */ | |
480 | for (i = 0; i < desc->out_num; i++) { | |
481 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, | |
482 | (u32 *) in_env, (u32 *) out_env); | |
483 | u32 copy; | |
484 | ||
485 | if (out_size == UINT_MAX) { | |
486 | dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n", | |
487 | __func__, dimm_name, cmd_name, i); | |
488 | return -EFAULT; | |
489 | } | |
490 | if (!access_ok(VERIFY_WRITE, p + in_len + out_len, out_size)) | |
491 | return -EFAULT; | |
492 | if (out_len < sizeof(out_env)) | |
493 | copy = min_t(u32, sizeof(out_env) - out_len, out_size); | |
494 | else | |
495 | copy = 0; | |
496 | if (copy && copy_from_user(&out_env[out_len], | |
497 | p + in_len + out_len, copy)) | |
498 | return -EFAULT; | |
499 | out_len += out_size; | |
500 | } | |
501 | ||
502 | buf_len = out_len + in_len; | |
503 | if (!access_ok(VERIFY_WRITE, p, sizeof(buf_len))) | |
504 | return -EFAULT; | |
505 | ||
506 | if (buf_len > ND_IOCTL_MAX_BUFLEN) { | |
507 | dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__, | |
508 | dimm_name, cmd_name, buf_len, | |
509 | ND_IOCTL_MAX_BUFLEN); | |
510 | return -EINVAL; | |
511 | } | |
512 | ||
513 | buf = vmalloc(buf_len); | |
514 | if (!buf) | |
515 | return -ENOMEM; | |
516 | ||
517 | if (copy_from_user(buf, p, buf_len)) { | |
518 | rc = -EFAULT; | |
519 | goto out; | |
520 | } | |
521 | ||
eaf96153 DW |
522 | nvdimm_bus_lock(&nvdimm_bus->dev); |
523 | rc = nd_cmd_clear_to_send(nvdimm, cmd); | |
524 | if (rc) | |
525 | goto out_unlock; | |
526 | ||
62232e45 DW |
527 | rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len); |
528 | if (rc < 0) | |
eaf96153 | 529 | goto out_unlock; |
62232e45 DW |
530 | if (copy_to_user(p, buf, buf_len)) |
531 | rc = -EFAULT; | |
eaf96153 DW |
532 | out_unlock: |
533 | nvdimm_bus_unlock(&nvdimm_bus->dev); | |
62232e45 DW |
534 | out: |
535 | vfree(buf); | |
536 | return rc; | |
537 | } | |
538 | ||
45def22c DW |
539 | static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
540 | { | |
62232e45 DW |
541 | long id = (long) file->private_data; |
542 | int rc = -ENXIO, read_only; | |
543 | struct nvdimm_bus *nvdimm_bus; | |
544 | ||
545 | read_only = (O_RDWR != (file->f_flags & O_ACCMODE)); | |
546 | mutex_lock(&nvdimm_bus_list_mutex); | |
547 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { | |
548 | if (nvdimm_bus->id == id) { | |
549 | rc = __nd_ioctl(nvdimm_bus, NULL, read_only, cmd, arg); | |
550 | break; | |
551 | } | |
552 | } | |
553 | mutex_unlock(&nvdimm_bus_list_mutex); | |
554 | ||
555 | return rc; | |
556 | } | |
557 | ||
558 | static int match_dimm(struct device *dev, void *data) | |
559 | { | |
560 | long id = (long) data; | |
561 | ||
562 | if (is_nvdimm(dev)) { | |
563 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
564 | ||
565 | return nvdimm->id == id; | |
566 | } | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
571 | static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
572 | { | |
573 | int rc = -ENXIO, read_only; | |
574 | struct nvdimm_bus *nvdimm_bus; | |
575 | ||
576 | read_only = (O_RDWR != (file->f_flags & O_ACCMODE)); | |
577 | mutex_lock(&nvdimm_bus_list_mutex); | |
578 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { | |
579 | struct device *dev = device_find_child(&nvdimm_bus->dev, | |
580 | file->private_data, match_dimm); | |
581 | struct nvdimm *nvdimm; | |
582 | ||
583 | if (!dev) | |
584 | continue; | |
585 | ||
586 | nvdimm = to_nvdimm(dev); | |
587 | rc = __nd_ioctl(nvdimm_bus, nvdimm, read_only, cmd, arg); | |
588 | put_device(dev); | |
589 | break; | |
590 | } | |
591 | mutex_unlock(&nvdimm_bus_list_mutex); | |
592 | ||
593 | return rc; | |
594 | } | |
595 | ||
596 | static int nd_open(struct inode *inode, struct file *file) | |
597 | { | |
598 | long minor = iminor(inode); | |
599 | ||
600 | file->private_data = (void *) minor; | |
601 | return 0; | |
45def22c DW |
602 | } |
603 | ||
604 | static const struct file_operations nvdimm_bus_fops = { | |
605 | .owner = THIS_MODULE, | |
62232e45 | 606 | .open = nd_open, |
45def22c DW |
607 | .unlocked_ioctl = nd_ioctl, |
608 | .compat_ioctl = nd_ioctl, | |
609 | .llseek = noop_llseek, | |
610 | }; | |
611 | ||
62232e45 DW |
612 | static const struct file_operations nvdimm_fops = { |
613 | .owner = THIS_MODULE, | |
614 | .open = nd_open, | |
615 | .unlocked_ioctl = nvdimm_ioctl, | |
616 | .compat_ioctl = nvdimm_ioctl, | |
617 | .llseek = noop_llseek, | |
618 | }; | |
619 | ||
45def22c DW |
620 | int __init nvdimm_bus_init(void) |
621 | { | |
622 | int rc; | |
623 | ||
e6dfb2de DW |
624 | rc = bus_register(&nvdimm_bus_type); |
625 | if (rc) | |
626 | return rc; | |
627 | ||
45def22c DW |
628 | rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops); |
629 | if (rc < 0) | |
62232e45 | 630 | goto err_bus_chrdev; |
45def22c DW |
631 | nvdimm_bus_major = rc; |
632 | ||
62232e45 DW |
633 | rc = register_chrdev(0, "dimmctl", &nvdimm_fops); |
634 | if (rc < 0) | |
635 | goto err_dimm_chrdev; | |
636 | nvdimm_major = rc; | |
637 | ||
45def22c DW |
638 | nd_class = class_create(THIS_MODULE, "nd"); |
639 | if (IS_ERR(nd_class)) | |
640 | goto err_class; | |
641 | ||
642 | return 0; | |
643 | ||
644 | err_class: | |
62232e45 DW |
645 | unregister_chrdev(nvdimm_major, "dimmctl"); |
646 | err_dimm_chrdev: | |
45def22c | 647 | unregister_chrdev(nvdimm_bus_major, "ndctl"); |
62232e45 | 648 | err_bus_chrdev: |
e6dfb2de | 649 | bus_unregister(&nvdimm_bus_type); |
45def22c DW |
650 | |
651 | return rc; | |
652 | } | |
653 | ||
4d88a97a | 654 | void nvdimm_bus_exit(void) |
45def22c DW |
655 | { |
656 | class_destroy(nd_class); | |
657 | unregister_chrdev(nvdimm_bus_major, "ndctl"); | |
62232e45 | 658 | unregister_chrdev(nvdimm_major, "dimmctl"); |
e6dfb2de | 659 | bus_unregister(&nvdimm_bus_type); |
45def22c | 660 | } |