bus.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. struct class *nd_class;
  28. static DEFINE_IDA(nd_ida);
  29. static int to_nd_device_type(struct device *dev)
  30. {
  31. if (is_nvdimm(dev))
  32. return ND_DEVICE_DIMM;
  33. else if (is_memory(dev))
  34. return ND_DEVICE_REGION_PMEM;
  35. else if (is_nd_dax(dev))
  36. return ND_DEVICE_DAX_PMEM;
  37. else if (is_nd_region(dev->parent))
  38. return nd_region_to_nstype(to_nd_region(dev->parent));
  39. return 0;
  40. }
  41. static int nvdimm_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  42. {
  43. return add_uevent_var(env, "MODALIAS=" ND_DEVICE_MODALIAS_FMT,
  44. to_nd_device_type(dev));
  45. }
  46. static struct module *to_bus_provider(struct device *dev)
  47. {
  48. /* pin bus providers while regions are enabled */
  49. if (is_nd_region(dev)) {
  50. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  51. return nvdimm_bus->nd_desc->module;
  52. }
  53. return NULL;
  54. }
  55. static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
  56. {
  57. nvdimm_bus_lock(&nvdimm_bus->dev);
  58. nvdimm_bus->probe_active++;
  59. nvdimm_bus_unlock(&nvdimm_bus->dev);
  60. }
  61. static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
  62. {
  63. nvdimm_bus_lock(&nvdimm_bus->dev);
  64. if (--nvdimm_bus->probe_active == 0)
  65. wake_up(&nvdimm_bus->wait);
  66. nvdimm_bus_unlock(&nvdimm_bus->dev);
  67. }
  68. static int nvdimm_bus_probe(struct device *dev)
  69. {
  70. struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
  71. struct module *provider = to_bus_provider(dev);
  72. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  73. int rc;
  74. if (!try_module_get(provider))
  75. return -ENXIO;
  76. dev_dbg(&nvdimm_bus->dev, "START: %s.probe(%s)\n",
  77. dev->driver->name, dev_name(dev));
  78. nvdimm_bus_probe_start(nvdimm_bus);
  79. rc = nd_drv->probe(dev);
  80. if ((rc == 0 || rc == -EOPNOTSUPP) &&
  81. dev->parent && is_nd_region(dev->parent))
  82. nd_region_advance_seeds(to_nd_region(dev->parent), dev);
  83. nvdimm_bus_probe_end(nvdimm_bus);
  84. dev_dbg(&nvdimm_bus->dev, "END: %s.probe(%s) = %d\n", dev->driver->name,
  85. dev_name(dev), rc);
  86. if (rc != 0)
  87. module_put(provider);
  88. return rc;
  89. }
  90. static void nvdimm_bus_remove(struct device *dev)
  91. {
  92. struct nd_device_driver *nd_drv = to_nd_device_driver(dev->driver);
  93. struct module *provider = to_bus_provider(dev);
  94. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  95. if (nd_drv->remove)
  96. nd_drv->remove(dev);
  97. dev_dbg(&nvdimm_bus->dev, "%s.remove(%s)\n", dev->driver->name,
  98. dev_name(dev));
  99. module_put(provider);
  100. }
  101. static void nvdimm_bus_shutdown(struct device *dev)
  102. {
  103. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  104. struct nd_device_driver *nd_drv = NULL;
  105. if (dev->driver)
  106. nd_drv = to_nd_device_driver(dev->driver);
  107. if (nd_drv && nd_drv->shutdown) {
  108. nd_drv->shutdown(dev);
  109. dev_dbg(&nvdimm_bus->dev, "%s.shutdown(%s)\n",
  110. dev->driver->name, dev_name(dev));
  111. }
  112. }
  113. void nd_device_notify(struct device *dev, enum nvdimm_event event)
  114. {
  115. device_lock(dev);
  116. if (dev->driver) {
  117. struct nd_device_driver *nd_drv;
  118. nd_drv = to_nd_device_driver(dev->driver);
  119. if (nd_drv->notify)
  120. nd_drv->notify(dev, event);
  121. }
  122. device_unlock(dev);
  123. }
  124. EXPORT_SYMBOL(nd_device_notify);
  125. void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event)
  126. {
  127. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  128. if (!nvdimm_bus)
  129. return;
  130. /* caller is responsible for holding a reference on the device */
  131. nd_device_notify(&nd_region->dev, event);
  132. }
  133. EXPORT_SYMBOL_GPL(nvdimm_region_notify);
  134. struct clear_badblocks_context {
  135. resource_size_t phys, cleared;
  136. };
  137. static int nvdimm_clear_badblocks_region(struct device *dev, void *data)
  138. {
  139. struct clear_badblocks_context *ctx = data;
  140. struct nd_region *nd_region;
  141. resource_size_t ndr_end;
  142. sector_t sector;
  143. /* make sure device is a region */
  144. if (!is_memory(dev))
  145. return 0;
  146. nd_region = to_nd_region(dev);
  147. ndr_end = nd_region->ndr_start + nd_region->ndr_size - 1;
  148. /* make sure we are in the region */
  149. if (ctx->phys < nd_region->ndr_start ||
  150. (ctx->phys + ctx->cleared - 1) > ndr_end)
  151. return 0;
  152. sector = (ctx->phys - nd_region->ndr_start) / 512;
  153. badblocks_clear(&nd_region->bb, sector, ctx->cleared / 512);
  154. if (nd_region->bb_state)
  155. sysfs_notify_dirent(nd_region->bb_state);
  156. return 0;
  157. }
  158. static void nvdimm_clear_badblocks_regions(struct nvdimm_bus *nvdimm_bus,
  159. phys_addr_t phys, u64 cleared)
  160. {
  161. struct clear_badblocks_context ctx = {
  162. .phys = phys,
  163. .cleared = cleared,
  164. };
  165. device_for_each_child(&nvdimm_bus->dev, &ctx,
  166. nvdimm_clear_badblocks_region);
  167. }
  168. static void nvdimm_account_cleared_poison(struct nvdimm_bus *nvdimm_bus,
  169. phys_addr_t phys, u64 cleared)
  170. {
  171. if (cleared > 0)
  172. badrange_forget(&nvdimm_bus->badrange, phys, cleared);
  173. if (cleared > 0 && cleared / 512)
  174. nvdimm_clear_badblocks_regions(nvdimm_bus, phys, cleared);
  175. }
  176. long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
  177. unsigned int len)
  178. {
  179. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  180. struct nvdimm_bus_descriptor *nd_desc;
  181. struct nd_cmd_clear_error clear_err;
  182. struct nd_cmd_ars_cap ars_cap;
  183. u32 clear_err_unit, mask;
  184. unsigned int noio_flag;
  185. int cmd_rc, rc;
  186. if (!nvdimm_bus)
  187. return -ENXIO;
  188. nd_desc = nvdimm_bus->nd_desc;
  189. /*
  190. * if ndctl does not exist, it's PMEM_LEGACY and
  191. * we want to just pretend everything is handled.
  192. */
  193. if (!nd_desc->ndctl)
  194. return len;
  195. memset(&ars_cap, 0, sizeof(ars_cap));
  196. ars_cap.address = phys;
  197. ars_cap.length = len;
  198. noio_flag = memalloc_noio_save();
  199. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, &ars_cap,
  200. sizeof(ars_cap), &cmd_rc);
  201. memalloc_noio_restore(noio_flag);
  202. if (rc < 0)
  203. return rc;
  204. if (cmd_rc < 0)
  205. return cmd_rc;
  206. clear_err_unit = ars_cap.clear_err_unit;
  207. if (!clear_err_unit || !is_power_of_2(clear_err_unit))
  208. return -ENXIO;
  209. mask = clear_err_unit - 1;
  210. if ((phys | len) & mask)
  211. return -ENXIO;
  212. memset(&clear_err, 0, sizeof(clear_err));
  213. clear_err.address = phys;
  214. clear_err.length = len;
  215. noio_flag = memalloc_noio_save();
  216. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_CLEAR_ERROR, &clear_err,
  217. sizeof(clear_err), &cmd_rc);
  218. memalloc_noio_restore(noio_flag);
  219. if (rc < 0)
  220. return rc;
  221. if (cmd_rc < 0)
  222. return cmd_rc;
  223. nvdimm_account_cleared_poison(nvdimm_bus, phys, clear_err.cleared);
  224. return clear_err.cleared;
  225. }
  226. EXPORT_SYMBOL_GPL(nvdimm_clear_poison);
  227. static int nvdimm_bus_match(struct device *dev, struct device_driver *drv);
  228. static struct bus_type nvdimm_bus_type = {
  229. .name = "nd",
  230. .uevent = nvdimm_bus_uevent,
  231. .match = nvdimm_bus_match,
  232. .probe = nvdimm_bus_probe,
  233. .remove = nvdimm_bus_remove,
  234. .shutdown = nvdimm_bus_shutdown,
  235. };
  236. static void nvdimm_bus_release(struct device *dev)
  237. {
  238. struct nvdimm_bus *nvdimm_bus;
  239. nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
  240. ida_simple_remove(&nd_ida, nvdimm_bus->id);
  241. kfree(nvdimm_bus);
  242. }
  243. static const struct device_type nvdimm_bus_dev_type = {
  244. .release = nvdimm_bus_release,
  245. .groups = nvdimm_bus_attribute_groups,
  246. };
  247. bool is_nvdimm_bus(struct device *dev)
  248. {
  249. return dev->type == &nvdimm_bus_dev_type;
  250. }
  251. struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
  252. {
  253. struct device *dev;
  254. for (dev = nd_dev; dev; dev = dev->parent)
  255. if (is_nvdimm_bus(dev))
  256. break;
  257. dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
  258. if (dev)
  259. return to_nvdimm_bus(dev);
  260. return NULL;
  261. }
  262. struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
  263. {
  264. struct nvdimm_bus *nvdimm_bus;
  265. nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
  266. WARN_ON(!is_nvdimm_bus(dev));
  267. return nvdimm_bus;
  268. }
  269. EXPORT_SYMBOL_GPL(to_nvdimm_bus);
  270. struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm)
  271. {
  272. return to_nvdimm_bus(nvdimm->dev.parent);
  273. }
  274. EXPORT_SYMBOL_GPL(nvdimm_to_bus);
  275. static struct lock_class_key nvdimm_bus_key;
  276. struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
  277. struct nvdimm_bus_descriptor *nd_desc)
  278. {
  279. struct nvdimm_bus *nvdimm_bus;
  280. int rc;
  281. nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
  282. if (!nvdimm_bus)
  283. return NULL;
  284. INIT_LIST_HEAD(&nvdimm_bus->list);
  285. INIT_LIST_HEAD(&nvdimm_bus->mapping_list);
  286. init_waitqueue_head(&nvdimm_bus->wait);
  287. nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
  288. if (nvdimm_bus->id < 0) {
  289. kfree(nvdimm_bus);
  290. return NULL;
  291. }
  292. mutex_init(&nvdimm_bus->reconfig_mutex);
  293. badrange_init(&nvdimm_bus->badrange);
  294. nvdimm_bus->nd_desc = nd_desc;
  295. nvdimm_bus->dev.parent = parent;
  296. nvdimm_bus->dev.type = &nvdimm_bus_dev_type;
  297. nvdimm_bus->dev.groups = nd_desc->attr_groups;
  298. nvdimm_bus->dev.bus = &nvdimm_bus_type;
  299. nvdimm_bus->dev.of_node = nd_desc->of_node;
  300. device_initialize(&nvdimm_bus->dev);
  301. lockdep_set_class(&nvdimm_bus->dev.mutex, &nvdimm_bus_key);
  302. device_set_pm_not_required(&nvdimm_bus->dev);
  303. rc = dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
  304. if (rc)
  305. goto err;
  306. rc = device_add(&nvdimm_bus->dev);
  307. if (rc) {
  308. dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
  309. goto err;
  310. }
  311. return nvdimm_bus;
  312. err:
  313. put_device(&nvdimm_bus->dev);
  314. return NULL;
  315. }
  316. EXPORT_SYMBOL_GPL(nvdimm_bus_register);
  317. void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
  318. {
  319. if (!nvdimm_bus)
  320. return;
  321. device_unregister(&nvdimm_bus->dev);
  322. }
  323. EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
  324. static int child_unregister(struct device *dev, void *data)
  325. {
  326. /*
  327. * the singular ndctl class device per bus needs to be
  328. * "device_destroy"ed, so skip it here
  329. *
  330. * i.e. remove classless children
  331. */
  332. if (dev->class)
  333. return 0;
  334. if (is_nvdimm(dev))
  335. nvdimm_delete(to_nvdimm(dev));
  336. else
  337. nd_device_unregister(dev, ND_SYNC);
  338. return 0;
  339. }
  340. static void free_badrange_list(struct list_head *badrange_list)
  341. {
  342. struct badrange_entry *bre, *next;
  343. list_for_each_entry_safe(bre, next, badrange_list, list) {
  344. list_del(&bre->list);
  345. kfree(bre);
  346. }
  347. list_del_init(badrange_list);
  348. }
  349. static void nd_bus_remove(struct device *dev)
  350. {
  351. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  352. mutex_lock(&nvdimm_bus_list_mutex);
  353. list_del_init(&nvdimm_bus->list);
  354. mutex_unlock(&nvdimm_bus_list_mutex);
  355. wait_event(nvdimm_bus->wait,
  356. atomic_read(&nvdimm_bus->ioctl_active) == 0);
  357. nd_synchronize();
  358. device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
  359. spin_lock(&nvdimm_bus->badrange.lock);
  360. free_badrange_list(&nvdimm_bus->badrange.list);
  361. spin_unlock(&nvdimm_bus->badrange.lock);
  362. nvdimm_bus_destroy_ndctl(nvdimm_bus);
  363. }
  364. static int nd_bus_probe(struct device *dev)
  365. {
  366. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  367. int rc;
  368. rc = nvdimm_bus_create_ndctl(nvdimm_bus);
  369. if (rc)
  370. return rc;
  371. mutex_lock(&nvdimm_bus_list_mutex);
  372. list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
  373. mutex_unlock(&nvdimm_bus_list_mutex);
  374. /* enable bus provider attributes to look up their local context */
  375. dev_set_drvdata(dev, nvdimm_bus->nd_desc);
  376. return 0;
  377. }
  378. static struct nd_device_driver nd_bus_driver = {
  379. .probe = nd_bus_probe,
  380. .remove = nd_bus_remove,
  381. .drv = {
  382. .name = "nd_bus",
  383. .suppress_bind_attrs = true,
  384. .bus = &nvdimm_bus_type,
  385. .owner = THIS_MODULE,
  386. .mod_name = KBUILD_MODNAME,
  387. },
  388. };
  389. static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
  390. {
  391. struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
  392. if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
  393. return true;
  394. return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
  395. }
  396. static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
  397. void nd_synchronize(void)
  398. {
  399. async_synchronize_full_domain(&nd_async_domain);
  400. }
  401. EXPORT_SYMBOL_GPL(nd_synchronize);
  402. static void nd_async_device_register(void *d, async_cookie_t cookie)
  403. {
  404. struct device *dev = d;
  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 (dev->parent)
  411. put_device(dev->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(sizeof(*dev), GFP_KERNEL);
  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. char *out_env = NULL, *in_env = NULL;
  867. const char *cmd_name, *dimm_name;
  868. u32 in_len = 0, out_len = 0;
  869. unsigned int func = cmd;
  870. unsigned long cmd_mask;
  871. struct nd_cmd_pkg pkg;
  872. int rc, i, cmd_rc;
  873. void *buf = NULL;
  874. u64 buf_len = 0;
  875. if (nvdimm) {
  876. desc = nd_cmd_dimm_desc(cmd);
  877. cmd_name = nvdimm_cmd_name(cmd);
  878. cmd_mask = nvdimm->cmd_mask;
  879. dimm_name = dev_name(&nvdimm->dev);
  880. } else {
  881. desc = nd_cmd_bus_desc(cmd);
  882. cmd_name = nvdimm_bus_cmd_name(cmd);
  883. cmd_mask = nd_desc->cmd_mask;
  884. dimm_name = "bus";
  885. }
  886. /* Validate command family support against bus declared support */
  887. if (cmd == ND_CMD_CALL) {
  888. unsigned long *mask;
  889. if (copy_from_user(&pkg, p, sizeof(pkg)))
  890. return -EFAULT;
  891. if (nvdimm) {
  892. if (pkg.nd_family > NVDIMM_FAMILY_MAX)
  893. return -EINVAL;
  894. mask = &nd_desc->dimm_family_mask;
  895. } else {
  896. if (pkg.nd_family > NVDIMM_BUS_FAMILY_MAX)
  897. return -EINVAL;
  898. mask = &nd_desc->bus_family_mask;
  899. }
  900. if (!test_bit(pkg.nd_family, mask))
  901. return -EINVAL;
  902. }
  903. if (!desc ||
  904. (desc->out_num + desc->in_num == 0) ||
  905. cmd > ND_CMD_CALL ||
  906. !test_bit(cmd, &cmd_mask))
  907. return -ENOTTY;
  908. /* fail write commands (when read-only) */
  909. if (read_only)
  910. switch (cmd) {
  911. case ND_CMD_VENDOR:
  912. case ND_CMD_SET_CONFIG_DATA:
  913. case ND_CMD_ARS_START:
  914. case ND_CMD_CLEAR_ERROR:
  915. case ND_CMD_CALL:
  916. dev_dbg(dev, "'%s' command while read-only.\n",
  917. nvdimm ? nvdimm_cmd_name(cmd)
  918. : nvdimm_bus_cmd_name(cmd));
  919. return -EPERM;
  920. default:
  921. break;
  922. }
  923. /* process an input envelope */
  924. in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
  925. if (!in_env)
  926. return -ENOMEM;
  927. for (i = 0; i < desc->in_num; i++) {
  928. u32 in_size, copy;
  929. in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
  930. if (in_size == UINT_MAX) {
  931. dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
  932. __func__, dimm_name, cmd_name, i);
  933. rc = -ENXIO;
  934. goto out;
  935. }
  936. if (in_len < ND_CMD_MAX_ENVELOPE)
  937. copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
  938. else
  939. copy = 0;
  940. if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) {
  941. rc = -EFAULT;
  942. goto out;
  943. }
  944. in_len += in_size;
  945. }
  946. if (cmd == ND_CMD_CALL) {
  947. func = pkg.nd_command;
  948. dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
  949. dimm_name, pkg.nd_command,
  950. in_len, out_len, buf_len);
  951. }
  952. /* process an output envelope */
  953. out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
  954. if (!out_env) {
  955. rc = -ENOMEM;
  956. goto out;
  957. }
  958. for (i = 0; i < desc->out_num; i++) {
  959. u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
  960. (u32 *) in_env, (u32 *) out_env, 0);
  961. u32 copy;
  962. if (out_size == UINT_MAX) {
  963. dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
  964. dimm_name, cmd_name, i);
  965. rc = -EFAULT;
  966. goto out;
  967. }
  968. if (out_len < ND_CMD_MAX_ENVELOPE)
  969. copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
  970. else
  971. copy = 0;
  972. if (copy && copy_from_user(&out_env[out_len],
  973. p + in_len + out_len, copy)) {
  974. rc = -EFAULT;
  975. goto out;
  976. }
  977. out_len += out_size;
  978. }
  979. buf_len = (u64) out_len + (u64) in_len;
  980. if (buf_len > ND_IOCTL_MAX_BUFLEN) {
  981. dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
  982. cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
  983. rc = -EINVAL;
  984. goto out;
  985. }
  986. buf = vmalloc(buf_len);
  987. if (!buf) {
  988. rc = -ENOMEM;
  989. goto out;
  990. }
  991. if (copy_from_user(buf, p, buf_len)) {
  992. rc = -EFAULT;
  993. goto out;
  994. }
  995. device_lock(dev);
  996. nvdimm_bus_lock(dev);
  997. rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
  998. if (rc)
  999. goto out_unlock;
  1000. rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
  1001. if (rc < 0)
  1002. goto out_unlock;
  1003. if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
  1004. struct nd_cmd_clear_error *clear_err = buf;
  1005. nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
  1006. clear_err->cleared);
  1007. }
  1008. if (copy_to_user(p, buf, buf_len))
  1009. rc = -EFAULT;
  1010. out_unlock:
  1011. nvdimm_bus_unlock(dev);
  1012. device_unlock(dev);
  1013. out:
  1014. kfree(in_env);
  1015. kfree(out_env);
  1016. vfree(buf);
  1017. return rc;
  1018. }
  1019. enum nd_ioctl_mode {
  1020. BUS_IOCTL,
  1021. DIMM_IOCTL,
  1022. };
  1023. static int match_dimm(struct device *dev, void *data)
  1024. {
  1025. long id = (long) data;
  1026. if (is_nvdimm(dev)) {
  1027. struct nvdimm *nvdimm = to_nvdimm(dev);
  1028. return nvdimm->id == id;
  1029. }
  1030. return 0;
  1031. }
  1032. static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
  1033. enum nd_ioctl_mode mode)
  1034. {
  1035. struct nvdimm_bus *nvdimm_bus, *found = NULL;
  1036. long id = (long) file->private_data;
  1037. struct nvdimm *nvdimm = NULL;
  1038. int rc, ro;
  1039. ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
  1040. mutex_lock(&nvdimm_bus_list_mutex);
  1041. list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
  1042. if (mode == DIMM_IOCTL) {
  1043. struct device *dev;
  1044. dev = device_find_child(&nvdimm_bus->dev,
  1045. file->private_data, match_dimm);
  1046. if (!dev)
  1047. continue;
  1048. nvdimm = to_nvdimm(dev);
  1049. found = nvdimm_bus;
  1050. } else if (nvdimm_bus->id == id) {
  1051. found = nvdimm_bus;
  1052. }
  1053. if (found) {
  1054. atomic_inc(&nvdimm_bus->ioctl_active);
  1055. break;
  1056. }
  1057. }
  1058. mutex_unlock(&nvdimm_bus_list_mutex);
  1059. if (!found)
  1060. return -ENXIO;
  1061. nvdimm_bus = found;
  1062. rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
  1063. if (nvdimm)
  1064. put_device(&nvdimm->dev);
  1065. if (atomic_dec_and_test(&nvdimm_bus->ioctl_active))
  1066. wake_up(&nvdimm_bus->wait);
  1067. return rc;
  1068. }
  1069. static long bus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1070. {
  1071. return nd_ioctl(file, cmd, arg, BUS_IOCTL);
  1072. }
  1073. static long dimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1074. {
  1075. return nd_ioctl(file, cmd, arg, DIMM_IOCTL);
  1076. }
  1077. static int nd_open(struct inode *inode, struct file *file)
  1078. {
  1079. long minor = iminor(inode);
  1080. file->private_data = (void *) minor;
  1081. return 0;
  1082. }
  1083. static const struct file_operations nvdimm_bus_fops = {
  1084. .owner = THIS_MODULE,
  1085. .open = nd_open,
  1086. .unlocked_ioctl = bus_ioctl,
  1087. .compat_ioctl = compat_ptr_ioctl,
  1088. .llseek = noop_llseek,
  1089. };
  1090. static const struct file_operations nvdimm_fops = {
  1091. .owner = THIS_MODULE,
  1092. .open = nd_open,
  1093. .unlocked_ioctl = dimm_ioctl,
  1094. .compat_ioctl = compat_ptr_ioctl,
  1095. .llseek = noop_llseek,
  1096. };
  1097. int __init nvdimm_bus_init(void)
  1098. {
  1099. int rc;
  1100. rc = bus_register(&nvdimm_bus_type);
  1101. if (rc)
  1102. return rc;
  1103. rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
  1104. if (rc < 0)
  1105. goto err_bus_chrdev;
  1106. nvdimm_bus_major = rc;
  1107. rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
  1108. if (rc < 0)
  1109. goto err_dimm_chrdev;
  1110. nvdimm_major = rc;
  1111. nd_class = class_create(THIS_MODULE, "nd");
  1112. if (IS_ERR(nd_class)) {
  1113. rc = PTR_ERR(nd_class);
  1114. goto err_class;
  1115. }
  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_destroy(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_destroy(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. }