blob: 31b6ecce4c64205f1109532f222b39b980e75a00 [file] [log] [blame]
Dan Williamsab68f262016-05-18 09:15:08 -07001/*
2 * Copyright(c) 2016 Intel Corporation. All rights reserved.
3 *
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.
7 *
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.
12 */
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"
Dan Williams7b6be842017-04-11 09:49:49 -070019#include "device-dax.h"
Dan Williamsab68f262016-05-18 09:15:08 -070020
21struct dax_pmem {
22 struct device *dev;
23 struct percpu_ref ref;
Christoph Hellwige8d51342017-12-29 08:54:05 +010024 struct dev_pagemap pgmap;
Dan Williamsab68f262016-05-18 09:15:08 -070025 struct completion cmp;
26};
27
Dan Williamsccdb07f2016-08-06 16:05:06 -070028static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
Dan Williamsab68f262016-05-18 09:15:08 -070029{
30 return container_of(ref, struct dax_pmem, ref);
31}
32
33static void dax_pmem_percpu_release(struct percpu_ref *ref)
34{
35 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
36
37 dev_dbg(dax_pmem->dev, "%s\n", __func__);
38 complete(&dax_pmem->cmp);
39}
40
41static void dax_pmem_percpu_exit(void *data)
42{
43 struct percpu_ref *ref = data;
44 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
45
46 dev_dbg(dax_pmem->dev, "%s\n", __func__);
Dan Williams71389702017-04-28 10:23:37 -070047 wait_for_completion(&dax_pmem->cmp);
Dan Williamsab68f262016-05-18 09:15:08 -070048 percpu_ref_exit(ref);
Dan Williamsab68f262016-05-18 09:15:08 -070049}
50
51static void dax_pmem_percpu_kill(void *data)
52{
53 struct percpu_ref *ref = data;
54 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
55
56 dev_dbg(dax_pmem->dev, "%s\n", __func__);
57 percpu_ref_kill(ref);
58}
59
60static int dax_pmem_probe(struct device *dev)
61{
Dan Williamsab68f262016-05-18 09:15:08 -070062 void *addr;
63 struct resource res;
Dan Williamsbbb3be12017-07-18 17:49:14 -070064 int rc, id, region_id;
Dan Williamsab68f262016-05-18 09:15:08 -070065 struct nd_pfn_sb *pfn_sb;
Dan Williams5f0694b2017-01-30 21:43:10 -080066 struct dev_dax *dev_dax;
Dan Williamsab68f262016-05-18 09:15:08 -070067 struct dax_pmem *dax_pmem;
Dan Williamsab68f262016-05-18 09:15:08 -070068 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;
Dan Williamsab68f262016-05-18 09:15:08 -070073
74 ndns = nvdimm_namespace_common_probe(dev);
75 if (IS_ERR(ndns))
76 return PTR_ERR(ndns);
77 nsio = to_nd_namespace_io(&ndns->dev);
78
Christoph Hellwige8d51342017-12-29 08:54:05 +010079 dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
80 if (!dax_pmem)
81 return -ENOMEM;
82
Dan Williamsab68f262016-05-18 09:15:08 -070083 /* parse the 'pfn' info block via ->rw_bytes */
Dan Williams6a84fb42016-10-28 14:34:51 -070084 rc = devm_nsio_enable(dev, nsio);
85 if (rc)
86 return rc;
Christoph Hellwige8d51342017-12-29 08:54:05 +010087 rc = nvdimm_setup_pfn(nd_pfn, &dax_pmem->pgmap);
88 if (rc)
89 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -070090 devm_nsio_disable(dev, nsio);
91
92 pfn_sb = nd_pfn->pfn_sb;
93
94 if (!devm_request_mem_region(dev, nsio->res.start,
Dan Williams450c6632016-11-28 11:15:18 -080095 resource_size(&nsio->res),
96 dev_name(&ndns->dev))) {
Dan Williamsab68f262016-05-18 09:15:08 -070097 dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
98 return -EBUSY;
99 }
100
Dan Williamsab68f262016-05-18 09:15:08 -0700101 dax_pmem->dev = dev;
102 init_completion(&dax_pmem->cmp);
103 rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
104 GFP_KERNEL);
105 if (rc)
106 return rc;
107
Sajjan, Vikas Cd1c8e0c2016-07-05 11:20:07 +0530108 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
109 &dax_pmem->ref);
110 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700111 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700112
Christoph Hellwige8d51342017-12-29 08:54:05 +0100113 dax_pmem->pgmap.ref = &dax_pmem->ref;
114 addr = devm_memremap_pages(dev, &dax_pmem->pgmap);
Dan Williamsab68f262016-05-18 09:15:08 -0700115 if (IS_ERR(addr))
116 return PTR_ERR(addr);
117
Sajjan, Vikas Cd1c8e0c2016-07-05 11:20:07 +0530118 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
119 &dax_pmem->ref);
120 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700121 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700122
Dan Williamsd0e58452016-08-25 15:17:14 -0700123 /* adjust the dax_region resource to the start of data */
Christoph Hellwige8d51342017-12-29 08:54:05 +0100124 memcpy(&res, &dax_pmem->pgmap.res, sizeof(res));
Dan Williamsd0e58452016-08-25 15:17:14 -0700125 res.start += le64_to_cpu(pfn_sb->dataoff);
126
Dan Williamsbbb3be12017-07-18 17:49:14 -0700127 rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
128 if (rc != 2)
129 return -EINVAL;
130
131 dax_region = alloc_dax_region(dev, region_id, &res,
Dan Williamsab68f262016-05-18 09:15:08 -0700132 le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
133 if (!dax_region)
134 return -ENOMEM;
135
136 /* TODO: support for subdividing a dax region... */
Dan Williamsbbb3be12017-07-18 17:49:14 -0700137 dev_dax = devm_create_dev_dax(dax_region, id, &res, 1);
Dan Williamsab68f262016-05-18 09:15:08 -0700138
Dan Williams5f0694b2017-01-30 21:43:10 -0800139 /* child dev_dax instances now own the lifetime of the dax_region */
Dan Williamsab68f262016-05-18 09:15:08 -0700140 dax_region_put(dax_region);
141
Dan Williams5f0694b2017-01-30 21:43:10 -0800142 return PTR_ERR_OR_ZERO(dev_dax);
Dan Williamsab68f262016-05-18 09:15:08 -0700143}
144
145static struct nd_device_driver dax_pmem_driver = {
146 .probe = dax_pmem_probe,
147 .drv = {
148 .name = "dax_pmem",
149 },
150 .type = ND_DRIVER_DAX_PMEM,
151};
152
153static int __init dax_pmem_init(void)
154{
155 return nd_driver_register(&dax_pmem_driver);
156}
157module_init(dax_pmem_init);
158
159static void __exit dax_pmem_exit(void)
160{
161 driver_unregister(&dax_pmem_driver.drv);
162}
163module_exit(dax_pmem_exit);
164
165MODULE_LICENSE("GPL v2");
166MODULE_AUTHOR("Intel Corporation");
167MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);