bus.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/libnvdimm.h>
  7. #include <linux/sched/mm.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/module.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/async.h>
  14. #include <linux/ndctl.h>
  15. #include <linux/sched.h>
  16. #include <linux/cpu.h>
  17. #include <linux/fs.h>
  18. #include <linux/io.h>
  19. #include <linux/mm.h>
  20. #include <linux/nd.h>
  21. #include "nd-core.h"
  22. #include "nd.h"
  23. #include "pfn.h"
  24. int nvdimm_major;
  25. static int nvdimm_bus_major;
  26. static DEFINE_IDA(nd_ida);
  27. static const struct class nd_class = {
  28. .name = "nd",
  29. };
  30. static int to_nd_device_type(const struct device *dev)
  31. {
  32. if (is_nvdimm(dev))
  33. return ND_DEVICE_DIMM;
  34. else if (is_memory(dev))
  35. return ND_DEVICE_REGION_PMEM;
  36. else if (is_nd_dax(dev))
  37. return ND_DEVICE_DAX_PMEM;
  38. else if (is_nd_region(dev->parent))
  39. return nd_region_to_nstype(to_nd_region(dev->parent));
  40. return 0;
  41. }
  42. static int nvdimm_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
  43. {
  44. return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
  45. to_nd_device_type(dev));
  46. }
  47. static struct module *to_bus_provider(struct device *dev)
  48. {
  49. /* pin bus providers while regions are enabled */
  50. if (is_nd_region(dev)) {
  51. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  52. return nvdimm_bus->nd_desc->module;
  53. }
  54. return NULL;
  55. }
  56. static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
  57. {
  58. guard(nvdimm_bus)(&nvdimm_bus->dev);
  59. nvdimm_bus->probe_active++;
  60. }
  61. static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
  62. {
  63. guard(nvdimm_bus)(&nvdimm_bus->dev);
  64. if (--nvdimm_bus->probe_active == 0)
  65. wake_up(&nvdimm_bus->wait);
  66. }
  67. static int nvdimm_bus_probe(struct device *dev)
  68. {
  69. struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
  70. struct module *provider = to_bus_provider(dev);
  71. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  72. int rc;
  73. if (!try_module_get(provider))
  74. return -ENXIO;
  75. dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
  76. dev->driver->name, dev_name(dev));
  77. nvdimm_bus_probe_start(nvdimm_bus);
  78. rc = nd_drv->probe(dev);
  79. if ((rc == 0 || rc == -EOPNOTSUPP) &&
  80. dev->parent && is_nd_region(dev->parent))
  81. nd_region_advance_seeds(to_nd_region(dev->parent), dev);
  82. nvdimm_bus_probe_end(nvdimm_bus);
  83. dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
  84. dev_name(dev), rc);
  85. if (rc != 0)
  86. module_put(provider);
  87. return rc;
  88. }
  89. static void nvdimm_bus_remove(struct device *dev)
  90. {
  91. struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
  92. struct module *provider = to_bus_provider(dev);
  93. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  94. if (nd_drv->remove)
  95. nd_drv->remove(dev);
  96. dev_dbg(&nvdimm_bus->dev, "%s.remove(%s)\n", dev->driver->name,
  97. dev_name(dev));
  98. module_put(provider);
  99. }
  100. static void nvdimm_bus_shutdown(struct device *dev)
  101. {
  102. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  103. struct nd_device_driver *nd_drv = NULL;
  104. if (dev->driver)
  105. nd_drv = to_nd_device_driver(dev->driver);
  106. if (nd_drv && nd_drv->shutdown) {
  107. nd_drv->shutdown(dev);
  108. dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
  109. dev->driver->name, dev_name(dev));
  110. }
  111. }
  112. void nd_device_notify(struct device *dev, enum nvdimm_event event)
  113. {
  114. device_lock(dev);
  115. if (dev->driver) {
  116. struct nd_device_driver *nd_drv;
  117. nd_drv = to_nd_device_driver(dev->driver);
  118. if (nd_drv->notify)
  119. nd_drv->notify(dev, event);
  120. }
  121. device_unlock(dev);
  122. }
  123. EXPORT_SYMBOL(nd_device_notify);
  124. void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
  125. {
  126. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  127. if (!nvdimm_bus)
  128. return;
  129. /* caller is responsible for holding a reference on the device */
  130. nd_device_notify(&nd_region->dev, event);
  131. }
  132. EXPORT_SYMBOL_GPL(nvdimm_region_notify);
  133. struct clear_badblocks_context {
  134. resource_size_t phys, cleared;
  135. };
  136. static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
  137. {
  138. struct clear_badblocks_context *ctx = data;
  139. struct nd_region *nd_region;
  140. resource_size_t ndr_end;
  141. sector_t sector;
  142. /* make sure device is a region */
  143. if (!is_memory(dev))
  144. return 0;
  145. nd_region = to_nd_region(dev);
  146. ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
  147. /* make sure we are in the region */
  148. if (ctx->phys < nd_region->ndr_start ||
  149. (ctx->phys + ctx->cleared - 1) > ndr_end)
  150. return 0;
  151. sector = (ctx->phys - nd_region->ndr_start) / 512;
  152. badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
  153. if (nd_region->bb_state)
  154. sysfs_notify_dirent(nd_region->bb_state);
  155. return 0;
  156. }
  157. static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
  158. phys_addr_t phys, u64 cleared)
  159. {
  160. struct clear_badblocks_context ctx = {
  161. .phys = phys,
  162. .cleared = cleared,
  163. };
  164. device_for_each_child(&nvdimm_bus->dev, &ctx,
  165. nvdimm_clear_badblocks_region);
  166. }
  167. static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
  168. phys_addr_t phys, u64 cleared)
  169. {
  170. if (cleared > 0)
  171. badrange_forget(&nvdimm_bus->badrange, phys, cleared);
  172. if (cleared > 0 && cleared / 512)
  173. nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
  174. }
  175. long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
  176. unsigned int len)
  177. {
  178. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  179. struct nvdimm_bus_descriptor *nd_desc;
  180. struct nd_cmd_clear_error clear_err;
  181. struct nd_cmd_ars_cap ars_cap;
  182. u32 clear_err_unit, mask;
  183. unsigned int noio_flag;
  184. int cmd_rc, rc;
  185. if (!nvdimm_bus)
  186. return -ENXIO;
  187. nd_desc = nvdimm_bus->nd_desc;
  188. /*
  189. * if ndctl does not exist, it's PMEM_LEGACY and
  190. * we want to just pretend everything is handled.
  191. */
  192. if (!nd_desc->ndctl)
  193. return len;
  194. memset(&ars_cap, 0, sizeof(ars_cap));
  195. ars_cap.address = phys;
  196. ars_cap.length = len;
  197. noio_flag = memalloc_noio_save();
  198. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
  199. sizeof(ars_cap), &cmd_rc);
  200. memalloc_noio_restore(noio_flag);
  201. if (rc < 0)
  202. return rc;
  203. if (cmd_rc < 0)
  204. return cmd_rc;
  205. clear_err_unit = ars_cap.clear_err_unit;
  206. if (!clear_err_unit || !is_power_of_2(clear_err_unit))
  207. return -ENXIO;
  208. mask = clear_err_unit - 1;
  209. if ((phys | len) & mask)
  210. return -ENXIO;
  211. memset(&clear_err, 0, sizeof(clear_err));
  212. clear_err.address = phys;
  213. clear_err.length = len;
  214. noio_flag = memalloc_noio_save();
  215. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
  216. sizeof(clear_err), &cmd_rc);
  217. memalloc_noio_restore(noio_flag);
  218. if (rc < 0)
  219. return rc;
  220. if (cmd_rc < 0)
  221. return cmd_rc;
  222. nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
  223. return clear_err.cleared;
  224. }
  225. EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
  226. static int nvdimm_bus_match(struct device *dev, const struct device_driver *drv);
  227. static const struct bus_type nvdimm_bus_type = {
  228. .name = "nd",
  229. .uevent = nvdimm_bus_uevent,
  230. .match = nvdimm_bus_match,
  231. .probe = nvdimm_bus_probe,
  232. .remove = nvdimm_bus_remove,
  233. .shutdown = nvdimm_bus_shutdown,
  234. };
  235. static void nvdimm_bus_release(struct device *dev)
  236. {
  237. struct nvdimm_bus *nvdimm_bus;
  238. nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
  239. ida_free(&nd_ida, nvdimm_bus->id);
  240. kfree(nvdimm_bus);
  241. }
  242. static const struct device_type nvdimm_bus_dev_type = {
  243. .release = nvdimm_bus_release,
  244. .groups = nvdimm_bus_attribute_groups,
  245. };
  246. bool is_nvdimm_bus(struct device *dev)
  247. {
  248. return dev->type == &nvdimm_bus_dev_type;
  249. }
  250. struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
  251. {
  252. struct device *dev;
  253. for (dev = nd_dev; dev; dev = dev->parent)
  254. if (is_nvdimm_bus(dev))
  255. break;
  256. dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
  257. if (dev)
  258. return to_nvdimm_bus(dev);
  259. return NULL;
  260. }
  261. struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
  262. {
  263. struct nvdimm_bus *nvdimm_bus;
  264. nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
  265. WARN_ON(!is_nvdimm_bus(dev));
  266. return nvdimm_bus;
  267. }
  268. EXPORT_SYMBOL_GPL(to_nvdimm_bus);
  269. struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm)
  270. {
  271. return to_nvdimm_bus(nvdimm->dev.parent);
  272. }
  273. EXPORT_SYMBOL_GPL(nvdimm_to_bus);
  274. static struct lock_class_key nvdimm_bus_key;
  275. struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
  276. struct nvdimm_bus_descriptor *nd_desc)
  277. {
  278. struct nvdimm_bus *nvdimm_bus;
  279. int rc;
  280. nvdimm_bus = kzalloc_obj(*nvdimm_bus);
  281. if (!nvdimm_bus)
  282. return NULL;
  283. INIT_LIST_HEAD(&nvdimm_bus->list);
  284. INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
  285. init_waitqueue_head(&nvdimm_bus->wait);
  286. nvdimm_bus->id = ida_alloc(&nd_ida, GFP_KERNEL);
  287. if (nvdimm_bus->id < 0) {
  288. kfree(nvdimm_bus);
  289. return NULL;
  290. }
  291. mutex_init(&nvdimm_bus->reconfig_mutex);
  292. badrange_init(&nvdimm_bus->badrange);
  293. nvdimm_bus->nd_desc = nd_desc;
  294. nvdimm_bus->dev.parent = parent;
  295. nvdimm_bus->dev.type = &nvdimm_bus_dev_type;
  296. nvdimm_bus->dev.groups = nd_desc->attr_groups;
  297. nvdimm_bus->dev.bus = &nvdimm_bus_type;
  298. nvdimm_bus->dev.of_node = nd_desc->of_node;
  299. device_initialize(&nvdimm_bus->dev);
  300. lockdep_set_class(&nvdimm_bus->dev.mutex, &nvdimm_bus_key);
  301. device_set_pm_not_required(&nvdimm_bus->dev);
  302. rc = dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
  303. if (rc)
  304. goto err;
  305. rc = device_add(&nvdimm_bus->dev);
  306. if (rc) {
  307. dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
  308. goto err;
  309. }
  310. return nvdimm_bus;
  311. err:
  312. put_device(&nvdimm_bus->dev);
  313. return NULL;
  314. }
  315. EXPORT_SYMBOL_GPL(nvdimm_bus_register);
  316. void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
  317. {
  318. if (!nvdimm_bus)
  319. return;
  320. device_unregister(&nvdimm_bus->dev);
  321. }
  322. EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
  323. static int child_unregister(struct device *dev, void *data)
  324. {
  325. /*
  326. * the singular ndctl class device per bus needs to be
  327. * "device_destroy"ed, so skip it here
  328. *
  329. * i.e. remove classless children
  330. */
  331. if (dev->class)
  332. return 0;
  333. if (is_nvdimm(dev))
  334. nvdimm_delete(to_nvdimm(dev));
  335. else
  336. nd_device_unregister(dev, ND_SYNC);
  337. return 0;
  338. }
  339. static void free_badrange_list(struct list_head *badrange_list)
  340. {
  341. struct badrange_entry *bre, *next;
  342. list_for_each_entry_safe(bre, next, badrange_list, list) {
  343. list_del(&bre->list);
  344. kfree(bre);
  345. }
  346. list_del_init(badrange_list);
  347. }
  348. static void nd_bus_remove(struct device *dev)
  349. {
  350. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  351. mutex_lock(&nvdimm_bus_list_mutex);
  352. list_del_init(&nvdimm_bus->list);
  353. mutex_unlock(&nvdimm_bus_list_mutex);
  354. wait_event(nvdimm_bus->wait,
  355. atomic_read(&nvdimm_bus->ioctl_active) == 0);
  356. nd_synchronize();
  357. device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
  358. spin_lock(&nvdimm_bus->badrange.lock);
  359. free_badrange_list(&nvdimm_bus->badrange.list);
  360. spin_unlock(&nvdimm_bus->badrange.lock);
  361. nvdimm_bus_destroy_ndctl(nvdimm_bus);
  362. }
  363. static int nd_bus_probe(struct device *dev)
  364. {
  365. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  366. int rc;
  367. rc = nvdimm_bus_create_ndctl(nvdimm_bus);
  368. if (rc)
  369. return rc;
  370. mutex_lock(&nvdimm_bus_list_mutex);
  371. list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
  372. mutex_unlock(&nvdimm_bus_list_mutex);
  373. /* enable bus provider attributes to look up their local context */
  374. dev_set_drvdata(dev, nvdimm_bus->nd_desc);
  375. return 0;
  376. }
  377. static struct nd_device_driver nd_bus_driver = {
  378. .probe = nd_bus_probe,
  379. .remove = nd_bus_remove,
  380. .drv = {
  381. .name = "nd_bus",
  382. .suppress_bind_attrs = true,
  383. .bus = &nvdimm_bus_type,
  384. .owner = THIS_MODULE,
  385. .mod_name = KBUILD_MODNAME,
  386. },
  387. };
  388. static int nvdimm_bus_match(struct device *dev, const struct device_driver *drv)
  389. {
  390. const struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
  391. if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
  392. return true;
  393. return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
  394. }
  395. static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
  396. void nd_synchronize(void)
  397. {
  398. async_synchronize_full_domain(&nd_async_domain);
  399. }
  400. EXPORT_SYMBOL_GPL(nd_synchronize);
  401. static void nd_async_device_register(void *d, async_cookie_t cookie)
  402. {
  403. struct device *dev = d;
  404. struct device *parent = dev->parent;
  405. if (device_add(dev) != 0) {
  406. dev_err(dev, "%s: failed\n", __func__);
  407. put_device(dev);
  408. }
  409. put_device(dev);
  410. if (parent)
  411. put_device(parent);
  412. }
  413. static void nd_async_device_unregister(void *d, async_cookie_t cookie)
  414. {
  415. struct device *dev = d;
  416. /* flush bus operations before delete */
  417. nvdimm_bus_lock(dev);
  418. nvdimm_bus_unlock(dev);
  419. device_unregister(dev);
  420. put_device(dev);
  421. }
  422. static void __nd_device_register(struct device *dev, bool sync)
  423. {
  424. if (!dev)
  425. return;
  426. /*
  427. * Ensure that region devices always have their NUMA node set as
  428. * early as possible. This way we are able to make certain that
  429. * any memory associated with the creation and the creation
  430. * itself of the region is associated with the correct node.
  431. */
  432. if (is_nd_region(dev))
  433. set_dev_node(dev, to_nd_region(dev)->numa_node);
  434. dev->bus = &nvdimm_bus_type;
  435. device_set_pm_not_required(dev);
  436. if (dev->parent) {
  437. get_device(dev->parent);
  438. if (dev_to_node(dev) == NUMA_NO_NODE)
  439. set_dev_node(dev, dev_to_node(dev->parent));
  440. }
  441. get_device(dev);
  442. if (sync)
  443. nd_async_device_register(dev, 0);
  444. else
  445. async_schedule_dev_domain(nd_async_device_register, dev,
  446. &nd_async_domain);
  447. }
  448. void nd_device_register(struct device *dev)
  449. {
  450. __nd_device_register(dev, false);
  451. }
  452. EXPORT_SYMBOL(nd_device_register);
  453. void nd_device_register_sync(struct device *dev)
  454. {
  455. __nd_device_register(dev, true);
  456. }
  457. void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
  458. {
  459. bool killed;
  460. switch (mode) {
  461. case ND_ASYNC:
  462. /*
  463. * In the async case this is being triggered with the
  464. * device lock held and the unregistration work needs to
  465. * be moved out of line iff this is thread has won the
  466. * race to schedule the deletion.
  467. */
  468. if (!kill_device(dev))
  469. return;
  470. get_device(dev);
  471. async_schedule_domain(nd_async_device_unregister, dev,
  472. &nd_async_domain);
  473. break;
  474. case ND_SYNC:
  475. /*
  476. * In the sync case the device is being unregistered due
  477. * to a state change of the parent. Claim the kill state
  478. * to synchronize against other unregistration requests,
  479. * or otherwise let the async path handle it if the
  480. * unregistration was already queued.
  481. */
  482. device_lock(dev);
  483. killed = kill_device(dev);
  484. device_unlock(dev);
  485. if (!killed)
  486. return;
  487. nd_synchronize();
  488. device_unregister(dev);
  489. break;
  490. }
  491. }
  492. EXPORT_SYMBOL(nd_device_unregister);
  493. /**
  494. * __nd_driver_register() - register a region or a namespace driver
  495. * @nd_drv: driver to register
  496. * @owner: automatically set by nd_driver_register() macro
  497. * @mod_name: automatically set by nd_driver_register() macro
  498. */
  499. int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
  500. const char *mod_name)
  501. {
  502. struct device_driver *drv = &nd_drv->drv;
  503. if (!nd_drv->type) {
  504. pr_debug("driver type bitmask not set (%ps)\n",
  505. __builtin_return_address(0));
  506. return -EINVAL;
  507. }
  508. if (!nd_drv->probe) {
  509. pr_debug("%s ->probe() must be specified\n", mod_name);
  510. return -EINVAL;
  511. }
  512. drv->bus = &nvdimm_bus_type;
  513. drv->owner = owner;
  514. drv->mod_name = mod_name;
  515. return driver_register(drv);
  516. }
  517. EXPORT_SYMBOL(__nd_driver_register);
  518. void nvdimm_check_and_set_ro(struct gendisk *disk)
  519. {
  520. struct device *dev = disk_to_dev(disk)->parent;
  521. struct nd_region *nd_region = to_nd_region(dev->parent);
  522. int disk_ro = get_disk_ro(disk);
  523. /* catch the disk up with the region ro state */
  524. if (disk_ro == nd_region->ro)
  525. return;
  526. dev_info(dev, "%s read-%s, marking %s read-%s\n",
  527. dev_name(&nd_region->dev), nd_region->ro ? "only" : "write",
  528. disk->disk_name, nd_region->ro ? "only" : "write");
  529. set_disk_ro(disk, nd_region->ro);
  530. }
  531. EXPORT_SYMBOL(nvdimm_check_and_set_ro);
  532. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  533. char *buf)
  534. {
  535. return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
  536. to_nd_device_type(dev));
  537. }
  538. static DEVICE_ATTR_RO(modalias);
  539. static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
  540. char *buf)
  541. {
  542. return sprintf(buf, "%s\n", dev->type->name);
  543. }
  544. static DEVICE_ATTR_RO(devtype);
  545. static struct attribute *nd_device_attributes[] = {
  546. &dev_attr_modalias.attr,
  547. &dev_attr_devtype.attr,
  548. NULL,
  549. };
  550. /*
  551. * nd_device_attribute_group - generic attributes for all devices on an nd bus
  552. */
  553. const struct attribute_group nd_device_attribute_group = {
  554. .attrs = nd_device_attributes,
  555. };
  556. static ssize_t numa_node_show(struct device *dev,
  557. struct device_attribute *attr, char *buf)
  558. {
  559. return sprintf(buf, "%d\n", dev_to_node(dev));
  560. }
  561. static DEVICE_ATTR_RO(numa_node);
  562. static int nvdimm_dev_to_target_node(struct device *dev)
  563. {
  564. struct device *parent = dev->parent;
  565. struct nd_region *nd_region = NULL;
  566. if (is_nd_region(dev))
  567. nd_region = to_nd_region(dev);
  568. else if (parent && is_nd_region(parent))
  569. nd_region = to_nd_region(parent);
  570. if (!nd_region)
  571. return NUMA_NO_NODE;
  572. return nd_region->target_node;
  573. }
  574. static ssize_t target_node_show(struct device *dev,
  575. struct device_attribute *attr, char *buf)
  576. {
  577. return sprintf(buf, "%d\n", nvdimm_dev_to_target_node(dev));
  578. }
  579. static DEVICE_ATTR_RO(target_node);
  580. static struct attribute *nd_numa_attributes[] = {
  581. &dev_attr_numa_node.attr,
  582. &dev_attr_target_node.attr,
  583. NULL,
  584. };
  585. static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
  586. int n)
  587. {
  588. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  589. if (!IS_ENABLED(CONFIG_NUMA))
  590. return 0;
  591. if (a == &dev_attr_target_node.attr &&
  592. nvdimm_dev_to_target_node(dev) == NUMA_NO_NODE)
  593. return 0;
  594. return a->mode;
  595. }
  596. /*
  597. * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
  598. */
  599. const struct attribute_group nd_numa_attribute_group = {
  600. .attrs = nd_numa_attributes,
  601. .is_visible = nd_numa_attr_visible,
  602. };
  603. static void ndctl_release(struct device *dev)
  604. {
  605. kfree(dev);
  606. }
  607. static struct lock_class_key nvdimm_ndctl_key;
  608. int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
  609. {
  610. dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
  611. struct device *dev;
  612. int rc;
  613. dev = kzalloc_obj(*dev);
  614. if (!dev)
  615. return -ENOMEM;
  616. device_initialize(dev);
  617. lockdep_set_class(&dev->mutex, &nvdimm_ndctl_key);
  618. device_set_pm_not_required(dev);
  619. dev->class = &nd_class;
  620. dev->parent = &nvdimm_bus->dev;
  621. dev->devt = devt;
  622. dev->release = ndctl_release;
  623. rc = dev_set_name(dev, "ndctl%d", nvdimm_bus->id);
  624. if (rc)
  625. goto err;
  626. rc = device_add(dev);
  627. if (rc) {
  628. dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %d\n",
  629. nvdimm_bus->id, rc);
  630. goto err;
  631. }
  632. return 0;
  633. err:
  634. put_device(dev);
  635. return rc;
  636. }
  637. void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
  638. {
  639. device_destroy(&nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
  640. }
  641. static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
  642. [ND_CMD_IMPLEMENTED] = { },
  643. [ND_CMD_SMART] = {
  644. .out_num = 2,
  645. .out_sizes = { 4, 128, },
  646. },
  647. [ND_CMD_SMART_THRESHOLD] = {
  648. .out_num = 2,
  649. .out_sizes = { 4, 8, },
  650. },
  651. [ND_CMD_DIMM_FLAGS] = {
  652. .out_num = 2,
  653. .out_sizes = { 4, 4 },
  654. },
  655. [ND_CMD_GET_CONFIG_SIZE] = {
  656. .out_num = 3,
  657. .out_sizes = { 4, 4, 4, },
  658. },
  659. [ND_CMD_GET_CONFIG_DATA] = {
  660. .in_num = 2,
  661. .in_sizes = { 4, 4, },
  662. .out_num = 2,
  663. .out_sizes = { 4, UINT_MAX, },
  664. },
  665. [ND_CMD_SET_CONFIG_DATA] = {
  666. .in_num = 3,
  667. .in_sizes = { 4, 4, UINT_MAX, },
  668. .out_num = 1,
  669. .out_sizes = { 4, },
  670. },
  671. [ND_CMD_VENDOR] = {
  672. .in_num = 3,
  673. .in_sizes = { 4, 4, UINT_MAX, },
  674. .out_num = 3,
  675. .out_sizes = { 4, 4, UINT_MAX, },
  676. },
  677. [ND_CMD_CALL] = {
  678. .in_num = 2,
  679. .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
  680. .out_num = 1,
  681. .out_sizes = { UINT_MAX, },
  682. },
  683. };
  684. const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
  685. {
  686. if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
  687. return &__nd_cmd_dimm_descs[cmd];
  688. return NULL;
  689. }
  690. EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
  691. static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
  692. [ND_CMD_IMPLEMENTED] = { },
  693. [ND_CMD_ARS_CAP] = {
  694. .in_num = 2,
  695. .in_sizes = { 8, 8, },
  696. .out_num = 4,
  697. .out_sizes = { 4, 4, 4, 4, },
  698. },
  699. [ND_CMD_ARS_START] = {
  700. .in_num = 5,
  701. .in_sizes = { 8, 8, 2, 1, 5, },
  702. .out_num = 2,
  703. .out_sizes = { 4, 4, },
  704. },
  705. [ND_CMD_ARS_STATUS] = {
  706. .out_num = 3,
  707. .out_sizes = { 4, 4, UINT_MAX, },
  708. },
  709. [ND_CMD_CLEAR_ERROR] = {
  710. .in_num = 2,
  711. .in_sizes = { 8, 8, },
  712. .out_num = 3,
  713. .out_sizes = { 4, 4, 8, },
  714. },
  715. [ND_CMD_CALL] = {
  716. .in_num = 2,
  717. .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
  718. .out_num = 1,
  719. .out_sizes = { UINT_MAX, },
  720. },
  721. };
  722. const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
  723. {
  724. if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
  725. return &__nd_cmd_bus_descs[cmd];
  726. return NULL;
  727. }
  728. EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
  729. u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
  730. const struct nd_cmd_desc *desc, int idx, void *buf)
  731. {
  732. if (idx >= desc->in_num)
  733. return UINT_MAX;
  734. if (desc->in_sizes[idx] < UINT_MAX)
  735. return desc->in_sizes[idx];
  736. if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
  737. struct nd_cmd_set_config_hdr *hdr = buf;
  738. return hdr->in_length;
  739. } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
  740. struct nd_cmd_vendor_hdr *hdr = buf;
  741. return hdr->in_length;
  742. } else if (cmd == ND_CMD_CALL) {
  743. struct nd_cmd_pkg *pkg = buf;
  744. return pkg->nd_size_in;
  745. }
  746. return UINT_MAX;
  747. }
  748. EXPORT_SYMBOL_GPL(nd_cmd_in_size);
  749. u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
  750. const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
  751. const u32 *out_field, unsigned long remainder)
  752. {
  753. if (idx >= desc->out_num)
  754. return UINT_MAX;
  755. if (desc->out_sizes[idx] < UINT_MAX)
  756. return desc->out_sizes[idx];
  757. if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
  758. return in_field[1];
  759. else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
  760. return out_field[1];
  761. else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
  762. /*
  763. * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
  764. * "Size of Output Buffer in bytes, including this
  765. * field."
  766. */
  767. if (out_field[1] < 4)
  768. return 0;
  769. /*
  770. * ACPI 6.1 is ambiguous if 'status' is included in the
  771. * output size. If we encounter an output size that
  772. * overshoots the remainder by 4 bytes, assume it was
  773. * including 'status'.
  774. */
  775. if (out_field[1] - 4 == remainder)
  776. return remainder;
  777. return out_field[1] - 8;
  778. } else if (cmd == ND_CMD_CALL) {
  779. struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
  780. return pkg->nd_size_out;
  781. }
  782. return UINT_MAX;
  783. }
  784. EXPORT_SYMBOL_GPL(nd_cmd_out_size);
  785. void wait_nvdimm_bus_probe_idle(struct device *dev)
  786. {
  787. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  788. do {
  789. if (nvdimm_bus->probe_active == 0)
  790. break;
  791. nvdimm_bus_unlock(dev);
  792. device_unlock(dev);
  793. wait_event(nvdimm_bus->wait,
  794. nvdimm_bus->probe_active == 0);
  795. device_lock(dev);
  796. nvdimm_bus_lock(dev);
  797. } while (true);
  798. }
  799. static int nd_pmem_forget_poison_check(struct device *dev, void *data)
  800. {
  801. struct nd_cmd_clear_error *clear_err =
  802. (struct nd_cmd_clear_error *)data;
  803. struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
  804. struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
  805. struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
  806. struct nd_namespace_common *ndns = NULL;
  807. struct nd_namespace_io *nsio;
  808. resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
  809. if (nd_dax || !dev->driver)
  810. return 0;
  811. start = clear_err->address;
  812. end = clear_err->address + clear_err->cleared - 1;
  813. if (nd_btt || nd_pfn || nd_dax) {
  814. if (nd_btt)
  815. ndns = nd_btt->ndns;
  816. else if (nd_pfn)
  817. ndns = nd_pfn->ndns;
  818. else if (nd_dax)
  819. ndns = nd_dax->nd_pfn.ndns;
  820. if (!ndns)
  821. return 0;
  822. } else
  823. ndns = to_ndns(dev);
  824. nsio = to_nd_namespace_io(&ndns->dev);
  825. pstart = nsio->res.start + offset;
  826. pend = nsio->res.end - end_trunc;
  827. if ((pstart >= start) && (pend <= end))
  828. return -EBUSY;
  829. return 0;
  830. }
  831. static int nd_ns_forget_poison_check(struct device *dev, void *data)
  832. {
  833. return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
  834. }
  835. /* set_config requires an idle interleave set */
  836. static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
  837. struct nvdimm *nvdimm, unsigned int cmd, void *data)
  838. {
  839. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  840. /* ask the bus provider if it would like to block this request */
  841. if (nd_desc->clear_to_send) {
  842. int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd, data);
  843. if (rc)
  844. return rc;
  845. }
  846. /* require clear error to go through the pmem driver */
  847. if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
  848. return device_for_each_child(&nvdimm_bus->dev, data,
  849. nd_ns_forget_poison_check);
  850. if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
  851. return 0;
  852. /* prevent label manipulation while the kernel owns label updates */
  853. wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
  854. if (atomic_read(&nvdimm->busy))
  855. return -EBUSY;
  856. return 0;
  857. }
  858. static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
  859. int read_only, unsigned int ioctl_cmd, unsigned long arg)
  860. {
  861. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  862. const struct nd_cmd_desc *desc = NULL;
  863. unsigned int cmd = _IOC_NR(ioctl_cmd);
  864. struct device *dev = &nvdimm_bus->dev;
  865. void __user *p = (void __user *) arg;
  866. const char *cmd_name, *dimm_name;
  867. u32 in_len = 0, out_len = 0;
  868. unsigned int func = cmd;
  869. unsigned long cmd_mask;
  870. struct nd_cmd_pkg pkg;
  871. int rc, i, cmd_rc;
  872. u64 buf_len = 0;
  873. if (nvdimm) {
  874. desc = nd_cmd_dimm_desc(cmd);
  875. cmd_name = nvdimm_cmd_name(cmd);
  876. cmd_mask = nvdimm->cmd_mask;
  877. dimm_name = dev_name(&nvdimm->dev);
  878. } else {
  879. desc = nd_cmd_bus_desc(cmd);
  880. cmd_name = nvdimm_bus_cmd_name(cmd);
  881. cmd_mask = nd_desc->cmd_mask;
  882. dimm_name = "bus";
  883. }
  884. /* Validate command family support against bus declared support */
  885. if (cmd == ND_CMD_CALL) {
  886. unsigned long *mask;
  887. if (copy_from_user(&pkg, p, sizeof(pkg)))
  888. return -EFAULT;
  889. if (nvdimm) {
  890. if (pkg.nd_family > NVDIMM_FAMILY_MAX)
  891. return -EINVAL;
  892. mask = &nd_desc->dimm_family_mask;
  893. } else {
  894. if (pkg.nd_family > NVDIMM_BUS_FAMILY_MAX)
  895. return -EINVAL;
  896. mask = &nd_desc->bus_family_mask;
  897. }
  898. if (!test_bit(pkg.nd_family, mask))
  899. return -EINVAL;
  900. }
  901. if (!desc ||
  902. (desc->out_num + desc->in_num == 0) ||
  903. cmd > ND_CMD_CALL ||
  904. !test_bit(cmd, &cmd_mask))
  905. return -ENOTTY;
  906. /* fail write commands (when read-only) */
  907. if (read_only)
  908. switch (cmd) {
  909. case ND_CMD_VENDOR:
  910. case ND_CMD_SET_CONFIG_DATA:
  911. case ND_CMD_ARS_START:
  912. case ND_CMD_CLEAR_ERROR:
  913. case ND_CMD_CALL:
  914. dev_dbg(dev, "'%s' command while read-only.\n",
  915. nvdimm ? nvdimm_cmd_name(cmd)
  916. : nvdimm_bus_cmd_name(cmd));
  917. return -EPERM;
  918. default:
  919. break;
  920. }
  921. /* process an input envelope */
  922. char *in_env __free(kfree) = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
  923. if (!in_env)
  924. return -ENOMEM;
  925. for (i = 0; i < desc->in_num; i++) {
  926. u32 in_size, copy;
  927. in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
  928. if (in_size == UINT_MAX) {
  929. dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
  930. __func__, dimm_name, cmd_name, i);
  931. return -ENXIO;
  932. }
  933. if (in_len < ND_CMD_MAX_ENVELOPE)
  934. copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
  935. else
  936. copy = 0;
  937. if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
  938. return -EFAULT;
  939. in_len += in_size;
  940. }
  941. if (cmd == ND_CMD_CALL) {
  942. func = pkg.nd_command;
  943. dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
  944. dimm_name, pkg.nd_command,
  945. in_len, out_len, buf_len);
  946. }
  947. /* process an output envelope */
  948. char *out_env __free(kfree) = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
  949. if (!out_env)
  950. return -ENOMEM;
  951. for (i = 0; i < desc->out_num; i++) {
  952. u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
  953. (u32 *) in_env, (u32 *) out_env, 0);
  954. u32 copy;
  955. if (out_size == UINT_MAX) {
  956. dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
  957. dimm_name, cmd_name, i);
  958. return -EFAULT;
  959. }
  960. if (out_len < ND_CMD_MAX_ENVELOPE)
  961. copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
  962. else
  963. copy = 0;
  964. if (copy && copy_from_user(&out_env[out_len],
  965. p + in_len + out_len, copy)) {
  966. return -EFAULT;
  967. }
  968. out_len += out_size;
  969. }
  970. buf_len = (u64) out_len + (u64) in_len;
  971. if (buf_len > ND_IOCTL_MAX_BUFLEN) {
  972. dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
  973. cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
  974. return -EINVAL;
  975. }
  976. void *buf __free(kvfree) = kvzalloc(buf_len, GFP_KERNEL);
  977. if (!buf)
  978. return -ENOMEM;
  979. if (copy_from_user(buf, p, buf_len))
  980. return -EFAULT;
  981. guard(device)(dev);
  982. guard(nvdimm_bus)(dev);
  983. rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
  984. if (rc)
  985. return rc;
  986. rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
  987. if (rc < 0)
  988. return rc;
  989. if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
  990. struct nd_cmd_clear_error *clear_err = buf;
  991. nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
  992. clear_err->cleared);
  993. }
  994. if (copy_to_user(p, buf, buf_len))
  995. return -EFAULT;
  996. return 0;
  997. }
  998. enum nd_ioctl_mode {
  999. BUS_IOCTL,
  1000. DIMM_IOCTL,
  1001. };
  1002. static int match_dimm(struct device *dev, const void *data)
  1003. {
  1004. long id = (long) data;
  1005. if (is_nvdimm(dev)) {
  1006. struct nvdimm *nvdimm = to_nvdimm(dev);
  1007. return nvdimm->id == id;
  1008. }
  1009. return 0;
  1010. }
  1011. static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
  1012. enum nd_ioctl_mode mode)
  1013. {
  1014. struct nvdimm_bus *nvdimm_bus, *found = NULL;
  1015. long id = (long) file->private_data;
  1016. struct nvdimm *nvdimm = NULL;
  1017. int rc, ro;
  1018. ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
  1019. mutex_lock(&nvdimm_bus_list_mutex);
  1020. list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
  1021. if (mode == DIMM_IOCTL) {
  1022. struct device *dev;
  1023. dev = device_find_child(&nvdimm_bus->dev,
  1024. file->private_data, match_dimm);
  1025. if (!dev)
  1026. continue;
  1027. nvdimm = to_nvdimm(dev);
  1028. found = nvdimm_bus;
  1029. } else if (nvdimm_bus->id == id) {
  1030. found = nvdimm_bus;
  1031. }
  1032. if (found) {
  1033. atomic_inc(&nvdimm_bus->ioctl_active);
  1034. break;
  1035. }
  1036. }
  1037. mutex_unlock(&nvdimm_bus_list_mutex);
  1038. if (!found)
  1039. return -ENXIO;
  1040. nvdimm_bus = found;
  1041. rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
  1042. if (nvdimm)
  1043. put_device(&nvdimm->dev);
  1044. if (atomic_dec_and_test(&nvdimm_bus->ioctl_active))
  1045. wake_up(&nvdimm_bus->wait);
  1046. return rc;
  1047. }
  1048. static long bus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1049. {
  1050. return nd_ioctl(file, cmd, arg, BUS_IOCTL);
  1051. }
  1052. static long dimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1053. {
  1054. return nd_ioctl(file, cmd, arg, DIMM_IOCTL);
  1055. }
  1056. static int nd_open(struct inode *inode, struct file *file)
  1057. {
  1058. long minor = iminor(inode);
  1059. file->private_data = (void *) minor;
  1060. return 0;
  1061. }
  1062. static const struct file_operations nvdimm_bus_fops = {
  1063. .owner = THIS_MODULE,
  1064. .open = nd_open,
  1065. .unlocked_ioctl = bus_ioctl,
  1066. .compat_ioctl = compat_ptr_ioctl,
  1067. .llseek = noop_llseek,
  1068. };
  1069. static const struct file_operations nvdimm_fops = {
  1070. .owner = THIS_MODULE,
  1071. .open = nd_open,
  1072. .unlocked_ioctl = dimm_ioctl,
  1073. .compat_ioctl = compat_ptr_ioctl,
  1074. .llseek = noop_llseek,
  1075. };
  1076. int __init nvdimm_bus_init(void)
  1077. {
  1078. int rc;
  1079. rc = bus_register(&nvdimm_bus_type);
  1080. if (rc)
  1081. return rc;
  1082. rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
  1083. if (rc < 0)
  1084. goto err_bus_chrdev;
  1085. nvdimm_bus_major = rc;
  1086. rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
  1087. if (rc < 0)
  1088. goto err_dimm_chrdev;
  1089. nvdimm_major = rc;
  1090. rc = class_register(&nd_class);
  1091. if (rc)
  1092. goto err_class;
  1093. rc = driver_register(&nd_bus_driver.drv);
  1094. if (rc)
  1095. goto err_nd_bus;
  1096. return 0;
  1097. err_nd_bus:
  1098. class_unregister(&nd_class);
  1099. err_class:
  1100. unregister_chrdev(nvdimm_major, "dimmctl");
  1101. err_dimm_chrdev:
  1102. unregister_chrdev(nvdimm_bus_major, "ndctl");
  1103. err_bus_chrdev:
  1104. bus_unregister(&nvdimm_bus_type);
  1105. return rc;
  1106. }
  1107. void nvdimm_bus_exit(void)
  1108. {
  1109. driver_unregister(&nd_bus_driver.drv);
  1110. class_unregister(&nd_class);
  1111. unregister_chrdev(nvdimm_bus_major, "ndctl");
  1112. unregister_chrdev(nvdimm_major, "dimmctl");
  1113. bus_unregister(&nvdimm_bus_type);
  1114. ida_destroy(&nd_ida);
  1115. }