bus.c 26 KB

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