nd.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 __ND_H__
  14. #define __ND_H__
  15. #include <linux/libnvdimm.h>
  16. #include <linux/badblocks.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/device.h>
  19. #include <linux/mutex.h>
  20. #include <linux/ndctl.h>
  21. #include <linux/types.h>
  22. #include <linux/nd.h>
  23. #include "label.h"
  24. enum {
  25. /*
  26. * Limits the maximum number of block apertures a dimm can
  27. * support and is an input to the geometry/on-disk-format of a
  28. * BTT instance
  29. */
  30. ND_MAX_LANES = 256,
  31. SECTOR_SHIFT = 9,
  32. INT_LBASIZE_ALIGNMENT = 64,
  33. };
  34. struct nd_poison {
  35. u64 start;
  36. u64 length;
  37. struct list_head list;
  38. };
  39. struct nvdimm_drvdata {
  40. struct device *dev;
  41. int nsindex_size;
  42. struct nd_cmd_get_config_size nsarea;
  43. void *data;
  44. int ns_current, ns_next;
  45. struct resource dpa;
  46. struct kref kref;
  47. };
  48. struct nd_region_data {
  49. int ns_count;
  50. int ns_active;
  51. unsigned int hints_shift;
  52. void __iomem *flush_wpq[0];
  53. };
  54. static inline void __iomem *ndrd_get_flush_wpq(struct nd_region_data *ndrd,
  55. int dimm, int hint)
  56. {
  57. unsigned int num = 1 << ndrd->hints_shift;
  58. unsigned int mask = num - 1;
  59. return ndrd->flush_wpq[dimm * num + (hint & mask)];
  60. }
  61. static inline void ndrd_set_flush_wpq(struct nd_region_data *ndrd, int dimm,
  62. int hint, void __iomem *flush)
  63. {
  64. unsigned int num = 1 << ndrd->hints_shift;
  65. unsigned int mask = num - 1;
  66. ndrd->flush_wpq[dimm * num + (hint & mask)] = flush;
  67. }
  68. static inline struct nd_namespace_index *to_namespace_index(
  69. struct nvdimm_drvdata *ndd, int i)
  70. {
  71. if (i < 0)
  72. return NULL;
  73. return ndd->data + sizeof_namespace_index(ndd) * i;
  74. }
  75. static inline struct nd_namespace_index *to_current_namespace_index(
  76. struct nvdimm_drvdata *ndd)
  77. {
  78. return to_namespace_index(ndd, ndd->ns_current);
  79. }
  80. static inline struct nd_namespace_index *to_next_namespace_index(
  81. struct nvdimm_drvdata *ndd)
  82. {
  83. return to_namespace_index(ndd, ndd->ns_next);
  84. }
  85. #define nd_dbg_dpa(r, d, res, fmt, arg...) \
  86. dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \
  87. (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \
  88. (unsigned long long) (res ? resource_size(res) : 0), \
  89. (unsigned long long) (res ? res->start : 0), ##arg)
  90. #define for_each_dpa_resource(ndd, res) \
  91. for (res = (ndd)->dpa.child; res; res = res->sibling)
  92. #define for_each_dpa_resource_safe(ndd, res, next) \
  93. for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \
  94. res; res = next, next = next ? next->sibling : NULL)
  95. struct nd_percpu_lane {
  96. int count;
  97. spinlock_t lock;
  98. };
  99. struct nd_label_ent {
  100. struct list_head list;
  101. struct nd_namespace_label *label;
  102. };
  103. enum nd_mapping_lock_class {
  104. ND_MAPPING_CLASS0,
  105. ND_MAPPING_UUID_SCAN,
  106. };
  107. struct nd_mapping {
  108. struct nvdimm *nvdimm;
  109. u64 start;
  110. u64 size;
  111. struct list_head labels;
  112. struct mutex lock;
  113. /*
  114. * @ndd is for private use at region enable / disable time for
  115. * get_ndd() + put_ndd(), all other nd_mapping to ndd
  116. * conversions use to_ndd() which respects enabled state of the
  117. * nvdimm.
  118. */
  119. struct nvdimm_drvdata *ndd;
  120. };
  121. struct nd_region {
  122. struct device dev;
  123. struct ida ns_ida;
  124. struct ida btt_ida;
  125. struct ida pfn_ida;
  126. struct ida dax_ida;
  127. unsigned long flags;
  128. struct device *ns_seed;
  129. struct device *btt_seed;
  130. struct device *pfn_seed;
  131. struct device *dax_seed;
  132. u16 ndr_mappings;
  133. u64 ndr_size;
  134. u64 ndr_start;
  135. int id, num_lanes, ro, numa_node;
  136. void *provider_data;
  137. struct nd_interleave_set *nd_set;
  138. struct nd_percpu_lane __percpu *lane;
  139. struct nd_mapping mapping[0];
  140. };
  141. struct nd_blk_region {
  142. int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
  143. int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
  144. void *iobuf, u64 len, int rw);
  145. void *blk_provider_data;
  146. struct nd_region nd_region;
  147. };
  148. /*
  149. * Lookup next in the repeating sequence of 01, 10, and 11.
  150. */
  151. static inline unsigned nd_inc_seq(unsigned seq)
  152. {
  153. static const unsigned next[] = { 0, 2, 3, 1 };
  154. return next[seq & 3];
  155. }
  156. struct btt;
  157. struct nd_btt {
  158. struct device dev;
  159. struct nd_namespace_common *ndns;
  160. struct btt *btt;
  161. unsigned long lbasize;
  162. u64 size;
  163. u8 *uuid;
  164. int id;
  165. };
  166. enum nd_pfn_mode {
  167. PFN_MODE_NONE,
  168. PFN_MODE_RAM,
  169. PFN_MODE_PMEM,
  170. };
  171. struct nd_pfn {
  172. int id;
  173. u8 *uuid;
  174. struct device dev;
  175. unsigned long align;
  176. unsigned long npfns;
  177. enum nd_pfn_mode mode;
  178. struct nd_pfn_sb *pfn_sb;
  179. struct nd_namespace_common *ndns;
  180. };
  181. struct nd_dax {
  182. struct nd_pfn nd_pfn;
  183. };
  184. enum nd_async_mode {
  185. ND_SYNC,
  186. ND_ASYNC,
  187. };
  188. int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
  189. void wait_nvdimm_bus_probe_idle(struct device *dev);
  190. void nd_device_register(struct device *dev);
  191. void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
  192. void nd_device_notify(struct device *dev, enum nvdimm_event event);
  193. int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
  194. size_t len);
  195. ssize_t nd_sector_size_show(unsigned long current_lbasize,
  196. const unsigned long *supported, char *buf);
  197. ssize_t nd_sector_size_store(struct device *dev, const char *buf,
  198. unsigned long *current_lbasize, const unsigned long *supported);
  199. int __init nvdimm_init(void);
  200. int __init nd_region_init(void);
  201. void nvdimm_exit(void);
  202. void nd_region_exit(void);
  203. struct nvdimm;
  204. struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
  205. int nvdimm_check_config_data(struct device *dev);
  206. int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
  207. int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
  208. int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
  209. void *buf, size_t len);
  210. long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
  211. unsigned int len);
  212. struct nd_btt *to_nd_btt(struct device *dev);
  213. struct nd_gen_sb {
  214. char reserved[SZ_4K - 8];
  215. __le64 checksum;
  216. };
  217. u64 nd_sb_checksum(struct nd_gen_sb *sb);
  218. #if IS_ENABLED(CONFIG_BTT)
  219. int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns);
  220. bool is_nd_btt(struct device *dev);
  221. struct device *nd_btt_create(struct nd_region *nd_region);
  222. #else
  223. static inline int nd_btt_probe(struct device *dev,
  224. struct nd_namespace_common *ndns)
  225. {
  226. return -ENODEV;
  227. }
  228. static inline bool is_nd_btt(struct device *dev)
  229. {
  230. return false;
  231. }
  232. static inline struct device *nd_btt_create(struct nd_region *nd_region)
  233. {
  234. return NULL;
  235. }
  236. #endif
  237. struct nd_pfn *to_nd_pfn(struct device *dev);
  238. #if IS_ENABLED(CONFIG_NVDIMM_PFN)
  239. int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns);
  240. bool is_nd_pfn(struct device *dev);
  241. struct device *nd_pfn_create(struct nd_region *nd_region);
  242. struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
  243. struct nd_namespace_common *ndns);
  244. int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig);
  245. extern struct attribute_group nd_pfn_attribute_group;
  246. #else
  247. static inline int nd_pfn_probe(struct device *dev,
  248. struct nd_namespace_common *ndns)
  249. {
  250. return -ENODEV;
  251. }
  252. static inline bool is_nd_pfn(struct device *dev)
  253. {
  254. return false;
  255. }
  256. static inline struct device *nd_pfn_create(struct nd_region *nd_region)
  257. {
  258. return NULL;
  259. }
  260. static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
  261. {
  262. return -ENODEV;
  263. }
  264. #endif
  265. struct nd_dax *to_nd_dax(struct device *dev);
  266. #if IS_ENABLED(CONFIG_NVDIMM_DAX)
  267. int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
  268. bool is_nd_dax(struct device *dev);
  269. struct device *nd_dax_create(struct nd_region *nd_region);
  270. #else
  271. static inline int nd_dax_probe(struct device *dev,
  272. struct nd_namespace_common *ndns)
  273. {
  274. return -ENODEV;
  275. }
  276. static inline bool is_nd_dax(struct device *dev)
  277. {
  278. return false;
  279. }
  280. static inline struct device *nd_dax_create(struct nd_region *nd_region)
  281. {
  282. return NULL;
  283. }
  284. #endif
  285. struct nd_region *to_nd_region(struct device *dev);
  286. int nd_region_to_nstype(struct nd_region *nd_region);
  287. int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
  288. u64 nd_region_interleave_set_cookie(struct nd_region *nd_region);
  289. u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
  290. void nvdimm_bus_lock(struct device *dev);
  291. void nvdimm_bus_unlock(struct device *dev);
  292. bool is_nvdimm_bus_locked(struct device *dev);
  293. int nvdimm_revalidate_disk(struct gendisk *disk);
  294. void nvdimm_drvdata_release(struct kref *kref);
  295. void put_ndd(struct nvdimm_drvdata *ndd);
  296. int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
  297. void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
  298. struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
  299. struct nd_label_id *label_id, resource_size_t start,
  300. resource_size_t n);
  301. resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
  302. struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
  303. int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
  304. int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt);
  305. const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
  306. char *name);
  307. void nvdimm_badblocks_populate(struct nd_region *nd_region,
  308. struct badblocks *bb, const struct resource *res);
  309. #if IS_ENABLED(CONFIG_ND_CLAIM)
  310. struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
  311. struct resource *res, struct vmem_altmap *altmap);
  312. int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio);
  313. void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio);
  314. #else
  315. static inline struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
  316. struct resource *res, struct vmem_altmap *altmap)
  317. {
  318. return ERR_PTR(-ENXIO);
  319. }
  320. static inline int devm_nsio_enable(struct device *dev,
  321. struct nd_namespace_io *nsio)
  322. {
  323. return -ENXIO;
  324. }
  325. static inline void devm_nsio_disable(struct device *dev,
  326. struct nd_namespace_io *nsio)
  327. {
  328. }
  329. #endif
  330. int nd_blk_region_init(struct nd_region *nd_region);
  331. int nd_region_activate(struct nd_region *nd_region);
  332. void __nd_iostat_start(struct bio *bio, unsigned long *start);
  333. static inline bool nd_iostat_start(struct bio *bio, unsigned long *start)
  334. {
  335. struct gendisk *disk = bio->bi_bdev->bd_disk;
  336. if (!blk_queue_io_stat(disk->queue))
  337. return false;
  338. __nd_iostat_start(bio, start);
  339. return true;
  340. }
  341. void nd_iostat_end(struct bio *bio, unsigned long start);
  342. static inline bool is_bad_pmem(struct badblocks *bb, sector_t sector,
  343. unsigned int len)
  344. {
  345. if (bb->count) {
  346. sector_t first_bad;
  347. int num_bad;
  348. return !!badblocks_check(bb, sector, len / 512, &first_bad,
  349. &num_bad);
  350. }
  351. return false;
  352. }
  353. resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk);
  354. const u8 *nd_dev_to_uuid(struct device *dev);
  355. bool pmem_should_map_pages(struct device *dev);
  356. #endif /* __ND_H__ */