nd.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #ifndef __LINUX_ND_H__
  6. #define __LINUX_ND_H__
  7. #include <linux/fs.h>
  8. #include <linux/ndctl.h>
  9. #include <linux/device.h>
  10. #include <linux/badblocks.h>
  11. #include <linux/perf_event.h>
  12. enum nvdimm_event {
  13. NVDIMM_REVALIDATE_POISON,
  14. NVDIMM_REVALIDATE_REGION,
  15. };
  16. enum nvdimm_claim_class {
  17. NVDIMM_CCLASS_NONE,
  18. NVDIMM_CCLASS_BTT,
  19. NVDIMM_CCLASS_BTT2,
  20. NVDIMM_CCLASS_PFN,
  21. NVDIMM_CCLASS_DAX,
  22. NVDIMM_CCLASS_UNKNOWN,
  23. };
  24. #define NVDIMM_EVENT_VAR(_id) event_attr_##_id
  25. #define NVDIMM_EVENT_PTR(_id) (&event_attr_##_id.attr.attr)
  26. #define NVDIMM_EVENT_ATTR(_name, _id) \
  27. PMU_EVENT_ATTR(_name, NVDIMM_EVENT_VAR(_id), _id, \
  28. nvdimm_events_sysfs_show)
  29. /* Event attribute array index */
  30. #define NVDIMM_PMU_FORMAT_ATTR 0
  31. #define NVDIMM_PMU_EVENT_ATTR 1
  32. #define NVDIMM_PMU_CPUMASK_ATTR 2
  33. #define NVDIMM_PMU_NULL_ATTR 3
  34. /**
  35. * struct nvdimm_pmu - data structure for nvdimm perf driver
  36. * @pmu: pmu data structure for nvdimm performance stats.
  37. * @dev: nvdimm device pointer.
  38. * @cpu: designated cpu for counter access.
  39. * @node: node for cpu hotplug notifier link.
  40. * @cpuhp_state: state for cpu hotplug notification.
  41. * @arch_cpumask: cpumask to get designated cpu for counter access.
  42. */
  43. struct nvdimm_pmu {
  44. struct pmu pmu;
  45. struct device *dev;
  46. int cpu;
  47. struct hlist_node node;
  48. enum cpuhp_state cpuhp_state;
  49. /* cpumask provided by arch/platform specific code */
  50. struct cpumask arch_cpumask;
  51. };
  52. struct platform_device;
  53. #ifdef CONFIG_PERF_EVENTS
  54. extern ssize_t nvdimm_events_sysfs_show(struct device *dev,
  55. struct device_attribute *attr,
  56. char *page);
  57. int register_nvdimm_pmu(struct nvdimm_pmu *nvdimm, struct platform_device *pdev);
  58. void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu);
  59. #else
  60. static inline int register_nvdimm_pmu(struct nvdimm_pmu *nvdimm, struct platform_device *pdev)
  61. {
  62. return -ENXIO;
  63. }
  64. static inline void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu) { }
  65. #endif
  66. struct nd_device_driver {
  67. struct device_driver drv;
  68. unsigned long type;
  69. int (*probe)(struct device *dev);
  70. void (*remove)(struct device *dev);
  71. void (*shutdown)(struct device *dev);
  72. void (*notify)(struct device *dev, enum nvdimm_event event);
  73. };
  74. static inline struct nd_device_driver *to_nd_device_driver(
  75. struct device_driver *drv)
  76. {
  77. return container_of(drv, struct nd_device_driver, drv);
  78. };
  79. /**
  80. * struct nd_namespace_common - core infrastructure of a namespace
  81. * @force_raw: ignore other personalities for the namespace (e.g. btt)
  82. * @dev: device model node
  83. * @claim: when set a another personality has taken ownership of the namespace
  84. * @claim_class: restrict claim type to a given class
  85. * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
  86. */
  87. struct nd_namespace_common {
  88. int force_raw;
  89. struct device dev;
  90. struct device *claim;
  91. enum nvdimm_claim_class claim_class;
  92. int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
  93. void *buf, size_t size, int rw, unsigned long flags);
  94. };
  95. static inline struct nd_namespace_common *to_ndns(struct device *dev)
  96. {
  97. return container_of(dev, struct nd_namespace_common, dev);
  98. }
  99. /**
  100. * struct nd_namespace_io - device representation of a persistent memory range
  101. * @dev: namespace device created by the nd region driver
  102. * @res: struct resource conversion of a NFIT SPA table
  103. * @size: cached resource_size(@res) for fast path size checks
  104. * @addr: virtual address to access the namespace range
  105. * @bb: badblocks list for the namespace range
  106. */
  107. struct nd_namespace_io {
  108. struct nd_namespace_common common;
  109. struct resource res;
  110. resource_size_t size;
  111. void *addr;
  112. struct badblocks bb;
  113. };
  114. /**
  115. * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
  116. * @nsio: device and system physical address range to drive
  117. * @lbasize: logical sector size for the namespace in block-device-mode
  118. * @alt_name: namespace name supplied in the dimm label
  119. * @uuid: namespace name supplied in the dimm label
  120. * @id: ida allocated id
  121. */
  122. struct nd_namespace_pmem {
  123. struct nd_namespace_io nsio;
  124. unsigned long lbasize;
  125. char *alt_name;
  126. uuid_t *uuid;
  127. int id;
  128. };
  129. static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
  130. {
  131. return container_of(dev, struct nd_namespace_io, common.dev);
  132. }
  133. static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
  134. {
  135. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  136. return container_of(nsio, struct nd_namespace_pmem, nsio);
  137. }
  138. /**
  139. * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
  140. * @ndns: device to read
  141. * @offset: namespace-relative starting offset
  142. * @buf: buffer to fill
  143. * @size: transfer length
  144. *
  145. * @buf is up-to-date upon return from this routine.
  146. */
  147. static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
  148. resource_size_t offset, void *buf, size_t size,
  149. unsigned long flags)
  150. {
  151. return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
  152. }
  153. /**
  154. * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
  155. * @ndns: device to write
  156. * @offset: namespace-relative starting offset
  157. * @buf: buffer to drain
  158. * @size: transfer length
  159. *
  160. * NVDIMM Namepaces disks do not implement sectors internally. Depending on
  161. * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
  162. * or on backing memory media upon return from this routine. Flushing
  163. * to media is handled internal to the @ndns driver, if at all.
  164. */
  165. static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
  166. resource_size_t offset, void *buf, size_t size,
  167. unsigned long flags)
  168. {
  169. return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
  170. }
  171. #define MODULE_ALIAS_ND_DEVICE(type) \
  172. MODULE_ALIAS("nd:t" __stringify(type) "*")
  173. #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
  174. struct nd_region;
  175. void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
  176. int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
  177. struct module *module, const char *mod_name);
  178. static inline void nd_driver_unregister(struct nd_device_driver *drv)
  179. {
  180. driver_unregister(&drv->drv);
  181. }
  182. #define nd_driver_register(driver) \
  183. __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  184. #define module_nd_driver(driver) \
  185. module_driver(driver, nd_driver_register, nd_driver_unregister)
  186. #endif /* __LINUX_ND_H__ */