claim.c 7.6 KB

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