nd.h 5.3 KB

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