bus.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. if (dev->parent)
  139. put_device(dev->parent);
  140. }
  141. static void nd_async_device_unregister(void *d, async_cookie_t cookie)
  142. {
  143. struct device *dev = d;
  144. /* flush bus operations before delete */
  145. nvdimm_bus_lock(dev);
  146. nvdimm_bus_unlock(dev);
  147. device_unregister(dev);
  148. put_device(dev);
  149. }
  150. void __nd_device_register(struct device *dev)
  151. {
  152. dev->bus = &nvdimm_bus_type;
  153. if (dev->parent)
  154. get_device(dev->parent);
  155. get_device(dev);
  156. async_schedule_domain(nd_async_device_register, dev,
  157. &nd_async_domain);
  158. }
  159. void nd_device_register(struct device *dev)
  160. {
  161. device_initialize(dev);
  162. __nd_device_register(dev);
  163. }
  164. EXPORT_SYMBOL(nd_device_register);
  165. void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
  166. {
  167. switch (mode) {
  168. case ND_ASYNC:
  169. get_device(dev);
  170. async_schedule_domain(nd_async_device_unregister, dev,
  171. &nd_async_domain);
  172. break;
  173. case ND_SYNC:
  174. nd_synchronize();
  175. device_unregister(dev);
  176. break;
  177. }
  178. }
  179. EXPORT_SYMBOL(nd_device_unregister);
  180. /**
  181. * __nd_driver_register() - register a region or a namespace driver
  182. * @nd_drv: driver to register
  183. * @owner: automatically set by nd_driver_register() macro
  184. * @mod_name: automatically set by nd_driver_register() macro
  185. */
  186. int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
  187. const char *mod_name)
  188. {
  189. struct device_driver *drv = &nd_drv->drv;
  190. if (!nd_drv->type) {
  191. pr_debug("driver type bitmask not set (%pf)\n",
  192. __builtin_return_address(0));
  193. return -EINVAL;
  194. }
  195. if (!nd_drv->probe || !nd_drv->remove) {
  196. pr_debug("->probe() and ->remove() must be specified\n");
  197. return -EINVAL;
  198. }
  199. drv->bus = &nvdimm_bus_type;
  200. drv->owner = owner;
  201. drv->mod_name = mod_name;
  202. return driver_register(drv);
  203. }
  204. EXPORT_SYMBOL(__nd_driver_register);
  205. int nvdimm_revalidate_disk(struct gendisk *disk)
  206. {
  207. struct device *dev = disk->driverfs_dev;
  208. struct nd_region *nd_region = to_nd_region(dev->parent);
  209. int disk_ro = get_disk_ro(disk);
  210. /*
  211. * Upgrade to read-only if the region is read-only preserve as
  212. * read-only if the disk is already read-only.
  213. */
  214. if (disk_ro || nd_region->ro == disk_ro)
  215. return 0;
  216. dev_info(dev, "%s read-only, marking %s read-only\n",
  217. dev_name(&nd_region->dev), disk->disk_name);
  218. set_disk_ro(disk, 1);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(nvdimm_revalidate_disk);
  222. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  223. char *buf)
  224. {
  225. return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
  226. to_nd_device_type(dev));
  227. }
  228. static DEVICE_ATTR_RO(modalias);
  229. static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
  230. char *buf)
  231. {
  232. return sprintf(buf, "%s\n", dev->type->name);
  233. }
  234. static DEVICE_ATTR_RO(devtype);
  235. static struct attribute *nd_device_attributes[] = {
  236. &dev_attr_modalias.attr,
  237. &dev_attr_devtype.attr,
  238. NULL,
  239. };
  240. /**
  241. * nd_device_attribute_group - generic attributes for all devices on an nd bus
  242. */
  243. struct attribute_group nd_device_attribute_group = {
  244. .attrs = nd_device_attributes,
  245. };
  246. EXPORT_SYMBOL_GPL(nd_device_attribute_group);
  247. static ssize_t numa_node_show(struct device *dev,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. return sprintf(buf, "%d\n", dev_to_node(dev));
  251. }
  252. static DEVICE_ATTR_RO(numa_node);
  253. static struct attribute *nd_numa_attributes[] = {
  254. &dev_attr_numa_node.attr,
  255. NULL,
  256. };
  257. static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
  258. int n)
  259. {
  260. if (!IS_ENABLED(CONFIG_NUMA))
  261. return 0;
  262. return a->mode;
  263. }
  264. /**
  265. * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
  266. */
  267. struct attribute_group nd_numa_attribute_group = {
  268. .attrs = nd_numa_attributes,
  269. .is_visible = nd_numa_attr_visible,
  270. };
  271. EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
  272. int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
  273. {
  274. dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
  275. struct device *dev;
  276. dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
  277. "ndctl%d", nvdimm_bus->id);
  278. if (IS_ERR(dev)) {
  279. dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
  280. nvdimm_bus->id, PTR_ERR(dev));
  281. return PTR_ERR(dev);
  282. }
  283. return 0;
  284. }
  285. void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
  286. {
  287. device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
  288. }
  289. static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
  290. [ND_CMD_IMPLEMENTED] = { },
  291. [ND_CMD_SMART] = {
  292. .out_num = 2,
  293. .out_sizes = { 4, 128, },
  294. },
  295. [ND_CMD_SMART_THRESHOLD] = {
  296. .out_num = 2,
  297. .out_sizes = { 4, 8, },
  298. },
  299. [ND_CMD_DIMM_FLAGS] = {
  300. .out_num = 2,
  301. .out_sizes = { 4, 4 },
  302. },
  303. [ND_CMD_GET_CONFIG_SIZE] = {
  304. .out_num = 3,
  305. .out_sizes = { 4, 4, 4, },
  306. },
  307. [ND_CMD_GET_CONFIG_DATA] = {
  308. .in_num = 2,
  309. .in_sizes = { 4, 4, },
  310. .out_num = 2,
  311. .out_sizes = { 4, UINT_MAX, },
  312. },
  313. [ND_CMD_SET_CONFIG_DATA] = {
  314. .in_num = 3,
  315. .in_sizes = { 4, 4, UINT_MAX, },
  316. .out_num = 1,
  317. .out_sizes = { 4, },
  318. },
  319. [ND_CMD_VENDOR] = {
  320. .in_num = 3,
  321. .in_sizes = { 4, 4, UINT_MAX, },
  322. .out_num = 3,
  323. .out_sizes = { 4, 4, UINT_MAX, },
  324. },
  325. };
  326. const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
  327. {
  328. if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
  329. return &__nd_cmd_dimm_descs[cmd];
  330. return NULL;
  331. }
  332. EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
  333. static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
  334. [ND_CMD_IMPLEMENTED] = { },
  335. [ND_CMD_ARS_CAP] = {
  336. .in_num = 2,
  337. .in_sizes = { 8, 8, },
  338. .out_num = 2,
  339. .out_sizes = { 4, 4, },
  340. },
  341. [ND_CMD_ARS_START] = {
  342. .in_num = 4,
  343. .in_sizes = { 8, 8, 2, 6, },
  344. .out_num = 1,
  345. .out_sizes = { 4, },
  346. },
  347. [ND_CMD_ARS_STATUS] = {
  348. .out_num = 2,
  349. .out_sizes = { 4, UINT_MAX, },
  350. },
  351. };
  352. const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
  353. {
  354. if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
  355. return &__nd_cmd_bus_descs[cmd];
  356. return NULL;
  357. }
  358. EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
  359. u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
  360. const struct nd_cmd_desc *desc, int idx, void *buf)
  361. {
  362. if (idx >= desc->in_num)
  363. return UINT_MAX;
  364. if (desc->in_sizes[idx] < UINT_MAX)
  365. return desc->in_sizes[idx];
  366. if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
  367. struct nd_cmd_set_config_hdr *hdr = buf;
  368. return hdr->in_length;
  369. } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
  370. struct nd_cmd_vendor_hdr *hdr = buf;
  371. return hdr->in_length;
  372. }
  373. return UINT_MAX;
  374. }
  375. EXPORT_SYMBOL_GPL(nd_cmd_in_size);
  376. u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
  377. const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
  378. const u32 *out_field)
  379. {
  380. if (idx >= desc->out_num)
  381. return UINT_MAX;
  382. if (desc->out_sizes[idx] < UINT_MAX)
  383. return desc->out_sizes[idx];
  384. if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
  385. return in_field[1];
  386. else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
  387. return out_field[1];
  388. else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 1)
  389. return ND_CMD_ARS_STATUS_MAX;
  390. return UINT_MAX;
  391. }
  392. EXPORT_SYMBOL_GPL(nd_cmd_out_size);
  393. void wait_nvdimm_bus_probe_idle(struct device *dev)
  394. {
  395. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  396. do {
  397. if (nvdimm_bus->probe_active == 0)
  398. break;
  399. nvdimm_bus_unlock(&nvdimm_bus->dev);
  400. wait_event(nvdimm_bus->probe_wait,
  401. nvdimm_bus->probe_active == 0);
  402. nvdimm_bus_lock(&nvdimm_bus->dev);
  403. } while (true);
  404. }
  405. /* set_config requires an idle interleave set */
  406. static int nd_cmd_clear_to_send(struct nvdimm *nvdimm, unsigned int cmd)
  407. {
  408. struct nvdimm_bus *nvdimm_bus;
  409. if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
  410. return 0;
  411. nvdimm_bus = walk_to_nvdimm_bus(&nvdimm->dev);
  412. wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
  413. if (atomic_read(&nvdimm->busy))
  414. return -EBUSY;
  415. return 0;
  416. }
  417. static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
  418. int read_only, unsigned int ioctl_cmd, unsigned long arg)
  419. {
  420. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  421. size_t buf_len = 0, in_len = 0, out_len = 0;
  422. static char out_env[ND_CMD_MAX_ENVELOPE];
  423. static char in_env[ND_CMD_MAX_ENVELOPE];
  424. const struct nd_cmd_desc *desc = NULL;
  425. unsigned int cmd = _IOC_NR(ioctl_cmd);
  426. void __user *p = (void __user *) arg;
  427. struct device *dev = &nvdimm_bus->dev;
  428. const char *cmd_name, *dimm_name;
  429. unsigned long dsm_mask;
  430. void *buf;
  431. int rc, i;
  432. if (nvdimm) {
  433. desc = nd_cmd_dimm_desc(cmd);
  434. cmd_name = nvdimm_cmd_name(cmd);
  435. dsm_mask = nvdimm->dsm_mask ? *(nvdimm->dsm_mask) : 0;
  436. dimm_name = dev_name(&nvdimm->dev);
  437. } else {
  438. desc = nd_cmd_bus_desc(cmd);
  439. cmd_name = nvdimm_bus_cmd_name(cmd);
  440. dsm_mask = nd_desc->dsm_mask;
  441. dimm_name = "bus";
  442. }
  443. if (!desc || (desc->out_num + desc->in_num == 0) ||
  444. !test_bit(cmd, &dsm_mask))
  445. return -ENOTTY;
  446. /* fail write commands (when read-only) */
  447. if (read_only)
  448. switch (cmd) {
  449. case ND_CMD_VENDOR:
  450. case ND_CMD_SET_CONFIG_DATA:
  451. case ND_CMD_ARS_START:
  452. dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
  453. nvdimm ? nvdimm_cmd_name(cmd)
  454. : nvdimm_bus_cmd_name(cmd));
  455. return -EPERM;
  456. default:
  457. break;
  458. }
  459. /* process an input envelope */
  460. for (i = 0; i < desc->in_num; i++) {
  461. u32 in_size, copy;
  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 (in_len < sizeof(in_env))
  469. copy = min_t(u32, sizeof(in_env) - in_len, in_size);
  470. else
  471. copy = 0;
  472. if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
  473. return -EFAULT;
  474. in_len += in_size;
  475. }
  476. /* process an output envelope */
  477. for (i = 0; i < desc->out_num; i++) {
  478. u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
  479. (u32 *) in_env, (u32 *) out_env);
  480. u32 copy;
  481. if (out_size == UINT_MAX) {
  482. dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
  483. __func__, dimm_name, cmd_name, i);
  484. return -EFAULT;
  485. }
  486. if (out_len < sizeof(out_env))
  487. copy = min_t(u32, sizeof(out_env) - out_len, out_size);
  488. else
  489. copy = 0;
  490. if (copy && copy_from_user(&out_env[out_len],
  491. p + in_len + out_len, copy))
  492. return -EFAULT;
  493. out_len += out_size;
  494. }
  495. buf_len = out_len + in_len;
  496. if (buf_len > ND_IOCTL_MAX_BUFLEN) {
  497. dev_dbg(dev, "%s:%s cmd: %s buf_len: %zu > %d\n", __func__,
  498. dimm_name, cmd_name, buf_len,
  499. ND_IOCTL_MAX_BUFLEN);
  500. return -EINVAL;
  501. }
  502. buf = vmalloc(buf_len);
  503. if (!buf)
  504. return -ENOMEM;
  505. if (copy_from_user(buf, p, buf_len)) {
  506. rc = -EFAULT;
  507. goto out;
  508. }
  509. nvdimm_bus_lock(&nvdimm_bus->dev);
  510. rc = nd_cmd_clear_to_send(nvdimm, cmd);
  511. if (rc)
  512. goto out_unlock;
  513. rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len);
  514. if (rc < 0)
  515. goto out_unlock;
  516. nvdimm_bus_unlock(&nvdimm_bus->dev);
  517. if (copy_to_user(p, buf, buf_len))
  518. rc = -EFAULT;
  519. vfree(buf);
  520. return rc;
  521. out_unlock:
  522. nvdimm_bus_unlock(&nvdimm_bus->dev);
  523. out:
  524. vfree(buf);
  525. return rc;
  526. }
  527. static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  528. {
  529. long id = (long) file->private_data;
  530. int rc = -ENXIO, read_only;
  531. struct nvdimm_bus *nvdimm_bus;
  532. read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
  533. mutex_lock(&nvdimm_bus_list_mutex);
  534. list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
  535. if (nvdimm_bus->id == id) {
  536. rc = __nd_ioctl(nvdimm_bus, NULL, read_only, cmd, arg);
  537. break;
  538. }
  539. }
  540. mutex_unlock(&nvdimm_bus_list_mutex);
  541. return rc;
  542. }
  543. static int match_dimm(struct device *dev, void *data)
  544. {
  545. long id = (long) data;
  546. if (is_nvdimm(dev)) {
  547. struct nvdimm *nvdimm = to_nvdimm(dev);
  548. return nvdimm->id == id;
  549. }
  550. return 0;
  551. }
  552. static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  553. {
  554. int rc = -ENXIO, read_only;
  555. struct nvdimm_bus *nvdimm_bus;
  556. read_only = (O_RDWR != (file->f_flags & O_ACCMODE));
  557. mutex_lock(&nvdimm_bus_list_mutex);
  558. list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
  559. struct device *dev = device_find_child(&nvdimm_bus->dev,
  560. file->private_data, match_dimm);
  561. struct nvdimm *nvdimm;
  562. if (!dev)
  563. continue;
  564. nvdimm = to_nvdimm(dev);
  565. rc = __nd_ioctl(nvdimm_bus, nvdimm, read_only, cmd, arg);
  566. put_device(dev);
  567. break;
  568. }
  569. mutex_unlock(&nvdimm_bus_list_mutex);
  570. return rc;
  571. }
  572. static int nd_open(struct inode *inode, struct file *file)
  573. {
  574. long minor = iminor(inode);
  575. file->private_data = (void *) minor;
  576. return 0;
  577. }
  578. static const struct file_operations nvdimm_bus_fops = {
  579. .owner = THIS_MODULE,
  580. .open = nd_open,
  581. .unlocked_ioctl = nd_ioctl,
  582. .compat_ioctl = nd_ioctl,
  583. .llseek = noop_llseek,
  584. };
  585. static const struct file_operations nvdimm_fops = {
  586. .owner = THIS_MODULE,
  587. .open = nd_open,
  588. .unlocked_ioctl = nvdimm_ioctl,
  589. .compat_ioctl = nvdimm_ioctl,
  590. .llseek = noop_llseek,
  591. };
  592. int __init nvdimm_bus_init(void)
  593. {
  594. int rc;
  595. rc = bus_register(&nvdimm_bus_type);
  596. if (rc)
  597. return rc;
  598. rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
  599. if (rc < 0)
  600. goto err_bus_chrdev;
  601. nvdimm_bus_major = rc;
  602. rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
  603. if (rc < 0)
  604. goto err_dimm_chrdev;
  605. nvdimm_major = rc;
  606. nd_class = class_create(THIS_MODULE, "nd");
  607. if (IS_ERR(nd_class)) {
  608. rc = PTR_ERR(nd_class);
  609. goto err_class;
  610. }
  611. return 0;
  612. err_class:
  613. unregister_chrdev(nvdimm_major, "dimmctl");
  614. err_dimm_chrdev:
  615. unregister_chrdev(nvdimm_bus_major, "ndctl");
  616. err_bus_chrdev:
  617. bus_unregister(&nvdimm_bus_type);
  618. return rc;
  619. }
  620. void nvdimm_bus_exit(void)
  621. {
  622. class_destroy(nd_class);
  623. unregister_chrdev(nvdimm_bus_major, "ndctl");
  624. unregister_chrdev(nvdimm_major, "dimmctl");
  625. bus_unregister(&nvdimm_bus_type);
  626. }