pfn_devs.c 18 KB

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