nd.h 4.7 KB

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