1 // 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>
6 #include "../nvdimm/pfn.h"
7 #include "../nvdimm/nd.h"
10 static struct dev_dax
*__dax_pmem_probe(struct device
*dev
)
13 int rc
, id
, region_id
;
14 resource_size_t offset
;
15 struct nd_pfn_sb
*pfn_sb
;
16 struct dev_dax_data data
;
17 struct nd_namespace_io
*nsio
;
18 struct dax_region
*dax_region
;
19 struct dev_pagemap pgmap
= { };
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
;
23 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
25 ndns
= nvdimm_namespace_common_probe(dev
);
27 return ERR_CAST(ndns
);
29 /* parse the 'pfn' info block via ->rw_bytes */
30 rc
= devm_namespace_enable(dev
, ndns
, nd_info_block_reserve());
33 rc
= nvdimm_setup_pfn(nd_pfn
, &pgmap
);
36 devm_namespace_disable(dev
, ndns
);
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
);
48 rc
= sscanf(dev_name(&ndns
->dev
), "namespace%d.%d", ®ion_id
, &id
);
50 return ERR_PTR(-EINVAL
);
52 /* adjust the dax_region range to the start of data */
54 range
.start
+= offset
;
55 dax_region
= alloc_dax_region(dev
, region_id
, &range
,
56 nd_region
->target_node
, le32_to_cpu(pfn_sb
->align
),
57 IORESOURCE_DAX_STATIC
);
59 return ERR_PTR(-ENOMEM
);
61 data
= (struct dev_dax_data
) {
62 .dax_region
= dax_region
,
65 .size
= range_len(&range
),
66 .memmap_on_memory
= false,
69 return devm_create_dev_dax(&data
);
72 static int dax_pmem_probe(struct device
*dev
)
74 return PTR_ERR_OR_ZERO(__dax_pmem_probe(dev
));
77 static struct nd_device_driver dax_pmem_driver
= {
78 .probe
= dax_pmem_probe
,
82 .type
= ND_DRIVER_DAX_PMEM
,
85 static int __init
dax_pmem_init(void)
87 return nd_driver_register(&dax_pmem_driver
);
89 module_init(dax_pmem_init
);
91 static void __exit
dax_pmem_exit(void)
93 driver_unregister(&dax_pmem_driver
.drv
);
95 module_exit(dax_pmem_exit
);
97 MODULE_DESCRIPTION("PMEM DAX: direct access to persistent memory");
98 MODULE_LICENSE("GPL v2");
99 MODULE_AUTHOR("Intel Corporation");
100 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM
);