bus.c 28 KB

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