blob: f050ea78bb83b3df47648fc696d0e3e6d3eadf97 [file] [log] [blame]
Dan Williams730926c2017-07-16 13:51:53 -07001// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
3#include <linux/memremap.h>
4#include <linux/module.h>
5#include <linux/pfn_t.h>
Dan Williams83762cb2021-11-15 13:20:57 -08006#include "../nvdimm/pfn.h"
7#include "../nvdimm/nd.h"
8#include "bus.h"
Dan Williams730926c2017-07-16 13:51:53 -07009
Dan Williams83762cb2021-11-15 13:20:57 -080010static struct dev_dax *__dax_pmem_probe(struct device *dev)
Dan Williams730926c2017-07-16 13:51:53 -070011{
Dan Williamsa4574f62020-10-13 16:50:29 -070012 struct range range;
Dan Williams730926c2017-07-16 13:51:53 -070013 int rc, id, region_id;
14 resource_size_t offset;
15 struct nd_pfn_sb *pfn_sb;
16 struct dev_dax *dev_dax;
Dan Williams174ebec2020-10-13 16:49:38 -070017 struct dev_dax_data data;
Dan Williams730926c2017-07-16 13:51:53 -070018 struct nd_namespace_io *nsio;
19 struct dax_region *dax_region;
Christoph Hellwig1e240e82019-06-26 14:27:08 +020020 struct dev_pagemap pgmap = { };
Dan Williams730926c2017-07-16 13:51:53 -070021 struct nd_namespace_common *ndns;
22 struct nd_dax *nd_dax = to_nd_dax(dev);
23 struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
Dan Williams8fc5c732018-11-09 12:43:07 -080024 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams730926c2017-07-16 13:51:53 -070025
26 ndns = nvdimm_namespace_common_probe(dev);
27 if (IS_ERR(ndns))
28 return ERR_CAST(ndns);
Dan Williams730926c2017-07-16 13:51:53 -070029
30 /* parse the 'pfn' info block via ->rw_bytes */
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +053031 rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
Dan Williams730926c2017-07-16 13:51:53 -070032 if (rc)
33 return ERR_PTR(rc);
34 rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
35 if (rc)
36 return ERR_PTR(rc);
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +053037 devm_namespace_disable(dev, ndns);
Dan Williams730926c2017-07-16 13:51:53 -070038
39 /* reserve the metadata area, device-dax will reserve the data */
Vishal Vermad521fba2019-03-18 16:53:37 -060040 pfn_sb = nd_pfn->pfn_sb;
Dan Williams730926c2017-07-16 13:51:53 -070041 offset = le64_to_cpu(pfn_sb->dataoff);
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +053042 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams730926c2017-07-16 13:51:53 -070043 if (!devm_request_mem_region(dev, nsio->res.start, offset,
44 dev_name(&ndns->dev))) {
Vishal Vermad521fba2019-03-18 16:53:37 -060045 dev_warn(dev, "could not reserve metadata\n");
Dan Williams730926c2017-07-16 13:51:53 -070046 return ERR_PTR(-EBUSY);
Vishal Vermad521fba2019-03-18 16:53:37 -060047 }
Dan Williams730926c2017-07-16 13:51:53 -070048
49 rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
50 if (rc != 2)
51 return ERR_PTR(-EINVAL);
52
Dan Williamsa4574f62020-10-13 16:50:29 -070053 /* adjust the dax_region range to the start of data */
54 range = pgmap.range;
Zheng Yongjun4e6a7b32020-12-14 21:45:06 +080055 range.start += offset;
Dan Williamsa4574f62020-10-13 16:50:29 -070056 dax_region = alloc_dax_region(dev, region_id, &range,
Dan Williamsc2f30112020-10-13 16:50:03 -070057 nd_region->target_node, le32_to_cpu(pfn_sb->align),
58 IORESOURCE_DAX_STATIC);
Dan Williams730926c2017-07-16 13:51:53 -070059 if (!dax_region)
60 return ERR_PTR(-ENOMEM);
61
Dan Williams174ebec2020-10-13 16:49:38 -070062 data = (struct dev_dax_data) {
63 .dax_region = dax_region,
64 .id = id,
65 .pgmap = &pgmap,
Dan Williamsa4574f62020-10-13 16:50:29 -070066 .size = range_len(&range),
Dan Williams174ebec2020-10-13 16:49:38 -070067 };
68 dev_dax = devm_create_dev_dax(&data);
Dan Williams730926c2017-07-16 13:51:53 -070069
70 /* child dev_dax instances now own the lifetime of the dax_region */
71 dax_region_put(dax_region);
72
73 return dev_dax;
74}
Dan Williams83762cb2021-11-15 13:20:57 -080075
76static int dax_pmem_probe(struct device *dev)
77{
78 return PTR_ERR_OR_ZERO(__dax_pmem_probe(dev));
79}
80
81static struct nd_device_driver dax_pmem_driver = {
82 .probe = dax_pmem_probe,
83 .drv = {
84 .name = "dax_pmem",
85 },
86 .type = ND_DRIVER_DAX_PMEM,
87};
88
89static int __init dax_pmem_init(void)
90{
91 return nd_driver_register(&dax_pmem_driver);
92}
93module_init(dax_pmem_init);
94
95static void __exit dax_pmem_exit(void)
96{
97 driver_unregister(&dax_pmem_driver.drv);
98}
99module_exit(dax_pmem_exit);
Dan Williams730926c2017-07-16 13:51:53 -0700100
101MODULE_LICENSE("GPL v2");
102MODULE_AUTHOR("Intel Corporation");
Dan Williams83762cb2021-11-15 13:20:57 -0800103MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);