bus.c 19 KB

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