bus.c 25 KB

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