core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #include <linux/libnvdimm.h>
  14. #include <linux/export.h>
  15. #include <linux/module.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/device.h>
  18. #include <linux/ctype.h>
  19. #include <linux/ndctl.h>
  20. #include <linux/mutex.h>
  21. #include <linux/slab.h>
  22. #include "nd-core.h"
  23. #include "nd.h"
  24. LIST_HEAD(nvdimm_bus_list);
  25. DEFINE_MUTEX(nvdimm_bus_list_mutex);
  26. static DEFINE_IDA(nd_ida);
  27. void nvdimm_bus_lock(struct device *dev)
  28. {
  29. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  30. if (!nvdimm_bus)
  31. return;
  32. mutex_lock(&nvdimm_bus->reconfig_mutex);
  33. }
  34. EXPORT_SYMBOL(nvdimm_bus_lock);
  35. void nvdimm_bus_unlock(struct device *dev)
  36. {
  37. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  38. if (!nvdimm_bus)
  39. return;
  40. mutex_unlock(&nvdimm_bus->reconfig_mutex);
  41. }
  42. EXPORT_SYMBOL(nvdimm_bus_unlock);
  43. bool is_nvdimm_bus_locked(struct device *dev)
  44. {
  45. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  46. if (!nvdimm_bus)
  47. return false;
  48. return mutex_is_locked(&nvdimm_bus->reconfig_mutex);
  49. }
  50. EXPORT_SYMBOL(is_nvdimm_bus_locked);
  51. u64 nd_fletcher64(void *addr, size_t len, bool le)
  52. {
  53. u32 *buf = addr;
  54. u32 lo32 = 0;
  55. u64 hi32 = 0;
  56. int i;
  57. for (i = 0; i < len / sizeof(u32); i++) {
  58. lo32 += le ? le32_to_cpu((__le32) buf[i]) : buf[i];
  59. hi32 += lo32;
  60. }
  61. return hi32 << 32 | lo32;
  62. }
  63. EXPORT_SYMBOL_GPL(nd_fletcher64);
  64. static void nvdimm_bus_release(struct device *dev)
  65. {
  66. struct nvdimm_bus *nvdimm_bus;
  67. nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
  68. ida_simple_remove(&nd_ida, nvdimm_bus->id);
  69. kfree(nvdimm_bus);
  70. }
  71. struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
  72. {
  73. struct nvdimm_bus *nvdimm_bus;
  74. nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
  75. WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release);
  76. return nvdimm_bus;
  77. }
  78. EXPORT_SYMBOL_GPL(to_nvdimm_bus);
  79. struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus)
  80. {
  81. /* struct nvdimm_bus definition is private to libnvdimm */
  82. return nvdimm_bus->nd_desc;
  83. }
  84. EXPORT_SYMBOL_GPL(to_nd_desc);
  85. struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
  86. {
  87. struct device *dev;
  88. for (dev = nd_dev; dev; dev = dev->parent)
  89. if (dev->release == nvdimm_bus_release)
  90. break;
  91. dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
  92. if (dev)
  93. return to_nvdimm_bus(dev);
  94. return NULL;
  95. }
  96. static bool is_uuid_sep(char sep)
  97. {
  98. if (sep == '\n' || sep == '-' || sep == ':' || sep == '\0')
  99. return true;
  100. return false;
  101. }
  102. static int nd_uuid_parse(struct device *dev, u8 *uuid_out, const char *buf,
  103. size_t len)
  104. {
  105. const char *str = buf;
  106. u8 uuid[16];
  107. int i;
  108. for (i = 0; i < 16; i++) {
  109. if (!isxdigit(str[0]) || !isxdigit(str[1])) {
  110. dev_dbg(dev, "%s: pos: %d buf[%zd]: %c buf[%zd]: %c\n",
  111. __func__, i, str - buf, str[0],
  112. str + 1 - buf, str[1]);
  113. return -EINVAL;
  114. }
  115. uuid[i] = (hex_to_bin(str[0]) << 4) | hex_to_bin(str[1]);
  116. str += 2;
  117. if (is_uuid_sep(*str))
  118. str++;
  119. }
  120. memcpy(uuid_out, uuid, sizeof(uuid));
  121. return 0;
  122. }
  123. /**
  124. * nd_uuid_store: common implementation for writing 'uuid' sysfs attributes
  125. * @dev: container device for the uuid property
  126. * @uuid_out: uuid buffer to replace
  127. * @buf: raw sysfs buffer to parse
  128. *
  129. * Enforce that uuids can only be changed while the device is disabled
  130. * (driver detached)
  131. * LOCKING: expects device_lock() is held on entry
  132. */
  133. int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
  134. size_t len)
  135. {
  136. u8 uuid[16];
  137. int rc;
  138. if (dev->driver)
  139. return -EBUSY;
  140. rc = nd_uuid_parse(dev, uuid, buf, len);
  141. if (rc)
  142. return rc;
  143. kfree(*uuid_out);
  144. *uuid_out = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
  145. if (!(*uuid_out))
  146. return -ENOMEM;
  147. return 0;
  148. }
  149. ssize_t nd_sector_size_show(unsigned long current_lbasize,
  150. const unsigned long *supported, char *buf)
  151. {
  152. ssize_t len = 0;
  153. int i;
  154. for (i = 0; supported[i]; i++)
  155. if (current_lbasize == supported[i])
  156. len += sprintf(buf + len, "[%ld] ", supported[i]);
  157. else
  158. len += sprintf(buf + len, "%ld ", supported[i]);
  159. len += sprintf(buf + len, "\n");
  160. return len;
  161. }
  162. ssize_t nd_sector_size_store(struct device *dev, const char *buf,
  163. unsigned long *current_lbasize, const unsigned long *supported)
  164. {
  165. unsigned long lbasize;
  166. int rc, i;
  167. if (dev->driver)
  168. return -EBUSY;
  169. rc = kstrtoul(buf, 0, &lbasize);
  170. if (rc)
  171. return rc;
  172. for (i = 0; supported[i]; i++)
  173. if (lbasize == supported[i])
  174. break;
  175. if (supported[i]) {
  176. *current_lbasize = lbasize;
  177. return 0;
  178. } else {
  179. return -EINVAL;
  180. }
  181. }
  182. void __nd_iostat_start(struct bio *bio, unsigned long *start)
  183. {
  184. struct gendisk *disk = bio->bi_bdev->bd_disk;
  185. const int rw = bio_data_dir(bio);
  186. int cpu = part_stat_lock();
  187. *start = jiffies;
  188. part_round_stats(cpu, &disk->part0);
  189. part_stat_inc(cpu, &disk->part0, ios[rw]);
  190. part_stat_add(cpu, &disk->part0, sectors[rw], bio_sectors(bio));
  191. part_inc_in_flight(&disk->part0, rw);
  192. part_stat_unlock();
  193. }
  194. EXPORT_SYMBOL(__nd_iostat_start);
  195. void nd_iostat_end(struct bio *bio, unsigned long start)
  196. {
  197. struct gendisk *disk = bio->bi_bdev->bd_disk;
  198. unsigned long duration = jiffies - start;
  199. const int rw = bio_data_dir(bio);
  200. int cpu = part_stat_lock();
  201. part_stat_add(cpu, &disk->part0, ticks[rw], duration);
  202. part_round_stats(cpu, &disk->part0);
  203. part_dec_in_flight(&disk->part0, rw);
  204. part_stat_unlock();
  205. }
  206. EXPORT_SYMBOL(nd_iostat_end);
  207. static ssize_t commands_show(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. int cmd, len = 0;
  211. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  212. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  213. for_each_set_bit(cmd, &nd_desc->dsm_mask, BITS_PER_LONG)
  214. len += sprintf(buf + len, "%s ", nvdimm_bus_cmd_name(cmd));
  215. len += sprintf(buf + len, "\n");
  216. return len;
  217. }
  218. static DEVICE_ATTR_RO(commands);
  219. static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
  220. {
  221. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  222. struct device *parent = nvdimm_bus->dev.parent;
  223. if (nd_desc->provider_name)
  224. return nd_desc->provider_name;
  225. else if (parent)
  226. return dev_name(parent);
  227. else
  228. return "unknown";
  229. }
  230. static ssize_t provider_show(struct device *dev,
  231. struct device_attribute *attr, char *buf)
  232. {
  233. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  234. return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
  235. }
  236. static DEVICE_ATTR_RO(provider);
  237. static int flush_namespaces(struct device *dev, void *data)
  238. {
  239. device_lock(dev);
  240. device_unlock(dev);
  241. return 0;
  242. }
  243. static int flush_regions_dimms(struct device *dev, void *data)
  244. {
  245. device_lock(dev);
  246. device_unlock(dev);
  247. device_for_each_child(dev, NULL, flush_namespaces);
  248. return 0;
  249. }
  250. static ssize_t wait_probe_show(struct device *dev,
  251. struct device_attribute *attr, char *buf)
  252. {
  253. nd_synchronize();
  254. device_for_each_child(dev, NULL, flush_regions_dimms);
  255. return sprintf(buf, "1\n");
  256. }
  257. static DEVICE_ATTR_RO(wait_probe);
  258. static struct attribute *nvdimm_bus_attributes[] = {
  259. &dev_attr_commands.attr,
  260. &dev_attr_wait_probe.attr,
  261. &dev_attr_provider.attr,
  262. NULL,
  263. };
  264. struct attribute_group nvdimm_bus_attribute_group = {
  265. .attrs = nvdimm_bus_attributes,
  266. };
  267. EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
  268. struct nvdimm_bus *__nvdimm_bus_register(struct device *parent,
  269. struct nvdimm_bus_descriptor *nd_desc, struct module *module)
  270. {
  271. struct nvdimm_bus *nvdimm_bus;
  272. int rc;
  273. nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
  274. if (!nvdimm_bus)
  275. return NULL;
  276. INIT_LIST_HEAD(&nvdimm_bus->list);
  277. init_waitqueue_head(&nvdimm_bus->probe_wait);
  278. nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
  279. mutex_init(&nvdimm_bus->reconfig_mutex);
  280. if (nvdimm_bus->id < 0) {
  281. kfree(nvdimm_bus);
  282. return NULL;
  283. }
  284. nvdimm_bus->nd_desc = nd_desc;
  285. nvdimm_bus->module = module;
  286. nvdimm_bus->dev.parent = parent;
  287. nvdimm_bus->dev.release = nvdimm_bus_release;
  288. nvdimm_bus->dev.groups = nd_desc->attr_groups;
  289. dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
  290. rc = device_register(&nvdimm_bus->dev);
  291. if (rc) {
  292. dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
  293. goto err;
  294. }
  295. rc = nvdimm_bus_create_ndctl(nvdimm_bus);
  296. if (rc)
  297. goto err;
  298. mutex_lock(&nvdimm_bus_list_mutex);
  299. list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
  300. mutex_unlock(&nvdimm_bus_list_mutex);
  301. return nvdimm_bus;
  302. err:
  303. put_device(&nvdimm_bus->dev);
  304. return NULL;
  305. }
  306. EXPORT_SYMBOL_GPL(__nvdimm_bus_register);
  307. static int child_unregister(struct device *dev, void *data)
  308. {
  309. /*
  310. * the singular ndctl class device per bus needs to be
  311. * "device_destroy"ed, so skip it here
  312. *
  313. * i.e. remove classless children
  314. */
  315. if (dev->class)
  316. /* pass */;
  317. else
  318. nd_device_unregister(dev, ND_SYNC);
  319. return 0;
  320. }
  321. void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
  322. {
  323. if (!nvdimm_bus)
  324. return;
  325. mutex_lock(&nvdimm_bus_list_mutex);
  326. list_del_init(&nvdimm_bus->list);
  327. mutex_unlock(&nvdimm_bus_list_mutex);
  328. nd_synchronize();
  329. device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
  330. nvdimm_bus_destroy_ndctl(nvdimm_bus);
  331. device_unregister(&nvdimm_bus->dev);
  332. }
  333. EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
  334. #ifdef CONFIG_BLK_DEV_INTEGRITY
  335. int nd_integrity_init(struct gendisk *disk, unsigned long meta_size)
  336. {
  337. struct blk_integrity bi;
  338. if (meta_size == 0)
  339. return 0;
  340. bi.profile = NULL;
  341. bi.tuple_size = meta_size;
  342. bi.tag_size = meta_size;
  343. blk_integrity_register(disk, &bi);
  344. blk_queue_max_integrity_segments(disk->queue, 1);
  345. return 0;
  346. }
  347. EXPORT_SYMBOL(nd_integrity_init);
  348. #else /* CONFIG_BLK_DEV_INTEGRITY */
  349. int nd_integrity_init(struct gendisk *disk, unsigned long meta_size)
  350. {
  351. return 0;
  352. }
  353. EXPORT_SYMBOL(nd_integrity_init);
  354. #endif
  355. static __init int libnvdimm_init(void)
  356. {
  357. int rc;
  358. rc = nvdimm_bus_init();
  359. if (rc)
  360. return rc;
  361. rc = nvdimm_init();
  362. if (rc)
  363. goto err_dimm;
  364. rc = nd_region_init();
  365. if (rc)
  366. goto err_region;
  367. return 0;
  368. err_region:
  369. nvdimm_exit();
  370. err_dimm:
  371. nvdimm_bus_exit();
  372. return rc;
  373. }
  374. static __exit void libnvdimm_exit(void)
  375. {
  376. WARN_ON(!list_empty(&nvdimm_bus_list));
  377. nd_region_exit();
  378. nvdimm_exit();
  379. nvdimm_bus_exit();
  380. }
  381. MODULE_LICENSE("GPL v2");
  382. MODULE_AUTHOR("Intel Corporation");
  383. subsys_initcall(libnvdimm_init);
  384. module_exit(libnvdimm_exit);