bus.c 25 KB

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