nd.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #ifndef __ND_H__
  14. #define __ND_H__
  15. #include <linux/libnvdimm.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/device.h>
  18. #include <linux/mutex.h>
  19. #include <linux/ndctl.h>
  20. #include <linux/types.h>
  21. #include "label.h"
  22. enum {
  23. /*
  24. * Limits the maximum number of block apertures a dimm can
  25. * support and is an input to the geometry/on-disk-format of a
  26. * BTT instance
  27. */
  28. ND_MAX_LANES = 256,
  29. SECTOR_SHIFT = 9,
  30. INT_LBASIZE_ALIGNMENT = 64,
  31. #if IS_ENABLED(CONFIG_NVDIMM_PFN)
  32. ND_PFN_ALIGN = PAGES_PER_SECTION * PAGE_SIZE,
  33. ND_PFN_MASK = ND_PFN_ALIGN - 1,
  34. #else
  35. ND_PFN_ALIGN = 0,
  36. ND_PFN_MASK = 0,
  37. #endif
  38. };
  39. struct nvdimm_drvdata {
  40. struct device *dev;
  41. int nsindex_size;
  42. struct nd_cmd_get_config_size nsarea;
  43. void *data;
  44. int ns_current, ns_next;
  45. struct resource dpa;
  46. struct kref kref;
  47. };
  48. struct nd_region_namespaces {
  49. int count;
  50. int active;
  51. };
  52. static inline struct nd_namespace_index *to_namespace_index(
  53. struct nvdimm_drvdata *ndd, int i)
  54. {
  55. if (i < 0)
  56. return NULL;
  57. return ndd->data + sizeof_namespace_index(ndd) * i;
  58. }
  59. static inline struct nd_namespace_index *to_current_namespace_index(
  60. struct nvdimm_drvdata *ndd)
  61. {
  62. return to_namespace_index(ndd, ndd->ns_current);
  63. }
  64. static inline struct nd_namespace_index *to_next_namespace_index(
  65. struct nvdimm_drvdata *ndd)
  66. {
  67. return to_namespace_index(ndd, ndd->ns_next);
  68. }
  69. #define nd_dbg_dpa(r, d, res, fmt, arg...) \
  70. dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \
  71. (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \
  72. (unsigned long long) (res ? resource_size(res) : 0), \
  73. (unsigned long long) (res ? res->start : 0), ##arg)
  74. #define for_each_label(l, label, labels) \
  75. for (l = 0; (label = labels ? labels[l] : NULL); l++)
  76. #define for_each_dpa_resource(ndd, res) \
  77. for (res = (ndd)->dpa.child; res; res = res->sibling)
  78. #define for_each_dpa_resource_safe(ndd, res, next) \
  79. for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \
  80. res; res = next, next = next ? next->sibling : NULL)
  81. struct nd_percpu_lane {
  82. int count;
  83. spinlock_t lock;
  84. };
  85. struct nd_region {
  86. struct device dev;
  87. struct ida ns_ida;
  88. struct ida btt_ida;
  89. struct ida pfn_ida;
  90. unsigned long flags;
  91. struct device *ns_seed;
  92. struct device *btt_seed;
  93. struct device *pfn_seed;
  94. u16 ndr_mappings;
  95. u64 ndr_size;
  96. u64 ndr_start;
  97. int id, num_lanes, ro, numa_node;
  98. void *provider_data;
  99. struct nd_interleave_set *nd_set;
  100. struct nd_percpu_lane __percpu *lane;
  101. struct nd_mapping mapping[0];
  102. };
  103. struct nd_blk_region {
  104. int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
  105. void (*disable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
  106. int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
  107. void *iobuf, u64 len, int rw);
  108. void *blk_provider_data;
  109. struct nd_region nd_region;
  110. };
  111. /*
  112. * Lookup next in the repeating sequence of 01, 10, and 11.
  113. */
  114. static inline unsigned nd_inc_seq(unsigned seq)
  115. {
  116. static const unsigned next[] = { 0, 2, 3, 1 };
  117. return next[seq & 3];
  118. }
  119. struct btt;
  120. struct nd_btt {
  121. struct device dev;
  122. struct nd_namespace_common *ndns;
  123. struct btt *btt;
  124. unsigned long lbasize;
  125. u8 *uuid;
  126. int id;
  127. };
  128. enum nd_pfn_mode {
  129. PFN_MODE_NONE,
  130. PFN_MODE_RAM,
  131. PFN_MODE_PMEM,
  132. };
  133. struct nd_pfn {
  134. int id;
  135. u8 *uuid;
  136. struct device dev;
  137. unsigned long npfns;
  138. enum nd_pfn_mode mode;
  139. struct nd_pfn_sb *pfn_sb;
  140. struct nd_namespace_common *ndns;
  141. };
  142. enum nd_async_mode {
  143. ND_SYNC,
  144. ND_ASYNC,
  145. };
  146. int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
  147. void wait_nvdimm_bus_probe_idle(struct device *dev);
  148. void nd_device_register(struct device *dev);
  149. void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
  150. int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
  151. size_t len);
  152. ssize_t nd_sector_size_show(unsigned long current_lbasize,
  153. const unsigned long *supported, char *buf);
  154. ssize_t nd_sector_size_store(struct device *dev, const char *buf,
  155. unsigned long *current_lbasize, const unsigned long *supported);
  156. int __init nvdimm_init(void);
  157. int __init nd_region_init(void);
  158. void nvdimm_exit(void);
  159. void nd_region_exit(void);
  160. struct nvdimm;
  161. struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
  162. int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
  163. int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
  164. int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
  165. void *buf, size_t len);
  166. struct nd_btt *to_nd_btt(struct device *dev);
  167. struct nd_gen_sb {
  168. char reserved[SZ_4K - 8];
  169. __le64 checksum;
  170. };
  171. u64 nd_sb_checksum(struct nd_gen_sb *sb);
  172. #if IS_ENABLED(CONFIG_BTT)
  173. int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata);
  174. bool is_nd_btt(struct device *dev);
  175. struct device *nd_btt_create(struct nd_region *nd_region);
  176. #else
  177. static inline int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata)
  178. {
  179. return -ENODEV;
  180. }
  181. static inline bool is_nd_btt(struct device *dev)
  182. {
  183. return false;
  184. }
  185. static inline struct device *nd_btt_create(struct nd_region *nd_region)
  186. {
  187. return NULL;
  188. }
  189. #endif
  190. struct nd_pfn *to_nd_pfn(struct device *dev);
  191. #if IS_ENABLED(CONFIG_NVDIMM_PFN)
  192. int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata);
  193. bool is_nd_pfn(struct device *dev);
  194. struct device *nd_pfn_create(struct nd_region *nd_region);
  195. int nd_pfn_validate(struct nd_pfn *nd_pfn);
  196. #else
  197. static inline int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
  198. {
  199. return -ENODEV;
  200. }
  201. static inline bool is_nd_pfn(struct device *dev)
  202. {
  203. return false;
  204. }
  205. static inline struct device *nd_pfn_create(struct nd_region *nd_region)
  206. {
  207. return NULL;
  208. }
  209. static inline int nd_pfn_validate(struct nd_pfn *nd_pfn)
  210. {
  211. return -ENODEV;
  212. }
  213. #endif
  214. struct nd_region *to_nd_region(struct device *dev);
  215. int nd_region_to_nstype(struct nd_region *nd_region);
  216. int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
  217. u64 nd_region_interleave_set_cookie(struct nd_region *nd_region);
  218. u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
  219. void nvdimm_bus_lock(struct device *dev);
  220. void nvdimm_bus_unlock(struct device *dev);
  221. bool is_nvdimm_bus_locked(struct device *dev);
  222. int nvdimm_revalidate_disk(struct gendisk *disk);
  223. void nvdimm_drvdata_release(struct kref *kref);
  224. void put_ndd(struct nvdimm_drvdata *ndd);
  225. int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
  226. void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
  227. struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
  228. struct nd_label_id *label_id, resource_size_t start,
  229. resource_size_t n);
  230. resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
  231. struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
  232. int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
  233. int nvdimm_namespace_detach_btt(struct nd_namespace_common *ndns);
  234. const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
  235. char *name);
  236. int nd_blk_region_init(struct nd_region *nd_region);
  237. void __nd_iostat_start(struct bio *bio, unsigned long *start);
  238. static inline bool nd_iostat_start(struct bio *bio, unsigned long *start)
  239. {
  240. struct gendisk *disk = bio->bi_bdev->bd_disk;
  241. if (!blk_queue_io_stat(disk->queue))
  242. return false;
  243. __nd_iostat_start(bio, start);
  244. return true;
  245. }
  246. void nd_iostat_end(struct bio *bio, unsigned long start);
  247. resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk);
  248. const u8 *nd_dev_to_uuid(struct device *dev);
  249. bool pmem_should_map_pages(struct device *dev);
  250. #endif /* __ND_H__ */