libnvdimm.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * libnvdimm - Non-volatile-memory Devices Subsystem
  4. *
  5. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  6. */
  7. #ifndef __LIBNVDIMM_H__
  8. #define __LIBNVDIMM_H__
  9. #include <linux/kernel.h>
  10. #include <linux/sizes.h>
  11. #include <linux/types.h>
  12. #include <linux/uuid.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/bio.h>
  15. struct badrange_entry {
  16. u64 start;
  17. u64 length;
  18. struct list_head list;
  19. };
  20. struct badrange {
  21. struct list_head list;
  22. spinlock_t lock;
  23. };
  24. enum {
  25. /* when a dimm supports both PMEM and BLK access a label is required */
  26. NDD_ALIASING = 0,
  27. /* unarmed memory devices may not persist writes */
  28. NDD_UNARMED = 1,
  29. /* locked memory devices should not be accessed */
  30. NDD_LOCKED = 2,
  31. /* memory under security wipes should not be accessed */
  32. NDD_SECURITY_OVERWRITE = 3,
  33. /* tracking whether or not there is a pending device reference */
  34. NDD_WORK_PENDING = 4,
  35. /* ignore / filter NSLABEL_FLAG_LOCAL for this DIMM, i.e. no aliasing */
  36. NDD_NOBLK = 5,
  37. /* dimm supports namespace labels */
  38. NDD_LABELING = 6,
  39. /* need to set a limit somewhere, but yes, this is likely overkill */
  40. ND_IOCTL_MAX_BUFLEN = SZ_4M,
  41. ND_CMD_MAX_ELEM = 5,
  42. ND_CMD_MAX_ENVELOPE = 256,
  43. ND_MAX_MAPPINGS = 32,
  44. /* region flag indicating to direct-map persistent memory by default */
  45. ND_REGION_PAGEMAP = 0,
  46. /*
  47. * Platform ensures entire CPU store data path is flushed to pmem on
  48. * system power loss.
  49. */
  50. ND_REGION_PERSIST_CACHE = 1,
  51. /*
  52. * Platform provides mechanisms to automatically flush outstanding
  53. * write data from memory controler to pmem on system power loss.
  54. * (ADR)
  55. */
  56. ND_REGION_PERSIST_MEMCTRL = 2,
  57. /* Platform provides asynchronous flush mechanism */
  58. ND_REGION_ASYNC = 3,
  59. /* mark newly adjusted resources as requiring a label update */
  60. DPA_RESOURCE_ADJUSTED = 1 << 0,
  61. };
  62. struct nvdimm;
  63. struct nvdimm_bus_descriptor;
  64. typedef int (*ndctl_fn)(struct nvdimm_bus_descriptor *nd_desc,
  65. struct nvdimm *nvdimm, unsigned int cmd, void *buf,
  66. unsigned int buf_len, int *cmd_rc);
  67. struct device_node;
  68. struct nvdimm_bus_descriptor {
  69. const struct attribute_group **attr_groups;
  70. unsigned long cmd_mask;
  71. unsigned long dimm_family_mask;
  72. unsigned long bus_family_mask;
  73. struct module *module;
  74. char *provider_name;
  75. struct device_node *of_node;
  76. ndctl_fn ndctl;
  77. int (*flush_probe)(struct nvdimm_bus_descriptor *nd_desc);
  78. int (*clear_to_send)(struct nvdimm_bus_descriptor *nd_desc,
  79. struct nvdimm *nvdimm, unsigned int cmd, void *data);
  80. const struct nvdimm_bus_fw_ops *fw_ops;
  81. };
  82. struct nd_cmd_desc {
  83. int in_num;
  84. int out_num;
  85. u32 in_sizes[ND_CMD_MAX_ELEM];
  86. int out_sizes[ND_CMD_MAX_ELEM];
  87. };
  88. struct nd_interleave_set {
  89. /* v1.1 definition of the interleave-set-cookie algorithm */
  90. u64 cookie1;
  91. /* v1.2 definition of the interleave-set-cookie algorithm */
  92. u64 cookie2;
  93. /* compatibility with initial buggy Linux implementation */
  94. u64 altcookie;
  95. guid_t type_guid;
  96. };
  97. struct nd_mapping_desc {
  98. struct nvdimm *nvdimm;
  99. u64 start;
  100. u64 size;
  101. int position;
  102. };
  103. struct nd_region;
  104. struct nd_region_desc {
  105. struct resource *res;
  106. struct nd_mapping_desc *mapping;
  107. u16 num_mappings;
  108. const struct attribute_group **attr_groups;
  109. struct nd_interleave_set *nd_set;
  110. void *provider_data;
  111. int num_lanes;
  112. int numa_node;
  113. int target_node;
  114. unsigned long flags;
  115. struct device_node *of_node;
  116. int (*flush)(struct nd_region *nd_region, struct bio *bio);
  117. };
  118. struct device;
  119. void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset,
  120. size_t size, unsigned long flags);
  121. static inline void __iomem *devm_nvdimm_ioremap(struct device *dev,
  122. resource_size_t offset, size_t size)
  123. {
  124. return (void __iomem *) devm_nvdimm_memremap(dev, offset, size, 0);
  125. }
  126. struct nvdimm_bus;
  127. struct module;
  128. struct device;
  129. struct nd_blk_region;
  130. struct nd_blk_region_desc {
  131. int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
  132. int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
  133. void *iobuf, u64 len, int rw);
  134. struct nd_region_desc ndr_desc;
  135. };
  136. static inline struct nd_blk_region_desc *to_blk_region_desc(
  137. struct nd_region_desc *ndr_desc)
  138. {
  139. return container_of(ndr_desc, struct nd_blk_region_desc, ndr_desc);
  140. }
  141. /*
  142. * Note that separate bits for locked + unlocked are defined so that
  143. * 'flags == 0' corresponds to an error / not-supported state.
  144. */
  145. enum nvdimm_security_bits {
  146. NVDIMM_SECURITY_DISABLED,
  147. NVDIMM_SECURITY_UNLOCKED,
  148. NVDIMM_SECURITY_LOCKED,
  149. NVDIMM_SECURITY_FROZEN,
  150. NVDIMM_SECURITY_OVERWRITE,
  151. };
  152. #define NVDIMM_PASSPHRASE_LEN 32
  153. #define NVDIMM_KEY_DESC_LEN 22
  154. struct nvdimm_key_data {
  155. u8 data[NVDIMM_PASSPHRASE_LEN];
  156. };
  157. enum nvdimm_passphrase_type {
  158. NVDIMM_USER,
  159. NVDIMM_MASTER,
  160. };
  161. struct nvdimm_security_ops {
  162. unsigned long (*get_flags)(struct nvdimm *nvdimm,
  163. enum nvdimm_passphrase_type pass_type);
  164. int (*freeze)(struct nvdimm *nvdimm);
  165. int (*change_key)(struct nvdimm *nvdimm,
  166. const struct nvdimm_key_data *old_data,
  167. const struct nvdimm_key_data *new_data,
  168. enum nvdimm_passphrase_type pass_type);
  169. int (*unlock)(struct nvdimm *nvdimm,
  170. const struct nvdimm_key_data *key_data);
  171. int (*disable)(struct nvdimm *nvdimm,
  172. const struct nvdimm_key_data *key_data);
  173. int (*erase)(struct nvdimm *nvdimm,
  174. const struct nvdimm_key_data *key_data,
  175. enum nvdimm_passphrase_type pass_type);
  176. int (*overwrite)(struct nvdimm *nvdimm,
  177. const struct nvdimm_key_data *key_data);
  178. int (*query_overwrite)(struct nvdimm *nvdimm);
  179. };
  180. enum nvdimm_fwa_state {
  181. NVDIMM_FWA_INVALID,
  182. NVDIMM_FWA_IDLE,
  183. NVDIMM_FWA_ARMED,
  184. NVDIMM_FWA_BUSY,
  185. NVDIMM_FWA_ARM_OVERFLOW,
  186. };
  187. enum nvdimm_fwa_trigger {
  188. NVDIMM_FWA_ARM,
  189. NVDIMM_FWA_DISARM,
  190. };
  191. enum nvdimm_fwa_capability {
  192. NVDIMM_FWA_CAP_INVALID,
  193. NVDIMM_FWA_CAP_NONE,
  194. NVDIMM_FWA_CAP_QUIESCE,
  195. NVDIMM_FWA_CAP_LIVE,
  196. };
  197. enum nvdimm_fwa_result {
  198. NVDIMM_FWA_RESULT_INVALID,
  199. NVDIMM_FWA_RESULT_NONE,
  200. NVDIMM_FWA_RESULT_SUCCESS,
  201. NVDIMM_FWA_RESULT_NOTSTAGED,
  202. NVDIMM_FWA_RESULT_NEEDRESET,
  203. NVDIMM_FWA_RESULT_FAIL,
  204. };
  205. struct nvdimm_bus_fw_ops {
  206. enum nvdimm_fwa_state (*activate_state)
  207. (struct nvdimm_bus_descriptor *nd_desc);
  208. enum nvdimm_fwa_capability (*capability)
  209. (struct nvdimm_bus_descriptor *nd_desc);
  210. int (*activate)(struct nvdimm_bus_descriptor *nd_desc);
  211. };
  212. struct nvdimm_fw_ops {
  213. enum nvdimm_fwa_state (*activate_state)(struct nvdimm *nvdimm);
  214. enum nvdimm_fwa_result (*activate_result)(struct nvdimm *nvdimm);
  215. int (*arm)(struct nvdimm *nvdimm, enum nvdimm_fwa_trigger arg);
  216. };
  217. void badrange_init(struct badrange *badrange);
  218. int badrange_add(struct badrange *badrange, u64 addr, u64 length);
  219. void badrange_forget(struct badrange *badrange, phys_addr_t start,
  220. unsigned int len);
  221. int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr,
  222. u64 length);
  223. struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
  224. struct nvdimm_bus_descriptor *nfit_desc);
  225. void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus);
  226. struct nvdimm_bus *to_nvdimm_bus(struct device *dev);
  227. struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm);
  228. struct nvdimm *to_nvdimm(struct device *dev);
  229. struct nd_region *to_nd_region(struct device *dev);
  230. struct device *nd_region_dev(struct nd_region *nd_region);
  231. struct nd_blk_region *to_nd_blk_region(struct device *dev);
  232. struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus);
  233. struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus);
  234. const char *nvdimm_name(struct nvdimm *nvdimm);
  235. struct kobject *nvdimm_kobj(struct nvdimm *nvdimm);
  236. unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm);
  237. void *nvdimm_provider_data(struct nvdimm *nvdimm);
  238. struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
  239. void *provider_data, const struct attribute_group **groups,
  240. unsigned long flags, unsigned long cmd_mask, int num_flush,
  241. struct resource *flush_wpq, const char *dimm_id,
  242. const struct nvdimm_security_ops *sec_ops,
  243. const struct nvdimm_fw_ops *fw_ops);
  244. static inline struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus,
  245. void *provider_data, const struct attribute_group **groups,
  246. unsigned long flags, unsigned long cmd_mask, int num_flush,
  247. struct resource *flush_wpq)
  248. {
  249. return __nvdimm_create(nvdimm_bus, provider_data, groups, flags,
  250. cmd_mask, num_flush, flush_wpq, NULL, NULL, NULL);
  251. }
  252. const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd);
  253. const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd);
  254. u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
  255. const struct nd_cmd_desc *desc, int idx, void *buf);
  256. u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
  257. const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
  258. const u32 *out_field, unsigned long remainder);
  259. int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count);
  260. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  261. struct nd_region_desc *ndr_desc);
  262. struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
  263. struct nd_region_desc *ndr_desc);
  264. struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
  265. struct nd_region_desc *ndr_desc);
  266. void *nd_region_provider_data(struct nd_region *nd_region);
  267. void *nd_blk_region_provider_data(struct nd_blk_region *ndbr);
  268. void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data);
  269. struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr);
  270. unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr);
  271. unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
  272. void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
  273. u64 nd_fletcher64(void *addr, size_t len, bool le);
  274. int nvdimm_flush(struct nd_region *nd_region, struct bio *bio);
  275. int generic_nvdimm_flush(struct nd_region *nd_region);
  276. int nvdimm_has_flush(struct nd_region *nd_region);
  277. int nvdimm_has_cache(struct nd_region *nd_region);
  278. int nvdimm_in_overwrite(struct nvdimm *nvdimm);
  279. bool is_nvdimm_sync(struct nd_region *nd_region);
  280. static inline int nvdimm_ctl(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
  281. unsigned int buf_len, int *cmd_rc)
  282. {
  283. struct nvdimm_bus *nvdimm_bus = nvdimm_to_bus(nvdimm);
  284. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  285. return nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, cmd_rc);
  286. }
  287. #ifdef CONFIG_ARCH_HAS_PMEM_API
  288. #define ARCH_MEMREMAP_PMEM MEMREMAP_WB
  289. void arch_wb_cache_pmem(void *addr, size_t size);
  290. void arch_invalidate_pmem(void *addr, size_t size);
  291. #else
  292. #define ARCH_MEMREMAP_PMEM MEMREMAP_WT
  293. static inline void arch_wb_cache_pmem(void *addr, size_t size)
  294. {
  295. }
  296. static inline void arch_invalidate_pmem(void *addr, size_t size)
  297. {
  298. }
  299. #endif
  300. #endif /* __LIBNVDIMM_H__ */