claim.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/sizes.h>
  7. #include <linux/badblocks.h>
  8. #include "nd-core.h"
  9. #include "pmem.h"
  10. #include "pfn.h"
  11. #include "btt.h"
  12. #include "nd.h"
  13. void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns)
  14. {
  15. struct nd_namespace_common *ndns = *_ndns;
  16. struct nvdimm_bus *nvdimm_bus;
  17. if (!ndns)
  18. return;
  19. nvdimm_bus = walk_to_nvdimm_bus(&ndns->dev);
  20. lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
  21. dev_WARN_ONCE(dev, ndns->claim != dev, "%s: invalid claim\n", __func__);
  22. ndns->claim = NULL;
  23. *_ndns = NULL;
  24. put_device(&ndns->dev);
  25. }
  26. void nd_detach_ndns(struct device *dev,
  27. struct nd_namespace_common **_ndns)
  28. {
  29. struct nd_namespace_common *ndns = *_ndns;
  30. if (!ndns)
  31. return;
  32. struct device *ndev __free(put_device) = get_device(&ndns->dev);
  33. guard(nvdimm_bus)(ndev);
  34. __nd_detach_ndns(dev, _ndns);
  35. }
  36. bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
  37. struct nd_namespace_common **_ndns)
  38. {
  39. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&attach->dev);
  40. if (attach->claim)
  41. return false;
  42. lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
  43. dev_WARN_ONCE(dev, *_ndns, "%s: invalid claim\n", __func__);
  44. attach->claim = dev;
  45. *_ndns = attach;
  46. get_device(&attach->dev);
  47. return true;
  48. }
  49. static bool is_idle(struct device *dev, struct nd_namespace_common *ndns)
  50. {
  51. struct nd_region *nd_region = to_nd_region(dev->parent);
  52. struct device *seed = NULL;
  53. if (is_nd_btt(dev))
  54. seed = nd_region->btt_seed;
  55. else if (is_nd_pfn(dev))
  56. seed = nd_region->pfn_seed;
  57. else if (is_nd_dax(dev))
  58. seed = nd_region->dax_seed;
  59. if (seed == dev || ndns || dev->driver)
  60. return false;
  61. return true;
  62. }
  63. struct nd_pfn *to_nd_pfn_safe(struct device *dev)
  64. {
  65. /*
  66. * pfn device attributes are re-used by dax device instances, so we
  67. * need to be careful to correct device-to-nd_pfn conversion.
  68. */
  69. if (is_nd_pfn(dev))
  70. return to_nd_pfn(dev);
  71. if (is_nd_dax(dev)) {
  72. struct nd_dax *nd_dax = to_nd_dax(dev);
  73. return &nd_dax->nd_pfn;
  74. }
  75. WARN_ON(1);
  76. return NULL;
  77. }
  78. static void nd_detach_and_reset(struct device *dev,
  79. struct nd_namespace_common **_ndns)
  80. {
  81. /* detach the namespace and destroy / reset the device */
  82. __nd_detach_ndns(dev, _ndns);
  83. if (is_idle(dev, *_ndns)) {
  84. nd_device_unregister(dev, ND_ASYNC);
  85. } else if (is_nd_btt(dev)) {
  86. struct nd_btt *nd_btt = to_nd_btt(dev);
  87. nd_btt->lbasize = 0;
  88. kfree(nd_btt->uuid);
  89. nd_btt->uuid = NULL;
  90. } else if (is_nd_pfn(dev) || is_nd_dax(dev)) {
  91. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  92. kfree(nd_pfn->uuid);
  93. nd_pfn->uuid = NULL;
  94. nd_pfn->mode = PFN_MODE_NONE;
  95. }
  96. }
  97. ssize_t nd_namespace_store(struct device *dev,
  98. struct nd_namespace_common **_ndns, const char *buf,
  99. size_t len)
  100. {
  101. struct nd_namespace_common *ndns;
  102. struct device *found;
  103. char *name;
  104. if (dev->driver) {
  105. dev_dbg(dev, "namespace already active\n");
  106. return -EBUSY;
  107. }
  108. name = kstrndup(buf, len, GFP_KERNEL);
  109. if (!name)
  110. return -ENOMEM;
  111. strim(name);
  112. if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
  113. /* pass */;
  114. else {
  115. len = -EINVAL;
  116. goto out;
  117. }
  118. ndns = *_ndns;
  119. if (strcmp(name, "") == 0) {
  120. nd_detach_and_reset(dev, _ndns);
  121. goto out;
  122. } else if (ndns) {
  123. dev_dbg(dev, "namespace already set to: %s\n",
  124. dev_name(&ndns->dev));
  125. len = -EBUSY;
  126. goto out;
  127. }
  128. found = device_find_child_by_name(dev->parent, name);
  129. if (!found) {
  130. dev_dbg(dev, "'%s' not found under %s\n", name,
  131. dev_name(dev->parent));
  132. len = -ENODEV;
  133. goto out;
  134. }
  135. ndns = to_ndns(found);
  136. switch (ndns->claim_class) {
  137. case NVDIMM_CCLASS_NONE:
  138. break;
  139. case NVDIMM_CCLASS_BTT:
  140. case NVDIMM_CCLASS_BTT2:
  141. if (!is_nd_btt(dev)) {
  142. len = -EBUSY;
  143. goto out_attach;
  144. }
  145. break;
  146. case NVDIMM_CCLASS_PFN:
  147. if (!is_nd_pfn(dev)) {
  148. len = -EBUSY;
  149. goto out_attach;
  150. }
  151. break;
  152. case NVDIMM_CCLASS_DAX:
  153. if (!is_nd_dax(dev)) {
  154. len = -EBUSY;
  155. goto out_attach;
  156. }
  157. break;
  158. default:
  159. len = -EBUSY;
  160. goto out_attach;
  161. break;
  162. }
  163. if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
  164. dev_dbg(dev, "%s too small to host\n", name);
  165. len = -ENXIO;
  166. goto out_attach;
  167. }
  168. WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
  169. if (!__nd_attach_ndns(dev, ndns, _ndns)) {
  170. dev_dbg(dev, "%s already claimed\n",
  171. dev_name(&ndns->dev));
  172. len = -EBUSY;
  173. }
  174. out_attach:
  175. put_device(&ndns->dev); /* from device_find_child */
  176. out:
  177. kfree(name);
  178. return len;
  179. }
  180. /*
  181. * nd_sb_checksum: compute checksum for a generic info block
  182. *
  183. * Returns a fletcher64 checksum of everything in the given info block
  184. * except the last field (since that's where the checksum lives).
  185. */
  186. u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
  187. {
  188. u64 sum;
  189. __le64 sum_save;
  190. BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
  191. BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
  192. BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
  193. sum_save = nd_gen_sb->checksum;
  194. nd_gen_sb->checksum = 0;
  195. sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
  196. nd_gen_sb->checksum = sum_save;
  197. return sum;
  198. }
  199. EXPORT_SYMBOL(nd_sb_checksum);
  200. static int nsio_rw_bytes(struct nd_namespace_common *ndns,
  201. resource_size_t offset, void *buf, size_t size, int rw,
  202. unsigned long flags)
  203. {
  204. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  205. unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
  206. sector_t sector = offset >> 9;
  207. int rc = 0, ret = 0;
  208. if (unlikely(!size))
  209. return 0;
  210. if (unlikely(offset + size > nsio->size)) {
  211. dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
  212. return -EFAULT;
  213. }
  214. if (rw == READ) {
  215. if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align)))
  216. return -EIO;
  217. if (copy_mc_to_kernel(buf, nsio->addr + offset, size) != 0)
  218. return -EIO;
  219. return 0;
  220. }
  221. if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) {
  222. if (IS_ALIGNED(offset, 512) && IS_ALIGNED(size, 512)
  223. && !(flags & NVDIMM_IO_ATOMIC)) {
  224. long cleared;
  225. might_sleep();
  226. cleared = nvdimm_clear_poison(&ndns->dev,
  227. nsio->res.start + offset, size);
  228. if (cleared < size)
  229. rc = -EIO;
  230. if (cleared > 0 && cleared / 512) {
  231. cleared /= 512;
  232. badblocks_clear(&nsio->bb, sector, cleared);
  233. }
  234. arch_invalidate_pmem(nsio->addr + offset, size);
  235. } else
  236. rc = -EIO;
  237. }
  238. memcpy_flushcache(nsio->addr + offset, buf, size);
  239. ret = nvdimm_flush(to_nd_region(ndns->dev.parent), NULL);
  240. if (ret)
  241. rc = ret;
  242. return rc;
  243. }
  244. int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio,
  245. resource_size_t size)
  246. {
  247. struct nd_namespace_common *ndns = &nsio->common;
  248. struct range range = {
  249. .start = nsio->res.start,
  250. .end = nsio->res.end,
  251. };
  252. nsio->size = size;
  253. if (!devm_request_mem_region(dev, range.start, size,
  254. dev_name(&ndns->dev))) {
  255. dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
  256. return -EBUSY;
  257. }
  258. ndns->rw_bytes = nsio_rw_bytes;
  259. if (devm_init_badblocks(dev, &nsio->bb))
  260. return -ENOMEM;
  261. nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
  262. &range);
  263. nsio->addr = devm_memremap(dev, range.start, size, ARCH_MEMREMAP_PMEM);
  264. return PTR_ERR_OR_ZERO(nsio->addr);
  265. }
  266. void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio)
  267. {
  268. struct resource *res = &nsio->res;
  269. devm_memunmap(dev, nsio->addr);
  270. devm_exit_badblocks(dev, &nsio->bb);
  271. devm_release_mem_region(dev, res->start, nsio->size);
  272. }