bus.c 17 KB

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