Merge tag 'iommu-updates-v6.6' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[linux-2.6-block.git] / drivers / dax / pmem.c
CommitLineData
730926c3
DW
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
ab68f262
DW
3#include <linux/memremap.h>
4#include <linux/module.h>
5#include <linux/pfn_t.h>
83762cb5
DW
6#include "../nvdimm/pfn.h"
7#include "../nvdimm/nd.h"
8#include "bus.h"
ab68f262 9
83762cb5 10static struct dev_dax *__dax_pmem_probe(struct device *dev)
ab68f262 11{
a4574f63 12 struct range range;
bbb3be17 13 int rc, id, region_id;
89ec9f2c 14 resource_size_t offset;
ab68f262 15 struct nd_pfn_sb *pfn_sb;
174ebece 16 struct dev_dax_data data;
ab68f262
DW
17 struct nd_namespace_io *nsio;
18 struct dax_region *dax_region;
1e240e8d 19 struct dev_pagemap pgmap = { };
ab68f262
DW
20 struct nd_namespace_common *ndns;
21 struct nd_dax *nd_dax = to_nd_dax(dev);
22 struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
8fc5c735 23 struct nd_region *nd_region = to_nd_region(dev->parent);
ab68f262
DW
24
25 ndns = nvdimm_namespace_common_probe(dev);
26 if (IS_ERR(ndns))
730926c3 27 return ERR_CAST(ndns);
ab68f262
DW
28
29 /* parse the 'pfn' info block via ->rw_bytes */
8f4b01fc 30 rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
6a84fb4b 31 if (rc)
730926c3 32 return ERR_PTR(rc);
89ec9f2c 33 rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
e8d51348 34 if (rc)
730926c3 35 return ERR_PTR(rc);
8f4b01fc 36 devm_namespace_disable(dev, ndns);
ab68f262 37
89ec9f2c 38 /* reserve the metadata area, device-dax will reserve the data */
d521fbae 39 pfn_sb = nd_pfn->pfn_sb;
89ec9f2c 40 offset = le64_to_cpu(pfn_sb->dataoff);
8f4b01fc 41 nsio = to_nd_namespace_io(&ndns->dev);
89ec9f2c 42 if (!devm_request_mem_region(dev, nsio->res.start, offset,
450c6633 43 dev_name(&ndns->dev))) {
d521fbae 44 dev_warn(dev, "could not reserve metadata\n");
730926c3 45 return ERR_PTR(-EBUSY);
d521fbae 46 }
d0e58455 47
bbb3be17
DW
48 rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
49 if (rc != 2)
730926c3 50 return ERR_PTR(-EINVAL);
bbb3be17 51
a4574f63
DW
52 /* adjust the dax_region range to the start of data */
53 range = pgmap.range;
4e6a7b3b 54 range.start += offset;
a4574f63 55 dax_region = alloc_dax_region(dev, region_id, &range,
c2f3011e
DW
56 nd_region->target_node, le32_to_cpu(pfn_sb->align),
57 IORESOURCE_DAX_STATIC);
ab68f262 58 if (!dax_region)
730926c3 59 return ERR_PTR(-ENOMEM);
ab68f262 60
174ebece
DW
61 data = (struct dev_dax_data) {
62 .dax_region = dax_region,
63 .id = id,
64 .pgmap = &pgmap,
a4574f63 65 .size = range_len(&range),
174ebece 66 };
ab68f262 67
2532f416 68 return devm_create_dev_dax(&data);
ab68f262 69}
83762cb5
DW
70
71static int dax_pmem_probe(struct device *dev)
72{
73 return PTR_ERR_OR_ZERO(__dax_pmem_probe(dev));
74}
75
76static struct nd_device_driver dax_pmem_driver = {
77 .probe = dax_pmem_probe,
78 .drv = {
79 .name = "dax_pmem",
80 },
81 .type = ND_DRIVER_DAX_PMEM,
82};
83
84static int __init dax_pmem_init(void)
85{
86 return nd_driver_register(&dax_pmem_driver);
87}
88module_init(dax_pmem_init);
89
90static void __exit dax_pmem_exit(void)
91{
92 driver_unregister(&dax_pmem_driver.drv);
93}
94module_exit(dax_pmem_exit);
ab68f262
DW
95
96MODULE_LICENSE("GPL v2");
97MODULE_AUTHOR("Intel Corporation");
83762cb5 98MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);