pfn_devs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright(c) 2013-2016 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. #include <linux/memremap.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/device.h>
  16. #include <linux/genhd.h>
  17. #include <linux/sizes.h>
  18. #include <linux/slab.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include "nd-core.h"
  22. #include "pfn.h"
  23. #include "nd.h"
  24. static void nd_pfn_release(struct device *dev)
  25. {
  26. struct nd_region *nd_region = to_nd_region(dev->parent);
  27. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  28. dev_dbg(dev, "%s\n", __func__);
  29. nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
  30. ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
  31. kfree(nd_pfn->uuid);
  32. kfree(nd_pfn);
  33. }
  34. static struct device_type nd_pfn_device_type = {
  35. .name = "nd_pfn",
  36. .release = nd_pfn_release,
  37. };
  38. bool is_nd_pfn(struct device *dev)
  39. {
  40. return dev ? dev->type == &nd_pfn_device_type : false;
  41. }
  42. EXPORT_SYMBOL(is_nd_pfn);
  43. struct nd_pfn *to_nd_pfn(struct device *dev)
  44. {
  45. struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
  46. WARN_ON(!is_nd_pfn(dev));
  47. return nd_pfn;
  48. }
  49. EXPORT_SYMBOL(to_nd_pfn);
  50. static ssize_t mode_show(struct device *dev,
  51. struct device_attribute *attr, char *buf)
  52. {
  53. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  54. switch (nd_pfn->mode) {
  55. case PFN_MODE_RAM:
  56. return sprintf(buf, "ram\n");
  57. case PFN_MODE_PMEM:
  58. return sprintf(buf, "pmem\n");
  59. default:
  60. return sprintf(buf, "none\n");
  61. }
  62. }
  63. static ssize_t mode_store(struct device *dev,
  64. struct device_attribute *attr, const char *buf, size_t len)
  65. {
  66. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  67. ssize_t rc = 0;
  68. device_lock(dev);
  69. nvdimm_bus_lock(dev);
  70. if (dev->driver)
  71. rc = -EBUSY;
  72. else {
  73. size_t n = len - 1;
  74. if (strncmp(buf, "pmem\n", n) == 0
  75. || strncmp(buf, "pmem", n) == 0) {
  76. nd_pfn->mode = PFN_MODE_PMEM;
  77. } else if (strncmp(buf, "ram\n", n) == 0
  78. || strncmp(buf, "ram", n) == 0)
  79. nd_pfn->mode = PFN_MODE_RAM;
  80. else if (strncmp(buf, "none\n", n) == 0
  81. || strncmp(buf, "none", n) == 0)
  82. nd_pfn->mode = PFN_MODE_NONE;
  83. else
  84. rc = -EINVAL;
  85. }
  86. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  87. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  88. nvdimm_bus_unlock(dev);
  89. device_unlock(dev);
  90. return rc ? rc : len;
  91. }
  92. static DEVICE_ATTR_RW(mode);
  93. static ssize_t align_show(struct device *dev,
  94. struct device_attribute *attr, char *buf)
  95. {
  96. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  97. return sprintf(buf, "%ld\n", nd_pfn->align);
  98. }
  99. static const unsigned long *nd_pfn_supported_alignments(void)
  100. {
  101. /*
  102. * This needs to be a non-static variable because the *_SIZE
  103. * macros aren't always constants.
  104. */
  105. const unsigned long supported_alignments[] = {
  106. PAGE_SIZE,
  107. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  108. HPAGE_PMD_SIZE,
  109. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  110. HPAGE_PUD_SIZE,
  111. #endif
  112. #endif
  113. 0,
  114. };
  115. static unsigned long data[ARRAY_SIZE(supported_alignments)];
  116. memcpy(data, supported_alignments, sizeof(data));
  117. return data;
  118. }
  119. static ssize_t align_store(struct device *dev,
  120. struct device_attribute *attr, const char *buf, size_t len)
  121. {
  122. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  123. ssize_t rc;
  124. device_lock(dev);
  125. nvdimm_bus_lock(dev);
  126. rc = nd_size_select_store(dev, buf, &nd_pfn->align,
  127. nd_pfn_supported_alignments());
  128. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  129. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  130. nvdimm_bus_unlock(dev);
  131. device_unlock(dev);
  132. return rc ? rc : len;
  133. }
  134. static DEVICE_ATTR_RW(align);
  135. static ssize_t uuid_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  139. if (nd_pfn->uuid)
  140. return sprintf(buf, "%pUb\n", nd_pfn->uuid);
  141. return sprintf(buf, "\n");
  142. }
  143. static ssize_t uuid_store(struct device *dev,
  144. struct device_attribute *attr, const char *buf, size_t len)
  145. {
  146. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  147. ssize_t rc;
  148. device_lock(dev);
  149. rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
  150. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  151. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  152. device_unlock(dev);
  153. return rc ? rc : len;
  154. }
  155. static DEVICE_ATTR_RW(uuid);
  156. static ssize_t namespace_show(struct device *dev,
  157. struct device_attribute *attr, char *buf)
  158. {
  159. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  160. ssize_t rc;
  161. nvdimm_bus_lock(dev);
  162. rc = sprintf(buf, "%s\n", nd_pfn->ndns
  163. ? dev_name(&nd_pfn->ndns->dev) : "");
  164. nvdimm_bus_unlock(dev);
  165. return rc;
  166. }
  167. static ssize_t namespace_store(struct device *dev,
  168. struct device_attribute *attr, const char *buf, size_t len)
  169. {
  170. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  171. ssize_t rc;
  172. device_lock(dev);
  173. nvdimm_bus_lock(dev);
  174. rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
  175. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  176. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  177. nvdimm_bus_unlock(dev);
  178. device_unlock(dev);
  179. return rc;
  180. }
  181. static DEVICE_ATTR_RW(namespace);
  182. static ssize_t resource_show(struct device *dev,
  183. struct device_attribute *attr, char *buf)
  184. {
  185. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  186. ssize_t rc;
  187. device_lock(dev);
  188. if (dev->driver) {
  189. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  190. u64 offset = __le64_to_cpu(pfn_sb->dataoff);
  191. struct nd_namespace_common *ndns = nd_pfn->ndns;
  192. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  193. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  194. rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
  195. + start_pad + offset);
  196. } else {
  197. /* no address to convey if the pfn instance is disabled */
  198. rc = -ENXIO;
  199. }
  200. device_unlock(dev);
  201. return rc;
  202. }
  203. static DEVICE_ATTR_RO(resource);
  204. static ssize_t size_show(struct device *dev,
  205. struct device_attribute *attr, char *buf)
  206. {
  207. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  208. ssize_t rc;
  209. device_lock(dev);
  210. if (dev->driver) {
  211. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  212. u64 offset = __le64_to_cpu(pfn_sb->dataoff);
  213. struct nd_namespace_common *ndns = nd_pfn->ndns;
  214. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  215. u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  216. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  217. rc = sprintf(buf, "%llu\n", (unsigned long long)
  218. resource_size(&nsio->res) - start_pad
  219. - end_trunc - offset);
  220. } else {
  221. /* no size to convey if the pfn instance is disabled */
  222. rc = -ENXIO;
  223. }
  224. device_unlock(dev);
  225. return rc;
  226. }
  227. static DEVICE_ATTR_RO(size);
  228. static ssize_t supported_alignments_show(struct device *dev,
  229. struct device_attribute *attr, char *buf)
  230. {
  231. return nd_size_select_show(0, nd_pfn_supported_alignments(), buf);
  232. }
  233. static DEVICE_ATTR_RO(supported_alignments);
  234. static struct attribute *nd_pfn_attributes[] = {
  235. &dev_attr_mode.attr,
  236. &dev_attr_namespace.attr,
  237. &dev_attr_uuid.attr,
  238. &dev_attr_align.attr,
  239. &dev_attr_resource.attr,
  240. &dev_attr_size.attr,
  241. &dev_attr_supported_alignments.attr,
  242. NULL,
  243. };
  244. struct attribute_group nd_pfn_attribute_group = {
  245. .attrs = nd_pfn_attributes,
  246. };
  247. static const struct attribute_group *nd_pfn_attribute_groups[] = {
  248. &nd_pfn_attribute_group,
  249. &nd_device_attribute_group,
  250. &nd_numa_attribute_group,
  251. NULL,
  252. };
  253. struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
  254. struct nd_namespace_common *ndns)
  255. {
  256. struct device *dev = &nd_pfn->dev;
  257. if (!nd_pfn)
  258. return NULL;
  259. nd_pfn->mode = PFN_MODE_NONE;
  260. nd_pfn->align = PFN_DEFAULT_ALIGNMENT;
  261. dev = &nd_pfn->dev;
  262. device_initialize(&nd_pfn->dev);
  263. if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
  264. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  265. __func__, dev_name(ndns->claim));
  266. put_device(dev);
  267. return NULL;
  268. }
  269. return dev;
  270. }
  271. static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
  272. {
  273. struct nd_pfn *nd_pfn;
  274. struct device *dev;
  275. nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
  276. if (!nd_pfn)
  277. return NULL;
  278. nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
  279. if (nd_pfn->id < 0) {
  280. kfree(nd_pfn);
  281. return NULL;
  282. }
  283. dev = &nd_pfn->dev;
  284. dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
  285. dev->groups = nd_pfn_attribute_groups;
  286. dev->type = &nd_pfn_device_type;
  287. dev->parent = &nd_region->dev;
  288. return nd_pfn;
  289. }
  290. struct device *nd_pfn_create(struct nd_region *nd_region)
  291. {
  292. struct nd_pfn *nd_pfn;
  293. struct device *dev;
  294. if (!is_memory(&nd_region->dev))
  295. return NULL;
  296. nd_pfn = nd_pfn_alloc(nd_region);
  297. dev = nd_pfn_devinit(nd_pfn, NULL);
  298. __nd_device_register(dev);
  299. return dev;
  300. }
  301. int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
  302. {
  303. u64 checksum, offset;
  304. unsigned long align;
  305. enum nd_pfn_mode mode;
  306. struct nd_namespace_io *nsio;
  307. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  308. struct nd_namespace_common *ndns = nd_pfn->ndns;
  309. const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
  310. if (!pfn_sb || !ndns)
  311. return -ENODEV;
  312. if (!is_memory(nd_pfn->dev.parent))
  313. return -ENODEV;
  314. if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
  315. return -ENXIO;
  316. if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
  317. return -ENODEV;
  318. checksum = le64_to_cpu(pfn_sb->checksum);
  319. pfn_sb->checksum = 0;
  320. if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
  321. return -ENODEV;
  322. pfn_sb->checksum = cpu_to_le64(checksum);
  323. if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
  324. return -ENODEV;
  325. if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
  326. pfn_sb->start_pad = 0;
  327. pfn_sb->end_trunc = 0;
  328. }
  329. if (__le16_to_cpu(pfn_sb->version_minor) < 2)
  330. pfn_sb->align = 0;
  331. switch (le32_to_cpu(pfn_sb->mode)) {
  332. case PFN_MODE_RAM:
  333. case PFN_MODE_PMEM:
  334. break;
  335. default:
  336. return -ENXIO;
  337. }
  338. align = le32_to_cpu(pfn_sb->align);
  339. offset = le64_to_cpu(pfn_sb->dataoff);
  340. if (align == 0)
  341. align = 1UL << ilog2(offset);
  342. mode = le32_to_cpu(pfn_sb->mode);
  343. if (!nd_pfn->uuid) {
  344. /*
  345. * When probing a namepace via nd_pfn_probe() the uuid
  346. * is NULL (see: nd_pfn_devinit()) we init settings from
  347. * pfn_sb
  348. */
  349. nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
  350. if (!nd_pfn->uuid)
  351. return -ENOMEM;
  352. nd_pfn->align = align;
  353. nd_pfn->mode = mode;
  354. } else {
  355. /*
  356. * When probing a pfn / dax instance we validate the
  357. * live settings against the pfn_sb
  358. */
  359. if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
  360. return -ENODEV;
  361. /*
  362. * If the uuid validates, but other settings mismatch
  363. * return EINVAL because userspace has managed to change
  364. * the configuration without specifying new
  365. * identification.
  366. */
  367. if (nd_pfn->align != align || nd_pfn->mode != mode) {
  368. dev_err(&nd_pfn->dev,
  369. "init failed, settings mismatch\n");
  370. dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
  371. nd_pfn->align, align, nd_pfn->mode,
  372. mode);
  373. return -EINVAL;
  374. }
  375. }
  376. if (align > nvdimm_namespace_capacity(ndns)) {
  377. dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
  378. align, nvdimm_namespace_capacity(ndns));
  379. return -EINVAL;
  380. }
  381. /*
  382. * These warnings are verbose because they can only trigger in
  383. * the case where the physical address alignment of the
  384. * namespace has changed since the pfn superblock was
  385. * established.
  386. */
  387. nsio = to_nd_namespace_io(&ndns->dev);
  388. if (offset >= resource_size(&nsio->res)) {
  389. dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
  390. dev_name(&ndns->dev));
  391. return -EBUSY;
  392. }
  393. if ((align && !IS_ALIGNED(offset, align))
  394. || !IS_ALIGNED(offset, PAGE_SIZE)) {
  395. dev_err(&nd_pfn->dev,
  396. "bad offset: %#llx dax disabled align: %#lx\n",
  397. offset, align);
  398. return -ENXIO;
  399. }
  400. return 0;
  401. }
  402. EXPORT_SYMBOL(nd_pfn_validate);
  403. int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
  404. {
  405. int rc;
  406. struct nd_pfn *nd_pfn;
  407. struct device *pfn_dev;
  408. struct nd_pfn_sb *pfn_sb;
  409. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  410. if (ndns->force_raw)
  411. return -ENODEV;
  412. switch (ndns->claim_class) {
  413. case NVDIMM_CCLASS_NONE:
  414. case NVDIMM_CCLASS_PFN:
  415. break;
  416. default:
  417. return -ENODEV;
  418. }
  419. nvdimm_bus_lock(&ndns->dev);
  420. nd_pfn = nd_pfn_alloc(nd_region);
  421. pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
  422. nvdimm_bus_unlock(&ndns->dev);
  423. if (!pfn_dev)
  424. return -ENOMEM;
  425. pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
  426. nd_pfn = to_nd_pfn(pfn_dev);
  427. nd_pfn->pfn_sb = pfn_sb;
  428. rc = nd_pfn_validate(nd_pfn, PFN_SIG);
  429. dev_dbg(dev, "%s: pfn: %s\n", __func__,
  430. rc == 0 ? dev_name(pfn_dev) : "<none>");
  431. if (rc < 0) {
  432. nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
  433. put_device(pfn_dev);
  434. } else
  435. __nd_device_register(pfn_dev);
  436. return rc;
  437. }
  438. EXPORT_SYMBOL(nd_pfn_probe);
  439. /*
  440. * We hotplug memory at section granularity, pad the reserved area from
  441. * the previous section base to the namespace base address.
  442. */
  443. static unsigned long init_altmap_base(resource_size_t base)
  444. {
  445. unsigned long base_pfn = PHYS_PFN(base);
  446. return PFN_SECTION_ALIGN_DOWN(base_pfn);
  447. }
  448. static unsigned long init_altmap_reserve(resource_size_t base)
  449. {
  450. unsigned long reserve = PHYS_PFN(SZ_8K);
  451. unsigned long base_pfn = PHYS_PFN(base);
  452. reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
  453. return reserve;
  454. }
  455. static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
  456. struct resource *res, struct vmem_altmap *altmap)
  457. {
  458. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  459. u64 offset = le64_to_cpu(pfn_sb->dataoff);
  460. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  461. u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  462. struct nd_namespace_common *ndns = nd_pfn->ndns;
  463. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  464. resource_size_t base = nsio->res.start + start_pad;
  465. struct vmem_altmap __altmap = {
  466. .base_pfn = init_altmap_base(base),
  467. .reserve = init_altmap_reserve(base),
  468. };
  469. memcpy(res, &nsio->res, sizeof(*res));
  470. res->start += start_pad;
  471. res->end -= end_trunc;
  472. if (nd_pfn->mode == PFN_MODE_RAM) {
  473. if (offset < SZ_8K)
  474. return ERR_PTR(-EINVAL);
  475. nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
  476. altmap = NULL;
  477. } else if (nd_pfn->mode == PFN_MODE_PMEM) {
  478. nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res)
  479. - offset) / PAGE_SIZE);
  480. if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
  481. dev_info(&nd_pfn->dev,
  482. "number of pfns truncated from %lld to %ld\n",
  483. le64_to_cpu(nd_pfn->pfn_sb->npfns),
  484. nd_pfn->npfns);
  485. memcpy(altmap, &__altmap, sizeof(*altmap));
  486. altmap->free = PHYS_PFN(offset - SZ_8K);
  487. altmap->alloc = 0;
  488. } else
  489. return ERR_PTR(-ENXIO);
  490. return altmap;
  491. }
  492. static int nd_pfn_init(struct nd_pfn *nd_pfn)
  493. {
  494. u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
  495. struct nd_namespace_common *ndns = nd_pfn->ndns;
  496. u32 start_pad = 0, end_trunc = 0;
  497. resource_size_t start, size;
  498. struct nd_namespace_io *nsio;
  499. struct nd_region *nd_region;
  500. struct nd_pfn_sb *pfn_sb;
  501. unsigned long npfns;
  502. phys_addr_t offset;
  503. const char *sig;
  504. u64 checksum;
  505. int rc;
  506. pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
  507. if (!pfn_sb)
  508. return -ENOMEM;
  509. nd_pfn->pfn_sb = pfn_sb;
  510. if (is_nd_dax(&nd_pfn->dev))
  511. sig = DAX_SIG;
  512. else
  513. sig = PFN_SIG;
  514. rc = nd_pfn_validate(nd_pfn, sig);
  515. if (rc != -ENODEV)
  516. return rc;
  517. /* no info block, do init */;
  518. nd_region = to_nd_region(nd_pfn->dev.parent);
  519. if (nd_region->ro) {
  520. dev_info(&nd_pfn->dev,
  521. "%s is read-only, unable to init metadata\n",
  522. dev_name(&nd_region->dev));
  523. return -ENXIO;
  524. }
  525. memset(pfn_sb, 0, sizeof(*pfn_sb));
  526. /*
  527. * Check if pmem collides with 'System RAM' when section aligned and
  528. * trim it accordingly
  529. */
  530. nsio = to_nd_namespace_io(&ndns->dev);
  531. start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
  532. size = resource_size(&nsio->res);
  533. if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
  534. IORES_DESC_NONE) == REGION_MIXED) {
  535. start = nsio->res.start;
  536. start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
  537. }
  538. start = nsio->res.start;
  539. size = PHYS_SECTION_ALIGN_UP(start + size) - start;
  540. if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
  541. IORES_DESC_NONE) == REGION_MIXED) {
  542. size = resource_size(&nsio->res);
  543. end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size);
  544. }
  545. if (start_pad + end_trunc)
  546. dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n",
  547. dev_name(&ndns->dev), start_pad + end_trunc);
  548. /*
  549. * Note, we use 64 here for the standard size of struct page,
  550. * debugging options may cause it to be larger in which case the
  551. * implementation will limit the pfns advertised through
  552. * ->direct_access() to those that are included in the memmap.
  553. */
  554. start += start_pad;
  555. size = resource_size(&nsio->res);
  556. npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
  557. / PAGE_SIZE);
  558. if (nd_pfn->mode == PFN_MODE_PMEM) {
  559. /*
  560. * The altmap should be padded out to the block size used
  561. * when populating the vmemmap. This *should* be equal to
  562. * PMD_SIZE for most architectures.
  563. */
  564. offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve,
  565. max(nd_pfn->align, PMD_SIZE)) - start;
  566. } else if (nd_pfn->mode == PFN_MODE_RAM)
  567. offset = ALIGN(start + SZ_8K + dax_label_reserve,
  568. nd_pfn->align) - start;
  569. else
  570. return -ENXIO;
  571. if (offset + start_pad + end_trunc >= size) {
  572. dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
  573. dev_name(&ndns->dev));
  574. return -ENXIO;
  575. }
  576. npfns = (size - offset - start_pad - end_trunc) / SZ_4K;
  577. pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
  578. pfn_sb->dataoff = cpu_to_le64(offset);
  579. pfn_sb->npfns = cpu_to_le64(npfns);
  580. memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
  581. memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
  582. memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
  583. pfn_sb->version_major = cpu_to_le16(1);
  584. pfn_sb->version_minor = cpu_to_le16(2);
  585. pfn_sb->start_pad = cpu_to_le32(start_pad);
  586. pfn_sb->end_trunc = cpu_to_le32(end_trunc);
  587. pfn_sb->align = cpu_to_le32(nd_pfn->align);
  588. checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
  589. pfn_sb->checksum = cpu_to_le64(checksum);
  590. return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
  591. }
  592. /*
  593. * Determine the effective resource range and vmem_altmap from an nd_pfn
  594. * instance.
  595. */
  596. struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
  597. struct resource *res, struct vmem_altmap *altmap)
  598. {
  599. int rc;
  600. if (!nd_pfn->uuid || !nd_pfn->ndns)
  601. return ERR_PTR(-ENODEV);
  602. rc = nd_pfn_init(nd_pfn);
  603. if (rc)
  604. return ERR_PTR(rc);
  605. /* we need a valid pfn_sb before we can init a vmem_altmap */
  606. return __nvdimm_setup_pfn(nd_pfn, res, altmap);
  607. }
  608. EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);