claim.c 7.6 KB

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