bus.c 31 KB

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