core.c 2.1 KB
Newer Older
1 2
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
3 4 5
#include <linux/memremap.h>
#include <linux/module.h>
#include <linux/pfn_t.h>
6 7 8
#include "../../nvdimm/pfn.h"
#include "../../nvdimm/nd.h"
#include "../bus.h"
9

10
struct dev_dax *__dax_pmem_probe(struct device *dev, enum dev_dax_subsys subsys)
11 12
{
	struct resource res;
13
	int rc, id, region_id;
14
	resource_size_t offset;
15
	struct nd_pfn_sb *pfn_sb;
16
	struct dev_dax *dev_dax;
17 18
	struct nd_namespace_io *nsio;
	struct dax_region *dax_region;
19
	struct dev_pagemap pgmap = { };
20 21 22
	struct nd_namespace_common *ndns;
	struct nd_dax *nd_dax = to_nd_dax(dev);
	struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
23
	struct nd_region *nd_region = to_nd_region(dev->parent);
24 25 26

	ndns = nvdimm_namespace_common_probe(dev);
	if (IS_ERR(ndns))
27
		return ERR_CAST(ndns);
28 29

	/* parse the 'pfn' info block via ->rw_bytes */
30
	rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
31
	if (rc)
32
		return ERR_PTR(rc);
33
	rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
34
	if (rc)
35
		return ERR_PTR(rc);
36
	devm_namespace_disable(dev, ndns);
37

38
	/* reserve the metadata area, device-dax will reserve the data */
39
	pfn_sb = nd_pfn->pfn_sb;
40
	offset = le64_to_cpu(pfn_sb->dataoff);
41
	nsio = to_nd_namespace_io(&ndns->dev);
42
	if (!devm_request_mem_region(dev, nsio->res.start, offset,
43
				dev_name(&ndns->dev))) {
44
		dev_warn(dev, "could not reserve metadata\n");
45
		return ERR_PTR(-EBUSY);
46
	}
D
Dan Williams 已提交
47

48 49
	rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
	if (rc != 2)
50
		return ERR_PTR(-EINVAL);
51

52 53 54
	/* adjust the dax_region resource to the start of data */
	memcpy(&res, &pgmap.res, sizeof(res));
	res.start += offset;
55
	dax_region = alloc_dax_region(dev, region_id, &res,
56 57
			nd_region->target_node, le32_to_cpu(pfn_sb->align),
			PFN_DEV|PFN_MAP);
58
	if (!dax_region)
59
		return ERR_PTR(-ENOMEM);
60

61
	dev_dax = __devm_create_dev_dax(dax_region, id, &pgmap, subsys);
62

63
	/* child dev_dax instances now own the lifetime of the dax_region */
64 65
	dax_region_put(dax_region);

66
	return dev_dax;
67
}
68
EXPORT_SYMBOL_GPL(__dax_pmem_probe);
69 70 71

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Intel Corporation");