bus.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
  308. rc = device_register(&nvdimm_bus->dev);
  309. if (rc) {
  310. dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
  311. goto err;
  312. }
  313. return nvdimm_bus;
  314. err:
  315. put_device(&nvdimm_bus->dev);
  316. return NULL;
  317. }
  318. EXPORT_SYMBOL_GPL(nvdimm_bus_register);
  319. void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
  320. {
  321. if (!nvdimm_bus)
  322. return;
  323. device_unregister(&nvdimm_bus->dev);
  324. }
  325. EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
  326. static int child_unregister(struct device *dev, void *data)
  327. {
  328. /*
  329. * the singular ndctl class device per bus needs to be
  330. * "device_destroy"ed, so skip it here
  331. *
  332. * i.e. remove classless children
  333. */
  334. if (dev->class)
  335. /* pass */;
  336. else
  337. nd_device_unregister(dev, ND_SYNC);
  338. return 0;
  339. }
  340. static void free_poison_list(struct list_head *poison_list)
  341. {
  342. struct nd_poison *pl, *next;
  343. list_for_each_entry_safe(pl, next, poison_list, list) {
  344. list_del(&pl->list);
  345. kfree(pl);
  346. }
  347. list_del_init(poison_list);
  348. }
  349. static int 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. nd_synchronize();
  356. device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
  357. spin_lock(&nvdimm_bus->poison_lock);
  358. free_poison_list(&nvdimm_bus->poison_list);
  359. spin_unlock(&nvdimm_bus->poison_lock);
  360. nvdimm_bus_destroy_ndctl(nvdimm_bus);
  361. return 0;
  362. }
  363. static int nd_bus_probe(struct device *dev)
  364. {
  365. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  366. int rc;
  367. rc = nvdimm_bus_create_ndctl(nvdimm_bus);
  368. if (rc)
  369. return rc;
  370. mutex_lock(&nvdimm_bus_list_mutex);
  371. list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
  372. mutex_unlock(&nvdimm_bus_list_mutex);
  373. /* enable bus provider attributes to look up their local context */
  374. dev_set_drvdata(dev, nvdimm_bus->nd_desc);
  375. return 0;
  376. }
  377. static struct nd_device_driver nd_bus_driver = {
  378. .probe = nd_bus_probe,
  379. .remove = nd_bus_remove,
  380. .drv = {
  381. .name = "nd_bus",
  382. .suppress_bind_attrs = true,
  383. .bus = &nvdimm_bus_type,
  384. .owner = THIS_MODULE,
  385. .mod_name = KBUILD_MODNAME,
  386. },
  387. };
  388. static int nvdimm_bus_match(struct device *dev, struct device_driver *drv)
  389. {
  390. struct nd_device_driver *nd_drv = to_nd_device_driver(drv);
  391. if (is_nvdimm_bus(dev) && nd_drv == &nd_bus_driver)
  392. return true;
  393. return !!test_bit(to_nd_device_type(dev), &nd_drv->type);
  394. }
  395. static ASYNC_DOMAIN_EXCLUSIVE(nd_async_domain);
  396. void nd_synchronize(void)
  397. {
  398. async_synchronize_full_domain(&nd_async_domain);
  399. }
  400. EXPORT_SYMBOL_GPL(nd_synchronize);
  401. static void nd_async_device_register(void *d, async_cookie_t cookie)
  402. {
  403. struct device *dev = d;
  404. if (device_add(dev) != 0) {
  405. dev_err(dev, "%s: failed\n", __func__);
  406. put_device(dev);
  407. }
  408. put_device(dev);
  409. }
  410. static void nd_async_device_unregister(void *d, async_cookie_t cookie)
  411. {
  412. struct device *dev = d;
  413. /* flush bus operations before delete */
  414. nvdimm_bus_lock(dev);
  415. nvdimm_bus_unlock(dev);
  416. device_unregister(dev);
  417. put_device(dev);
  418. }
  419. void __nd_device_register(struct device *dev)
  420. {
  421. if (!dev)
  422. return;
  423. dev->bus = &nvdimm_bus_type;
  424. get_device(dev);
  425. async_schedule_domain(nd_async_device_register, dev,
  426. &nd_async_domain);
  427. }
  428. void nd_device_register(struct device *dev)
  429. {
  430. device_initialize(dev);
  431. __nd_device_register(dev);
  432. }
  433. EXPORT_SYMBOL(nd_device_register);
  434. void nd_device_unregister(struct device *dev, enum nd_async_mode mode)
  435. {
  436. switch (mode) {
  437. case ND_ASYNC:
  438. get_device(dev);
  439. async_schedule_domain(nd_async_device_unregister, dev,
  440. &nd_async_domain);
  441. break;
  442. case ND_SYNC:
  443. nd_synchronize();
  444. device_unregister(dev);
  445. break;
  446. }
  447. }
  448. EXPORT_SYMBOL(nd_device_unregister);
  449. /**
  450. * __nd_driver_register() - register a region or a namespace driver
  451. * @nd_drv: driver to register
  452. * @owner: automatically set by nd_driver_register() macro
  453. * @mod_name: automatically set by nd_driver_register() macro
  454. */
  455. int __nd_driver_register(struct nd_device_driver *nd_drv, struct module *owner,
  456. const char *mod_name)
  457. {
  458. struct device_driver *drv = &nd_drv->drv;
  459. if (!nd_drv->type) {
  460. pr_debug("driver type bitmask not set (%pf)\n",
  461. __builtin_return_address(0));
  462. return -EINVAL;
  463. }
  464. if (!nd_drv->probe) {
  465. pr_debug("%s ->probe() must be specified\n", mod_name);
  466. return -EINVAL;
  467. }
  468. drv->bus = &nvdimm_bus_type;
  469. drv->owner = owner;
  470. drv->mod_name = mod_name;
  471. return driver_register(drv);
  472. }
  473. EXPORT_SYMBOL(__nd_driver_register);
  474. int nvdimm_revalidate_disk(struct gendisk *disk)
  475. {
  476. struct device *dev = disk_to_dev(disk)->parent;
  477. struct nd_region *nd_region = to_nd_region(dev->parent);
  478. const char *pol = nd_region->ro ? "only" : "write";
  479. if (nd_region->ro == get_disk_ro(disk))
  480. return 0;
  481. dev_info(dev, "%s read-%s, marking %s read-%s\n",
  482. dev_name(&nd_region->dev), pol, disk->disk_name, pol);
  483. set_disk_ro(disk, nd_region->ro);
  484. return 0;
  485. }
  486. EXPORT_SYMBOL(nvdimm_revalidate_disk);
  487. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  488. char *buf)
  489. {
  490. return sprintf(buf, ND_DEVICE_MODALIAS_FMT "\n",
  491. to_nd_device_type(dev));
  492. }
  493. static DEVICE_ATTR_RO(modalias);
  494. static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
  495. char *buf)
  496. {
  497. return sprintf(buf, "%s\n", dev->type->name);
  498. }
  499. static DEVICE_ATTR_RO(devtype);
  500. static struct attribute *nd_device_attributes[] = {
  501. &dev_attr_modalias.attr,
  502. &dev_attr_devtype.attr,
  503. NULL,
  504. };
  505. /**
  506. * nd_device_attribute_group - generic attributes for all devices on an nd bus
  507. */
  508. struct attribute_group nd_device_attribute_group = {
  509. .attrs = nd_device_attributes,
  510. };
  511. EXPORT_SYMBOL_GPL(nd_device_attribute_group);
  512. static ssize_t numa_node_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. return sprintf(buf, "%d\n", dev_to_node(dev));
  516. }
  517. static DEVICE_ATTR_RO(numa_node);
  518. static struct attribute *nd_numa_attributes[] = {
  519. &dev_attr_numa_node.attr,
  520. NULL,
  521. };
  522. static umode_t nd_numa_attr_visible(struct kobject *kobj, struct attribute *a,
  523. int n)
  524. {
  525. if (!IS_ENABLED(CONFIG_NUMA))
  526. return 0;
  527. return a->mode;
  528. }
  529. /**
  530. * nd_numa_attribute_group - NUMA attributes for all devices on an nd bus
  531. */
  532. struct attribute_group nd_numa_attribute_group = {
  533. .attrs = nd_numa_attributes,
  534. .is_visible = nd_numa_attr_visible,
  535. };
  536. EXPORT_SYMBOL_GPL(nd_numa_attribute_group);
  537. int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
  538. {
  539. dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
  540. struct device *dev;
  541. dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
  542. "ndctl%d", nvdimm_bus->id);
  543. if (IS_ERR(dev))
  544. dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
  545. nvdimm_bus->id, PTR_ERR(dev));
  546. return PTR_ERR_OR_ZERO(dev);
  547. }
  548. void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
  549. {
  550. device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
  551. }
  552. static const struct nd_cmd_desc __nd_cmd_dimm_descs[] = {
  553. [ND_CMD_IMPLEMENTED] = { },
  554. [ND_CMD_SMART] = {
  555. .out_num = 2,
  556. .out_sizes = { 4, 128, },
  557. },
  558. [ND_CMD_SMART_THRESHOLD] = {
  559. .out_num = 2,
  560. .out_sizes = { 4, 8, },
  561. },
  562. [ND_CMD_DIMM_FLAGS] = {
  563. .out_num = 2,
  564. .out_sizes = { 4, 4 },
  565. },
  566. [ND_CMD_GET_CONFIG_SIZE] = {
  567. .out_num = 3,
  568. .out_sizes = { 4, 4, 4, },
  569. },
  570. [ND_CMD_GET_CONFIG_DATA] = {
  571. .in_num = 2,
  572. .in_sizes = { 4, 4, },
  573. .out_num = 2,
  574. .out_sizes = { 4, UINT_MAX, },
  575. },
  576. [ND_CMD_SET_CONFIG_DATA] = {
  577. .in_num = 3,
  578. .in_sizes = { 4, 4, UINT_MAX, },
  579. .out_num = 1,
  580. .out_sizes = { 4, },
  581. },
  582. [ND_CMD_VENDOR] = {
  583. .in_num = 3,
  584. .in_sizes = { 4, 4, UINT_MAX, },
  585. .out_num = 3,
  586. .out_sizes = { 4, 4, UINT_MAX, },
  587. },
  588. [ND_CMD_CALL] = {
  589. .in_num = 2,
  590. .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
  591. .out_num = 1,
  592. .out_sizes = { UINT_MAX, },
  593. },
  594. };
  595. const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd)
  596. {
  597. if (cmd < ARRAY_SIZE(__nd_cmd_dimm_descs))
  598. return &__nd_cmd_dimm_descs[cmd];
  599. return NULL;
  600. }
  601. EXPORT_SYMBOL_GPL(nd_cmd_dimm_desc);
  602. static const struct nd_cmd_desc __nd_cmd_bus_descs[] = {
  603. [ND_CMD_IMPLEMENTED] = { },
  604. [ND_CMD_ARS_CAP] = {
  605. .in_num = 2,
  606. .in_sizes = { 8, 8, },
  607. .out_num = 4,
  608. .out_sizes = { 4, 4, 4, 4, },
  609. },
  610. [ND_CMD_ARS_START] = {
  611. .in_num = 5,
  612. .in_sizes = { 8, 8, 2, 1, 5, },
  613. .out_num = 2,
  614. .out_sizes = { 4, 4, },
  615. },
  616. [ND_CMD_ARS_STATUS] = {
  617. .out_num = 3,
  618. .out_sizes = { 4, 4, UINT_MAX, },
  619. },
  620. [ND_CMD_CLEAR_ERROR] = {
  621. .in_num = 2,
  622. .in_sizes = { 8, 8, },
  623. .out_num = 3,
  624. .out_sizes = { 4, 4, 8, },
  625. },
  626. [ND_CMD_CALL] = {
  627. .in_num = 2,
  628. .in_sizes = { sizeof(struct nd_cmd_pkg), UINT_MAX, },
  629. .out_num = 1,
  630. .out_sizes = { UINT_MAX, },
  631. },
  632. };
  633. const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd)
  634. {
  635. if (cmd < ARRAY_SIZE(__nd_cmd_bus_descs))
  636. return &__nd_cmd_bus_descs[cmd];
  637. return NULL;
  638. }
  639. EXPORT_SYMBOL_GPL(nd_cmd_bus_desc);
  640. u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
  641. const struct nd_cmd_desc *desc, int idx, void *buf)
  642. {
  643. if (idx >= desc->in_num)
  644. return UINT_MAX;
  645. if (desc->in_sizes[idx] < UINT_MAX)
  646. return desc->in_sizes[idx];
  647. if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA && idx == 2) {
  648. struct nd_cmd_set_config_hdr *hdr = buf;
  649. return hdr->in_length;
  650. } else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2) {
  651. struct nd_cmd_vendor_hdr *hdr = buf;
  652. return hdr->in_length;
  653. } else if (cmd == ND_CMD_CALL) {
  654. struct nd_cmd_pkg *pkg = buf;
  655. return pkg->nd_size_in;
  656. }
  657. return UINT_MAX;
  658. }
  659. EXPORT_SYMBOL_GPL(nd_cmd_in_size);
  660. u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
  661. const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
  662. const u32 *out_field, unsigned long remainder)
  663. {
  664. if (idx >= desc->out_num)
  665. return UINT_MAX;
  666. if (desc->out_sizes[idx] < UINT_MAX)
  667. return desc->out_sizes[idx];
  668. if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && idx == 1)
  669. return in_field[1];
  670. else if (nvdimm && cmd == ND_CMD_VENDOR && idx == 2)
  671. return out_field[1];
  672. else if (!nvdimm && cmd == ND_CMD_ARS_STATUS && idx == 2) {
  673. /*
  674. * Per table 9-276 ARS Data in ACPI 6.1, out_field[1] is
  675. * "Size of Output Buffer in bytes, including this
  676. * field."
  677. */
  678. if (out_field[1] < 4)
  679. return 0;
  680. /*
  681. * ACPI 6.1 is ambiguous if 'status' is included in the
  682. * output size. If we encounter an output size that
  683. * overshoots the remainder by 4 bytes, assume it was
  684. * including 'status'.
  685. */
  686. if (out_field[1] - 8 == remainder)
  687. return remainder;
  688. return out_field[1] - 4;
  689. } else if (cmd == ND_CMD_CALL) {
  690. struct nd_cmd_pkg *pkg = (struct nd_cmd_pkg *) in_field;
  691. return pkg->nd_size_out;
  692. }
  693. return UINT_MAX;
  694. }
  695. EXPORT_SYMBOL_GPL(nd_cmd_out_size);
  696. void wait_nvdimm_bus_probe_idle(struct device *dev)
  697. {
  698. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  699. do {
  700. if (nvdimm_bus->probe_active == 0)
  701. break;
  702. nvdimm_bus_unlock(&nvdimm_bus->dev);
  703. wait_event(nvdimm_bus->probe_wait,
  704. nvdimm_bus->probe_active == 0);
  705. nvdimm_bus_lock(&nvdimm_bus->dev);
  706. } while (true);
  707. }
  708. static int nd_pmem_forget_poison_check(struct device *dev, void *data)
  709. {
  710. struct nd_cmd_clear_error *clear_err =
  711. (struct nd_cmd_clear_error *)data;
  712. struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
  713. struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
  714. struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
  715. struct nd_namespace_common *ndns = NULL;
  716. struct nd_namespace_io *nsio;
  717. resource_size_t offset = 0, end_trunc = 0, start, end, pstart, pend;
  718. if (nd_dax || !dev->driver)
  719. return 0;
  720. start = clear_err->address;
  721. end = clear_err->address + clear_err->cleared - 1;
  722. if (nd_btt || nd_pfn || nd_dax) {
  723. if (nd_btt)
  724. ndns = nd_btt->ndns;
  725. else if (nd_pfn)
  726. ndns = nd_pfn->ndns;
  727. else if (nd_dax)
  728. ndns = nd_dax->nd_pfn.ndns;
  729. if (!ndns)
  730. return 0;
  731. } else
  732. ndns = to_ndns(dev);
  733. nsio = to_nd_namespace_io(&ndns->dev);
  734. pstart = nsio->res.start + offset;
  735. pend = nsio->res.end - end_trunc;
  736. if ((pstart >= start) && (pend <= end))
  737. return -EBUSY;
  738. return 0;
  739. }
  740. static int nd_ns_forget_poison_check(struct device *dev, void *data)
  741. {
  742. return device_for_each_child(dev, data, nd_pmem_forget_poison_check);
  743. }
  744. /* set_config requires an idle interleave set */
  745. static int nd_cmd_clear_to_send(struct nvdimm_bus *nvdimm_bus,
  746. struct nvdimm *nvdimm, unsigned int cmd, void *data)
  747. {
  748. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  749. /* ask the bus provider if it would like to block this request */
  750. if (nd_desc->clear_to_send) {
  751. int rc = nd_desc->clear_to_send(nd_desc, nvdimm, cmd);
  752. if (rc)
  753. return rc;
  754. }
  755. /* require clear error to go through the pmem driver */
  756. if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR)
  757. return device_for_each_child(&nvdimm_bus->dev, data,
  758. nd_ns_forget_poison_check);
  759. if (!nvdimm || cmd != ND_CMD_SET_CONFIG_DATA)
  760. return 0;
  761. /* prevent label manipulation while the kernel owns label updates */
  762. wait_nvdimm_bus_probe_idle(&nvdimm_bus->dev);
  763. if (atomic_read(&nvdimm->busy))
  764. return -EBUSY;
  765. return 0;
  766. }
  767. static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
  768. int read_only, unsigned int ioctl_cmd, unsigned long arg)
  769. {
  770. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  771. static char out_env[ND_CMD_MAX_ENVELOPE];
  772. static char in_env[ND_CMD_MAX_ENVELOPE];
  773. const struct nd_cmd_desc *desc = NULL;
  774. unsigned int cmd = _IOC_NR(ioctl_cmd);
  775. struct device *dev = &nvdimm_bus->dev;
  776. void __user *p = (void __user *) arg;
  777. const char *cmd_name, *dimm_name;
  778. u32 in_len = 0, out_len = 0;
  779. unsigned int func = cmd;
  780. unsigned long cmd_mask;
  781. struct nd_cmd_pkg pkg;
  782. int rc, i, cmd_rc;
  783. u64 buf_len = 0;
  784. void *buf;
  785. if (nvdimm) {
  786. desc = nd_cmd_dimm_desc(cmd);
  787. cmd_name = nvdimm_cmd_name(cmd);
  788. cmd_mask = nvdimm->cmd_mask;
  789. dimm_name = dev_name(&nvdimm->dev);
  790. } else {
  791. desc = nd_cmd_bus_desc(cmd);
  792. cmd_name = nvdimm_bus_cmd_name(cmd);
  793. cmd_mask = nd_desc->cmd_mask;
  794. dimm_name = "bus";
  795. }
  796. if (cmd == ND_CMD_CALL) {
  797. if (copy_from_user(&pkg, p, sizeof(pkg)))
  798. return -EFAULT;
  799. }
  800. if (!desc || (desc->out_num + desc->in_num == 0) ||
  801. !test_bit(cmd, &cmd_mask))
  802. return -ENOTTY;
  803. /* fail write commands (when read-only) */
  804. if (read_only)
  805. switch (cmd) {
  806. case ND_CMD_VENDOR:
  807. case ND_CMD_SET_CONFIG_DATA:
  808. case ND_CMD_ARS_START:
  809. case ND_CMD_CLEAR_ERROR:
  810. case ND_CMD_CALL:
  811. dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n",
  812. nvdimm ? nvdimm_cmd_name(cmd)
  813. : nvdimm_bus_cmd_name(cmd));
  814. return -EPERM;
  815. default:
  816. break;
  817. }
  818. /* process an input envelope */
  819. for (i = 0; i < desc->in_num; i++) {
  820. u32 in_size, copy;
  821. in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
  822. if (in_size == UINT_MAX) {
  823. dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
  824. __func__, dimm_name, cmd_name, i);
  825. return -ENXIO;
  826. }
  827. if (in_len < sizeof(in_env))
  828. copy = min_t(u32, sizeof(in_env) - in_len, in_size);
  829. else
  830. copy = 0;
  831. if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
  832. return -EFAULT;
  833. in_len += in_size;
  834. }
  835. if (cmd == ND_CMD_CALL) {
  836. func = pkg.nd_command;
  837. dev_dbg(dev, "%s:%s, idx: %llu, in: %u, out: %u, len %llu\n",
  838. __func__, dimm_name, pkg.nd_command,
  839. in_len, out_len, buf_len);
  840. }
  841. /* process an output envelope */
  842. for (i = 0; i < desc->out_num; i++) {
  843. u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
  844. (u32 *) in_env, (u32 *) out_env, 0);
  845. u32 copy;
  846. if (out_size == UINT_MAX) {
  847. dev_dbg(dev, "%s:%s unknown output size cmd: %s field: %d\n",
  848. __func__, dimm_name, cmd_name, i);
  849. return -EFAULT;
  850. }
  851. if (out_len < sizeof(out_env))
  852. copy = min_t(u32, sizeof(out_env) - out_len, out_size);
  853. else
  854. copy = 0;
  855. if (copy && copy_from_user(&out_env[out_len],
  856. p + in_len + out_len, copy))
  857. return -EFAULT;
  858. out_len += out_size;
  859. }
  860. buf_len = (u64) out_len + (u64) in_len;
  861. if (buf_len > ND_IOCTL_MAX_BUFLEN) {
  862. dev_dbg(dev, "%s:%s cmd: %s buf_len: %llu > %d\n", __func__,
  863. dimm_name, cmd_name, buf_len,
  864. ND_IOCTL_MAX_BUFLEN);
  865. return -EINVAL;
  866. }
  867. buf = vmalloc(buf_len);
  868. if (!buf)
  869. return -ENOMEM;
  870. if (copy_from_user(buf, p, buf_len)) {
  871. rc = -EFAULT;
  872. goto out;
  873. }
  874. nvdimm_bus_lock(&nvdimm_bus->dev);
  875. rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
  876. if (rc)
  877. goto out_unlock;
  878. rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
  879. if (rc < 0)
  880. goto out_unlock;
  881. if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
  882. struct nd_cmd_clear_error *clear_err = buf;
  883. nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
  884. clear_err->cleared);
  885. }
  886. nvdimm_bus_unlock(&nvdimm_bus->dev);
  887. if (copy_to_user(p, buf, buf_len))
  888. rc = -EFAULT;
  889. vfree(buf);
  890. return rc;
  891. out_unlock:
  892. nvdimm_bus_unlock(&nvdimm_bus->dev);
  893. out:
  894. vfree(buf);
  895. return rc;
  896. }
  897. static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  898. {
  899. long id = (long) file->private_data;
  900. int rc = -ENXIO, ro;
  901. struct nvdimm_bus *nvdimm_bus;
  902. ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
  903. mutex_lock(&nvdimm_bus_list_mutex);
  904. list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
  905. if (nvdimm_bus->id == id) {
  906. rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg);
  907. break;
  908. }
  909. }
  910. mutex_unlock(&nvdimm_bus_list_mutex);
  911. return rc;
  912. }
  913. static int match_dimm(struct device *dev, void *data)
  914. {
  915. long id = (long) data;
  916. if (is_nvdimm(dev)) {
  917. struct nvdimm *nvdimm = to_nvdimm(dev);
  918. return nvdimm->id == id;
  919. }
  920. return 0;
  921. }
  922. static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  923. {
  924. int rc = -ENXIO, ro;
  925. struct nvdimm_bus *nvdimm_bus;
  926. ro = ((file->f_flags & O_ACCMODE) == O_RDONLY);
  927. mutex_lock(&nvdimm_bus_list_mutex);
  928. list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) {
  929. struct device *dev = device_find_child(&nvdimm_bus->dev,
  930. file->private_data, match_dimm);
  931. struct nvdimm *nvdimm;
  932. if (!dev)
  933. continue;
  934. nvdimm = to_nvdimm(dev);
  935. rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg);
  936. put_device(dev);
  937. break;
  938. }
  939. mutex_unlock(&nvdimm_bus_list_mutex);
  940. return rc;
  941. }
  942. static int nd_open(struct inode *inode, struct file *file)
  943. {
  944. long minor = iminor(inode);
  945. file->private_data = (void *) minor;
  946. return 0;
  947. }
  948. static const struct file_operations nvdimm_bus_fops = {
  949. .owner = THIS_MODULE,
  950. .open = nd_open,
  951. .unlocked_ioctl = nd_ioctl,
  952. .compat_ioctl = nd_ioctl,
  953. .llseek = noop_llseek,
  954. };
  955. static const struct file_operations nvdimm_fops = {
  956. .owner = THIS_MODULE,
  957. .open = nd_open,
  958. .unlocked_ioctl = nvdimm_ioctl,
  959. .compat_ioctl = nvdimm_ioctl,
  960. .llseek = noop_llseek,
  961. };
  962. int __init nvdimm_bus_init(void)
  963. {
  964. int rc;
  965. BUILD_BUG_ON(sizeof(struct nd_smart_payload) != 128);
  966. BUILD_BUG_ON(sizeof(struct nd_smart_threshold_payload) != 8);
  967. rc = bus_register(&nvdimm_bus_type);
  968. if (rc)
  969. return rc;
  970. rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
  971. if (rc < 0)
  972. goto err_bus_chrdev;
  973. nvdimm_bus_major = rc;
  974. rc = register_chrdev(0, "dimmctl", &nvdimm_fops);
  975. if (rc < 0)
  976. goto err_dimm_chrdev;
  977. nvdimm_major = rc;
  978. nd_class = class_create(THIS_MODULE, "nd");
  979. if (IS_ERR(nd_class)) {
  980. rc = PTR_ERR(nd_class);
  981. goto err_class;
  982. }
  983. rc = driver_register(&nd_bus_driver.drv);
  984. if (rc)
  985. goto err_nd_bus;
  986. return 0;
  987. err_nd_bus:
  988. class_destroy(nd_class);
  989. err_class:
  990. unregister_chrdev(nvdimm_major, "dimmctl");
  991. err_dimm_chrdev:
  992. unregister_chrdev(nvdimm_bus_major, "ndctl");
  993. err_bus_chrdev:
  994. bus_unregister(&nvdimm_bus_type);
  995. return rc;
  996. }
  997. void nvdimm_bus_exit(void)
  998. {
  999. driver_unregister(&nd_bus_driver.drv);
  1000. class_destroy(nd_class);
  1001. unregister_chrdev(nvdimm_bus_major, "ndctl");
  1002. unregister_chrdev(nvdimm_major, "dimmctl");
  1003. bus_unregister(&nvdimm_bus_type);
  1004. ida_destroy(&nd_ida);
  1005. }