2 * Copyright(c) 2016 Intel Corporation. All rights reserved.
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.
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.
13 #include <linux/percpu-refcount.h>
14 #include <linux/memremap.h>
15 #include <linux/module.h>
16 #include <linux/pfn_t.h>
17 #include "../nvdimm/pfn.h"
18 #include "../nvdimm/nd.h"
19 #include "device-dax.h"
23 struct percpu_ref ref
;
24 struct dev_pagemap pgmap
;
25 struct completion cmp
;
28 static struct dax_pmem
*to_dax_pmem(struct percpu_ref
*ref
)
30 return container_of(ref
, struct dax_pmem
, ref
);
33 static void dax_pmem_percpu_release(struct percpu_ref
*ref
)
35 struct dax_pmem
*dax_pmem
= to_dax_pmem(ref
);
37 dev_dbg(dax_pmem
->dev
, "%s\n", __func__
);
38 complete(&dax_pmem
->cmp
);
41 static void dax_pmem_percpu_exit(void *data
)
43 struct percpu_ref
*ref
= data
;
44 struct dax_pmem
*dax_pmem
= to_dax_pmem(ref
);
46 dev_dbg(dax_pmem
->dev
, "%s\n", __func__
);
47 wait_for_completion(&dax_pmem
->cmp
);
51 static void dax_pmem_percpu_kill(void *data
)
53 struct percpu_ref
*ref
= data
;
54 struct dax_pmem
*dax_pmem
= to_dax_pmem(ref
);
56 dev_dbg(dax_pmem
->dev
, "%s\n", __func__
);
60 static int dax_pmem_probe(struct device
*dev
)
64 int rc
, id
, region_id
;
65 struct nd_pfn_sb
*pfn_sb
;
66 struct dev_dax
*dev_dax
;
67 struct dax_pmem
*dax_pmem
;
68 struct nd_namespace_io
*nsio
;
69 struct dax_region
*dax_region
;
70 struct nd_namespace_common
*ndns
;
71 struct nd_dax
*nd_dax
= to_nd_dax(dev
);
72 struct nd_pfn
*nd_pfn
= &nd_dax
->nd_pfn
;
74 ndns
= nvdimm_namespace_common_probe(dev
);
77 nsio
= to_nd_namespace_io(&ndns
->dev
);
79 dax_pmem
= devm_kzalloc(dev
, sizeof(*dax_pmem
), GFP_KERNEL
);
83 /* parse the 'pfn' info block via ->rw_bytes */
84 rc
= devm_nsio_enable(dev
, nsio
);
87 rc
= nvdimm_setup_pfn(nd_pfn
, &dax_pmem
->pgmap
);
90 devm_nsio_disable(dev
, nsio
);
92 pfn_sb
= nd_pfn
->pfn_sb
;
94 if (!devm_request_mem_region(dev
, nsio
->res
.start
,
95 resource_size(&nsio
->res
),
96 dev_name(&ndns
->dev
))) {
97 dev_warn(dev
, "could not reserve region %pR\n", &nsio
->res
);
102 init_completion(&dax_pmem
->cmp
);
103 rc
= percpu_ref_init(&dax_pmem
->ref
, dax_pmem_percpu_release
, 0,
108 rc
= devm_add_action_or_reset(dev
, dax_pmem_percpu_exit
,
113 dax_pmem
->pgmap
.ref
= &dax_pmem
->ref
;
114 addr
= devm_memremap_pages(dev
, &dax_pmem
->pgmap
);
116 return PTR_ERR(addr
);
118 rc
= devm_add_action_or_reset(dev
, dax_pmem_percpu_kill
,
123 /* adjust the dax_region resource to the start of data */
124 memcpy(&res
, &dax_pmem
->pgmap
.res
, sizeof(res
));
125 res
.start
+= le64_to_cpu(pfn_sb
->dataoff
);
127 rc
= sscanf(dev_name(&ndns
->dev
), "namespace%d.%d", ®ion_id
, &id
);
131 dax_region
= alloc_dax_region(dev
, region_id
, &res
,
132 le32_to_cpu(pfn_sb
->align
), addr
, PFN_DEV
|PFN_MAP
);
136 /* TODO: support for subdividing a dax region... */
137 dev_dax
= devm_create_dev_dax(dax_region
, id
, &res
, 1);
139 /* child dev_dax instances now own the lifetime of the dax_region */
140 dax_region_put(dax_region
);
142 return PTR_ERR_OR_ZERO(dev_dax
);
145 static struct nd_device_driver dax_pmem_driver
= {
146 .probe
= dax_pmem_probe
,
150 .type
= ND_DRIVER_DAX_PMEM
,
153 static int __init
dax_pmem_init(void)
155 return nd_driver_register(&dax_pmem_driver
);
157 module_init(dax_pmem_init
);
159 static void __exit
dax_pmem_exit(void)
161 driver_unregister(&dax_pmem_driver
.drv
);
163 module_exit(dax_pmem_exit
);
165 MODULE_LICENSE("GPL v2");
166 MODULE_AUTHOR("Intel Corporation");
167 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM
);