pfn_devs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. static umode_t pfn_visible(struct kobject *kobj, struct attribute *a, int n)
  245. {
  246. if (a == &dev_attr_resource.attr)
  247. return 0400;
  248. return a->mode;
  249. }
  250. struct attribute_group nd_pfn_attribute_group = {
  251. .attrs = nd_pfn_attributes,
  252. .is_visible = pfn_visible,
  253. };
  254. static const struct attribute_group *nd_pfn_attribute_groups[] = {
  255. &nd_pfn_attribute_group,
  256. &nd_device_attribute_group,
  257. &nd_numa_attribute_group,
  258. NULL,
  259. };
  260. struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
  261. struct nd_namespace_common *ndns)
  262. {
  263. struct device *dev = &nd_pfn->dev;
  264. if (!nd_pfn)
  265. return NULL;
  266. nd_pfn->mode = PFN_MODE_NONE;
  267. nd_pfn->align = PFN_DEFAULT_ALIGNMENT;
  268. dev = &nd_pfn->dev;
  269. device_initialize(&nd_pfn->dev);
  270. if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
  271. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  272. __func__, dev_name(ndns->claim));
  273. put_device(dev);
  274. return NULL;
  275. }
  276. return dev;
  277. }
  278. static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
  279. {
  280. struct nd_pfn *nd_pfn;
  281. struct device *dev;
  282. nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
  283. if (!nd_pfn)
  284. return NULL;
  285. nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
  286. if (nd_pfn->id < 0) {
  287. kfree(nd_pfn);
  288. return NULL;
  289. }
  290. dev = &nd_pfn->dev;
  291. dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
  292. dev->groups = nd_pfn_attribute_groups;
  293. dev->type = &nd_pfn_device_type;
  294. dev->parent = &nd_region->dev;
  295. return nd_pfn;
  296. }
  297. struct device *nd_pfn_create(struct nd_region *nd_region)
  298. {
  299. struct nd_pfn *nd_pfn;
  300. struct device *dev;
  301. if (!is_memory(&nd_region->dev))
  302. return NULL;
  303. nd_pfn = nd_pfn_alloc(nd_region);
  304. dev = nd_pfn_devinit(nd_pfn, NULL);
  305. __nd_device_register(dev);
  306. return dev;
  307. }
  308. /**
  309. * nd_pfn_validate - read and validate info-block
  310. * @nd_pfn: fsdax namespace runtime state / properties
  311. * @sig: 'devdax' or 'fsdax' signature
  312. *
  313. * Upon return the info-block buffer contents (->pfn_sb) are
  314. * indeterminate when validation fails, and a coherent info-block
  315. * otherwise.
  316. */
  317. int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
  318. {
  319. u64 checksum, offset;
  320. enum nd_pfn_mode mode;
  321. struct nd_namespace_io *nsio;
  322. unsigned long align, start_pad;
  323. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  324. struct nd_namespace_common *ndns = nd_pfn->ndns;
  325. const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
  326. if (!pfn_sb || !ndns)
  327. return -ENODEV;
  328. if (!is_memory(nd_pfn->dev.parent))
  329. return -ENODEV;
  330. if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
  331. return -ENXIO;
  332. if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
  333. return -ENODEV;
  334. checksum = le64_to_cpu(pfn_sb->checksum);
  335. pfn_sb->checksum = 0;
  336. if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
  337. return -ENODEV;
  338. pfn_sb->checksum = cpu_to_le64(checksum);
  339. if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
  340. return -ENODEV;
  341. if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
  342. pfn_sb->start_pad = 0;
  343. pfn_sb->end_trunc = 0;
  344. }
  345. if (__le16_to_cpu(pfn_sb->version_minor) < 2)
  346. pfn_sb->align = 0;
  347. switch (le32_to_cpu(pfn_sb->mode)) {
  348. case PFN_MODE_RAM:
  349. case PFN_MODE_PMEM:
  350. break;
  351. default:
  352. return -ENXIO;
  353. }
  354. align = le32_to_cpu(pfn_sb->align);
  355. offset = le64_to_cpu(pfn_sb->dataoff);
  356. start_pad = le32_to_cpu(pfn_sb->start_pad);
  357. if (align == 0)
  358. align = 1UL << ilog2(offset);
  359. mode = le32_to_cpu(pfn_sb->mode);
  360. if (!nd_pfn->uuid) {
  361. /*
  362. * When probing a namepace via nd_pfn_probe() the uuid
  363. * is NULL (see: nd_pfn_devinit()) we init settings from
  364. * pfn_sb
  365. */
  366. nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
  367. if (!nd_pfn->uuid)
  368. return -ENOMEM;
  369. nd_pfn->align = align;
  370. nd_pfn->mode = mode;
  371. } else {
  372. /*
  373. * When probing a pfn / dax instance we validate the
  374. * live settings against the pfn_sb
  375. */
  376. if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
  377. return -ENODEV;
  378. /*
  379. * If the uuid validates, but other settings mismatch
  380. * return EINVAL because userspace has managed to change
  381. * the configuration without specifying new
  382. * identification.
  383. */
  384. if (nd_pfn->align != align || nd_pfn->mode != mode) {
  385. dev_err(&nd_pfn->dev,
  386. "init failed, settings mismatch\n");
  387. dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
  388. nd_pfn->align, align, nd_pfn->mode,
  389. mode);
  390. return -EINVAL;
  391. }
  392. }
  393. if (align > nvdimm_namespace_capacity(ndns)) {
  394. dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
  395. align, nvdimm_namespace_capacity(ndns));
  396. return -EINVAL;
  397. }
  398. /*
  399. * These warnings are verbose because they can only trigger in
  400. * the case where the physical address alignment of the
  401. * namespace has changed since the pfn superblock was
  402. * established.
  403. */
  404. nsio = to_nd_namespace_io(&ndns->dev);
  405. if (offset >= resource_size(&nsio->res)) {
  406. dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
  407. dev_name(&ndns->dev));
  408. return -EBUSY;
  409. }
  410. if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align))
  411. || !IS_ALIGNED(offset, PAGE_SIZE)) {
  412. dev_err(&nd_pfn->dev,
  413. "bad offset: %#llx dax disabled align: %#lx\n",
  414. offset, align);
  415. return -ENXIO;
  416. }
  417. return 0;
  418. }
  419. EXPORT_SYMBOL(nd_pfn_validate);
  420. int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
  421. {
  422. int rc;
  423. struct nd_pfn *nd_pfn;
  424. struct device *pfn_dev;
  425. struct nd_pfn_sb *pfn_sb;
  426. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  427. if (ndns->force_raw)
  428. return -ENODEV;
  429. switch (ndns->claim_class) {
  430. case NVDIMM_CCLASS_NONE:
  431. case NVDIMM_CCLASS_PFN:
  432. break;
  433. default:
  434. return -ENODEV;
  435. }
  436. nvdimm_bus_lock(&ndns->dev);
  437. nd_pfn = nd_pfn_alloc(nd_region);
  438. pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
  439. nvdimm_bus_unlock(&ndns->dev);
  440. if (!pfn_dev)
  441. return -ENOMEM;
  442. pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
  443. nd_pfn = to_nd_pfn(pfn_dev);
  444. nd_pfn->pfn_sb = pfn_sb;
  445. rc = nd_pfn_validate(nd_pfn, PFN_SIG);
  446. dev_dbg(dev, "%s: pfn: %s\n", __func__,
  447. rc == 0 ? dev_name(pfn_dev) : "<none>");
  448. if (rc < 0) {
  449. nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
  450. put_device(pfn_dev);
  451. } else
  452. __nd_device_register(pfn_dev);
  453. return rc;
  454. }
  455. EXPORT_SYMBOL(nd_pfn_probe);
  456. /*
  457. * We hotplug memory at section granularity, pad the reserved area from
  458. * the previous section base to the namespace base address.
  459. */
  460. static unsigned long init_altmap_base(resource_size_t base)
  461. {
  462. unsigned long base_pfn = PHYS_PFN(base);
  463. return PFN_SECTION_ALIGN_DOWN(base_pfn);
  464. }
  465. static unsigned long init_altmap_reserve(resource_size_t base)
  466. {
  467. unsigned long reserve = PFN_UP(SZ_8K);
  468. unsigned long base_pfn = PHYS_PFN(base);
  469. reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
  470. return reserve;
  471. }
  472. static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
  473. struct resource *res, struct vmem_altmap *altmap)
  474. {
  475. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  476. u64 offset = le64_to_cpu(pfn_sb->dataoff);
  477. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  478. u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  479. struct nd_namespace_common *ndns = nd_pfn->ndns;
  480. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  481. resource_size_t base = nsio->res.start + start_pad;
  482. struct vmem_altmap __altmap = {
  483. .base_pfn = init_altmap_base(base),
  484. .reserve = init_altmap_reserve(base),
  485. };
  486. memcpy(res, &nsio->res, sizeof(*res));
  487. res->start += start_pad;
  488. res->end -= end_trunc;
  489. if (nd_pfn->mode == PFN_MODE_RAM) {
  490. if (offset < SZ_8K)
  491. return ERR_PTR(-EINVAL);
  492. nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
  493. altmap = NULL;
  494. } else if (nd_pfn->mode == PFN_MODE_PMEM) {
  495. nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res)
  496. - offset) / PAGE_SIZE);
  497. if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
  498. dev_info(&nd_pfn->dev,
  499. "number of pfns truncated from %lld to %ld\n",
  500. le64_to_cpu(nd_pfn->pfn_sb->npfns),
  501. nd_pfn->npfns);
  502. memcpy(altmap, &__altmap, sizeof(*altmap));
  503. altmap->free = PHYS_PFN(offset - SZ_8K);
  504. altmap->alloc = 0;
  505. } else
  506. return ERR_PTR(-ENXIO);
  507. return altmap;
  508. }
  509. static u64 phys_pmem_align_down(struct nd_pfn *nd_pfn, u64 phys)
  510. {
  511. return min_t(u64, PHYS_SECTION_ALIGN_DOWN(phys),
  512. ALIGN_DOWN(phys, nd_pfn->align));
  513. }
  514. /*
  515. * Check if pmem collides with 'System RAM', or other regions when
  516. * section aligned. Trim it accordingly.
  517. */
  518. static void trim_pfn_device(struct nd_pfn *nd_pfn, u32 *start_pad, u32 *end_trunc)
  519. {
  520. struct nd_namespace_common *ndns = nd_pfn->ndns;
  521. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  522. struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
  523. const resource_size_t start = nsio->res.start;
  524. const resource_size_t end = start + resource_size(&nsio->res);
  525. resource_size_t adjust, size;
  526. *start_pad = 0;
  527. *end_trunc = 0;
  528. adjust = start - PHYS_SECTION_ALIGN_DOWN(start);
  529. size = resource_size(&nsio->res) + adjust;
  530. if (region_intersects(start - adjust, size, IORESOURCE_SYSTEM_RAM,
  531. IORES_DESC_NONE) == REGION_MIXED
  532. || nd_region_conflict(nd_region, start - adjust, size))
  533. *start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
  534. /* Now check that end of the range does not collide. */
  535. adjust = PHYS_SECTION_ALIGN_UP(end) - end;
  536. size = resource_size(&nsio->res) + adjust;
  537. if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
  538. IORES_DESC_NONE) == REGION_MIXED
  539. || !IS_ALIGNED(end, nd_pfn->align)
  540. || nd_region_conflict(nd_region, start, size))
  541. *end_trunc = end - phys_pmem_align_down(nd_pfn, end);
  542. }
  543. static int nd_pfn_init(struct nd_pfn *nd_pfn)
  544. {
  545. u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
  546. struct nd_namespace_common *ndns = nd_pfn->ndns;
  547. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  548. resource_size_t start, size;
  549. struct nd_region *nd_region;
  550. u32 start_pad, end_trunc;
  551. struct nd_pfn_sb *pfn_sb;
  552. unsigned long npfns;
  553. phys_addr_t offset;
  554. const char *sig;
  555. u64 checksum;
  556. int rc;
  557. pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
  558. if (!pfn_sb)
  559. return -ENOMEM;
  560. nd_pfn->pfn_sb = pfn_sb;
  561. if (is_nd_dax(&nd_pfn->dev))
  562. sig = DAX_SIG;
  563. else
  564. sig = PFN_SIG;
  565. rc = nd_pfn_validate(nd_pfn, sig);
  566. if (rc != -ENODEV)
  567. return rc;
  568. /* no info block, do init */;
  569. memset(pfn_sb, 0, sizeof(*pfn_sb));
  570. nd_region = to_nd_region(nd_pfn->dev.parent);
  571. if (nd_region->ro) {
  572. dev_info(&nd_pfn->dev,
  573. "%s is read-only, unable to init metadata\n",
  574. dev_name(&nd_region->dev));
  575. return -ENXIO;
  576. }
  577. memset(pfn_sb, 0, sizeof(*pfn_sb));
  578. trim_pfn_device(nd_pfn, &start_pad, &end_trunc);
  579. if (start_pad + end_trunc)
  580. dev_info(&nd_pfn->dev, "%s alignment collision, truncate %d bytes\n",
  581. dev_name(&ndns->dev), start_pad + end_trunc);
  582. /*
  583. * Note, we use 64 here for the standard size of struct page,
  584. * debugging options may cause it to be larger in which case the
  585. * implementation will limit the pfns advertised through
  586. * ->direct_access() to those that are included in the memmap.
  587. */
  588. start = nsio->res.start + start_pad;
  589. size = resource_size(&nsio->res);
  590. npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
  591. / PAGE_SIZE);
  592. if (nd_pfn->mode == PFN_MODE_PMEM) {
  593. /*
  594. * The altmap should be padded out to the block size used
  595. * when populating the vmemmap. This *should* be equal to
  596. * PMD_SIZE for most architectures.
  597. */
  598. offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve,
  599. max(nd_pfn->align, PMD_SIZE)) - start;
  600. } else if (nd_pfn->mode == PFN_MODE_RAM)
  601. offset = ALIGN(start + SZ_8K + dax_label_reserve,
  602. nd_pfn->align) - start;
  603. else
  604. return -ENXIO;
  605. if (offset + start_pad + end_trunc >= size) {
  606. dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
  607. dev_name(&ndns->dev));
  608. return -ENXIO;
  609. }
  610. npfns = (size - offset - start_pad - end_trunc) / SZ_4K;
  611. pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
  612. pfn_sb->dataoff = cpu_to_le64(offset);
  613. pfn_sb->npfns = cpu_to_le64(npfns);
  614. memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
  615. memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
  616. memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
  617. pfn_sb->version_major = cpu_to_le16(1);
  618. pfn_sb->version_minor = cpu_to_le16(3);
  619. pfn_sb->start_pad = cpu_to_le32(start_pad);
  620. pfn_sb->end_trunc = cpu_to_le32(end_trunc);
  621. pfn_sb->align = cpu_to_le32(nd_pfn->align);
  622. checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
  623. pfn_sb->checksum = cpu_to_le64(checksum);
  624. return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
  625. }
  626. /*
  627. * Determine the effective resource range and vmem_altmap from an nd_pfn
  628. * instance.
  629. */
  630. struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
  631. struct resource *res, struct vmem_altmap *altmap)
  632. {
  633. int rc;
  634. if (!nd_pfn->uuid || !nd_pfn->ndns)
  635. return ERR_PTR(-ENODEV);
  636. rc = nd_pfn_init(nd_pfn);
  637. if (rc)
  638. return ERR_PTR(rc);
  639. /* we need a valid pfn_sb before we can init a vmem_altmap */
  640. return __nvdimm_setup_pfn(nd_pfn, res, altmap);
  641. }
  642. EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);