nd.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 __LINUX_ND_H__
  14. #define __LINUX_ND_H__
  15. #include <linux/fs.h>
  16. #include <linux/ndctl.h>
  17. #include <linux/device.h>
  18. #include <linux/badblocks.h>
  19. enum nvdimm_event {
  20. NVDIMM_REVALIDATE_POISON,
  21. };
  22. enum nvdimm_claim_class {
  23. NVDIMM_CCLASS_NONE,
  24. NVDIMM_CCLASS_BTT,
  25. NVDIMM_CCLASS_BTT2,
  26. NVDIMM_CCLASS_PFN,
  27. NVDIMM_CCLASS_DAX,
  28. NVDIMM_CCLASS_UNKNOWN,
  29. };
  30. struct nd_device_driver {
  31. struct device_driver drv;
  32. unsigned long type;
  33. int (*probe)(struct device *dev);
  34. int (*remove)(struct device *dev);
  35. void (*shutdown)(struct device *dev);
  36. void (*notify)(struct device *dev, enum nvdimm_event event);
  37. };
  38. static inline struct nd_device_driver *to_nd_device_driver(
  39. struct device_driver *drv)
  40. {
  41. return container_of(drv, struct nd_device_driver, drv);
  42. };
  43. /**
  44. * struct nd_namespace_common - core infrastructure of a namespace
  45. * @force_raw: ignore other personalities for the namespace (e.g. btt)
  46. * @dev: device model node
  47. * @claim: when set a another personality has taken ownership of the namespace
  48. * @claim_class: restrict claim type to a given class
  49. * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
  50. */
  51. struct nd_namespace_common {
  52. int force_raw;
  53. struct device dev;
  54. struct device *claim;
  55. enum nvdimm_claim_class claim_class;
  56. int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
  57. void *buf, size_t size, int rw, unsigned long flags);
  58. };
  59. static inline struct nd_namespace_common *to_ndns(struct device *dev)
  60. {
  61. return container_of(dev, struct nd_namespace_common, dev);
  62. }
  63. /**
  64. * struct nd_namespace_io - device representation of a persistent memory range
  65. * @dev: namespace device created by the nd region driver
  66. * @res: struct resource conversion of a NFIT SPA table
  67. * @size: cached resource_size(@res) for fast path size checks
  68. * @addr: virtual address to access the namespace range
  69. * @bb: badblocks list for the namespace range
  70. */
  71. struct nd_namespace_io {
  72. struct nd_namespace_common common;
  73. struct resource res;
  74. resource_size_t size;
  75. void *addr;
  76. struct badblocks bb;
  77. };
  78. /**
  79. * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
  80. * @nsio: device and system physical address range to drive
  81. * @lbasize: logical sector size for the namespace in block-device-mode
  82. * @alt_name: namespace name supplied in the dimm label
  83. * @uuid: namespace name supplied in the dimm label
  84. * @id: ida allocated id
  85. */
  86. struct nd_namespace_pmem {
  87. struct nd_namespace_io nsio;
  88. unsigned long lbasize;
  89. char *alt_name;
  90. u8 *uuid;
  91. int id;
  92. };
  93. /**
  94. * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
  95. * @alt_name: namespace name supplied in the dimm label
  96. * @uuid: namespace name supplied in the dimm label
  97. * @id: ida allocated id
  98. * @lbasize: blk namespaces have a native sector size when btt not present
  99. * @size: sum of all the resource ranges allocated to this namespace
  100. * @num_resources: number of dpa extents to claim
  101. * @res: discontiguous dpa extents for given dimm
  102. */
  103. struct nd_namespace_blk {
  104. struct nd_namespace_common common;
  105. char *alt_name;
  106. u8 *uuid;
  107. int id;
  108. unsigned long lbasize;
  109. resource_size_t size;
  110. int num_resources;
  111. struct resource **res;
  112. };
  113. static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
  114. {
  115. return container_of(dev, struct nd_namespace_io, common.dev);
  116. }
  117. static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
  118. {
  119. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  120. return container_of(nsio, struct nd_namespace_pmem, nsio);
  121. }
  122. static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
  123. {
  124. return container_of(dev, struct nd_namespace_blk, common.dev);
  125. }
  126. /**
  127. * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
  128. * @ndns: device to read
  129. * @offset: namespace-relative starting offset
  130. * @buf: buffer to fill
  131. * @size: transfer length
  132. *
  133. * @buf is up-to-date upon return from this routine.
  134. */
  135. static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
  136. resource_size_t offset, void *buf, size_t size,
  137. unsigned long flags)
  138. {
  139. return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
  140. }
  141. /**
  142. * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
  143. * @ndns: device to read
  144. * @offset: namespace-relative starting offset
  145. * @buf: buffer to drain
  146. * @size: transfer length
  147. *
  148. * NVDIMM Namepaces disks do not implement sectors internally. Depending on
  149. * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
  150. * or on backing memory media upon return from this routine. Flushing
  151. * to media is handled internal to the @ndns driver, if at all.
  152. */
  153. static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
  154. resource_size_t offset, void *buf, size_t size,
  155. unsigned long flags)
  156. {
  157. return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
  158. }
  159. #define MODULE_ALIAS_ND_DEVICE(type) \
  160. MODULE_ALIAS("nd:t" __stringify(type) "*")
  161. #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
  162. struct nd_region;
  163. void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
  164. int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
  165. struct module *module, const char *mod_name);
  166. static inline void nd_driver_unregister(struct nd_device_driver *drv)
  167. {
  168. driver_unregister(&drv->drv);
  169. }
  170. #define nd_driver_register(driver) \
  171. __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
  172. #define module_nd_driver(driver) \
  173. module_driver(driver, nd_driver_register, nd_driver_unregister)
  174. #endif /* __LINUX_ND_H__ */