pfn_devs.c 17 KB

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