bus.c 26 KB

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