libnvdimm.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /* unarmed memory devices may not persist writes */
  26. NDD_UNARMED = 1,
  27. /* locked memory devices should not be accessed */
  28. NDD_LOCKED = 2,
  29. /* memory under security wipes should not be accessed */
  30. NDD_SECURITY_OVERWRITE = 3,
  31. /* tracking whether or not there is a pending device reference */
  32. NDD_WORK_PENDING = 4,
  33. /* dimm supports namespace labels */
  34. NDD_LABELING = 6,
  35. /* dimm provider wants synchronous registration by __nvdimm_create() */
  36. NDD_REGISTER_SYNC = 8,
  37. /* need to set a limit somewhere, but yes, this is likely overkill */
  38. ND_IOCTL_MAX_BUFLEN = SZ_4M,
  39. ND_CMD_MAX_ELEM = 5,
  40. ND_CMD_MAX_ENVELOPE = 256,
  41. ND_MAX_MAPPINGS = 32,
  42. /* region flag indicating to direct-map persistent memory by default */
  43. ND_REGION_PAGEMAP = 0,
  44. /*
  45. * Platform ensures entire CPU store data path is flushed to pmem on
  46. * system power loss.
  47. */
  48. ND_REGION_PERSIST_CACHE = 1,
  49. /*
  50. * Platform provides mechanisms to automatically flush outstanding
  51. * write data from memory controler to pmem on system power loss.
  52. * (ADR)
  53. */
  54. ND_REGION_PERSIST_MEMCTRL = 2,
  55. /* Platform provides asynchronous flush mechanism */
  56. ND_REGION_ASYNC = 3,
  57. /* Region was created by CXL subsystem */
  58. ND_REGION_CXL = 4,
  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. int memregion;
  116. struct device_node *of_node;
  117. int (*flush)(struct nd_region *nd_region, struct bio *bio);
  118. };
  119. struct device;
  120. void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset,
  121. size_t size, unsigned long flags);
  122. static inline void __iomem *devm_nvdimm_ioremap(struct device *dev,
  123. resource_size_t offset, size_t size)
  124. {
  125. return (void __iomem *) devm_nvdimm_memremap(dev, offset, size, 0);
  126. }
  127. struct nvdimm_bus;
  128. /*
  129. * Note that separate bits for locked + unlocked are defined so that
  130. * 'flags == 0' corresponds to an error / not-supported state.
  131. */
  132. enum nvdimm_security_bits {
  133. NVDIMM_SECURITY_DISABLED,
  134. NVDIMM_SECURITY_UNLOCKED,
  135. NVDIMM_SECURITY_LOCKED,
  136. NVDIMM_SECURITY_FROZEN,
  137. NVDIMM_SECURITY_OVERWRITE,
  138. };
  139. #define NVDIMM_PASSPHRASE_LEN 32
  140. #define NVDIMM_KEY_DESC_LEN 22
  141. struct nvdimm_key_data {
  142. u8 data[NVDIMM_PASSPHRASE_LEN];
  143. };
  144. enum nvdimm_passphrase_type {
  145. NVDIMM_USER,
  146. NVDIMM_MASTER,
  147. };
  148. struct nvdimm_security_ops {
  149. unsigned long (*get_flags)(struct nvdimm *nvdimm,
  150. enum nvdimm_passphrase_type pass_type);
  151. int (*freeze)(struct nvdimm *nvdimm);
  152. int (*change_key)(struct nvdimm *nvdimm,
  153. const struct nvdimm_key_data *old_data,
  154. const struct nvdimm_key_data *new_data,
  155. enum nvdimm_passphrase_type pass_type);
  156. int (*unlock)(struct nvdimm *nvdimm,
  157. const struct nvdimm_key_data *key_data);
  158. int (*disable)(struct nvdimm *nvdimm,
  159. const struct nvdimm_key_data *key_data);
  160. int (*erase)(struct nvdimm *nvdimm,
  161. const struct nvdimm_key_data *key_data,
  162. enum nvdimm_passphrase_type pass_type);
  163. int (*overwrite)(struct nvdimm *nvdimm,
  164. const struct nvdimm_key_data *key_data);
  165. int (*query_overwrite)(struct nvdimm *nvdimm);
  166. };
  167. enum nvdimm_fwa_state {
  168. NVDIMM_FWA_INVALID,
  169. NVDIMM_FWA_IDLE,
  170. NVDIMM_FWA_ARMED,
  171. NVDIMM_FWA_BUSY,
  172. NVDIMM_FWA_ARM_OVERFLOW,
  173. };
  174. enum nvdimm_fwa_trigger {
  175. NVDIMM_FWA_ARM,
  176. NVDIMM_FWA_DISARM,
  177. };
  178. enum nvdimm_fwa_capability {
  179. NVDIMM_FWA_CAP_INVALID,
  180. NVDIMM_FWA_CAP_NONE,
  181. NVDIMM_FWA_CAP_QUIESCE,
  182. NVDIMM_FWA_CAP_LIVE,
  183. };
  184. enum nvdimm_fwa_result {
  185. NVDIMM_FWA_RESULT_INVALID,
  186. NVDIMM_FWA_RESULT_NONE,
  187. NVDIMM_FWA_RESULT_SUCCESS,
  188. NVDIMM_FWA_RESULT_NOTSTAGED,
  189. NVDIMM_FWA_RESULT_NEEDRESET,
  190. NVDIMM_FWA_RESULT_FAIL,
  191. };
  192. struct nvdimm_bus_fw_ops {
  193. enum nvdimm_fwa_state (*activate_state)
  194. (struct nvdimm_bus_descriptor *nd_desc);
  195. enum nvdimm_fwa_capability (*capability)
  196. (struct nvdimm_bus_descriptor *nd_desc);
  197. int (*activate)(struct nvdimm_bus_descriptor *nd_desc);
  198. };
  199. struct nvdimm_fw_ops {
  200. enum nvdimm_fwa_state (*activate_state)(struct nvdimm *nvdimm);
  201. enum nvdimm_fwa_result (*activate_result)(struct nvdimm *nvdimm);
  202. int (*arm)(struct nvdimm *nvdimm, enum nvdimm_fwa_trigger arg);
  203. };
  204. void badrange_init(struct badrange *badrange);
  205. int badrange_add(struct badrange *badrange, u64 addr, u64 length);
  206. void badrange_forget(struct badrange *badrange, phys_addr_t start,
  207. unsigned int len);
  208. int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr,
  209. u64 length);
  210. struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
  211. struct nvdimm_bus_descriptor *nfit_desc);
  212. void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus);
  213. struct nvdimm_bus *to_nvdimm_bus(struct device *dev);
  214. struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm);
  215. struct nvdimm *to_nvdimm(struct device *dev);
  216. struct nd_region *to_nd_region(struct device *dev);
  217. struct device *nd_region_dev(struct nd_region *nd_region);
  218. struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus);
  219. struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus);
  220. const char *nvdimm_name(struct nvdimm *nvdimm);
  221. struct kobject *nvdimm_kobj(struct nvdimm *nvdimm);
  222. unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm);
  223. void *nvdimm_provider_data(struct nvdimm *nvdimm);
  224. struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
  225. void *provider_data, const struct attribute_group **groups,
  226. unsigned long flags, unsigned long cmd_mask, int num_flush,
  227. struct resource *flush_wpq, const char *dimm_id,
  228. const struct nvdimm_security_ops *sec_ops,
  229. const struct nvdimm_fw_ops *fw_ops);
  230. static inline struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus,
  231. void *provider_data, const struct attribute_group **groups,
  232. unsigned long flags, unsigned long cmd_mask, int num_flush,
  233. struct resource *flush_wpq)
  234. {
  235. return __nvdimm_create(nvdimm_bus, provider_data, groups, flags,
  236. cmd_mask, num_flush, flush_wpq, NULL, NULL, NULL);
  237. }
  238. void nvdimm_delete(struct nvdimm *nvdimm);
  239. void nvdimm_region_delete(struct nd_region *nd_region);
  240. const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd);
  241. const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd);
  242. u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
  243. const struct nd_cmd_desc *desc, int idx, void *buf);
  244. u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
  245. const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
  246. const u32 *out_field, unsigned long remainder);
  247. int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count);
  248. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  249. struct nd_region_desc *ndr_desc);
  250. struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
  251. struct nd_region_desc *ndr_desc);
  252. struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
  253. struct nd_region_desc *ndr_desc);
  254. void *nd_region_provider_data(struct nd_region *nd_region);
  255. unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
  256. void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
  257. u64 nd_fletcher64(void *addr, size_t len, bool le);
  258. int nvdimm_flush(struct nd_region *nd_region, struct bio *bio);
  259. int generic_nvdimm_flush(struct nd_region *nd_region);
  260. int nvdimm_has_flush(struct nd_region *nd_region);
  261. int nvdimm_has_cache(struct nd_region *nd_region);
  262. int nvdimm_in_overwrite(struct nvdimm *nvdimm);
  263. bool is_nvdimm_sync(struct nd_region *nd_region);
  264. static inline int nvdimm_ctl(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
  265. unsigned int buf_len, int *cmd_rc)
  266. {
  267. struct nvdimm_bus *nvdimm_bus = nvdimm_to_bus(nvdimm);
  268. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  269. return nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, cmd_rc);
  270. }
  271. #ifdef CONFIG_ARCH_HAS_PMEM_API
  272. #define ARCH_MEMREMAP_PMEM MEMREMAP_WB
  273. void arch_wb_cache_pmem(void *addr, size_t size);
  274. void arch_invalidate_pmem(void *addr, size_t size);
  275. #else
  276. #define ARCH_MEMREMAP_PMEM MEMREMAP_WT
  277. static inline void arch_wb_cache_pmem(void *addr, size_t size)
  278. {
  279. }
  280. static inline void arch_invalidate_pmem(void *addr, size_t size)
  281. {
  282. }
  283. #endif
  284. #endif /* __LIBNVDIMM_H__ */