bus.c 28 KB

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