]>
Commit | Line | Data |
---|---|---|
9e853f23 RZ |
1 | /* |
2 | * Persistent Memory Driver | |
3 | * | |
9f53f9fa | 4 | * Copyright (c) 2014-2015, Intel Corporation. |
9e853f23 RZ |
5 | * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>. |
6 | * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms and conditions of the GNU General Public License, | |
10 | * version 2, as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope it will be useful, but WITHOUT | |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 | * more details. | |
16 | */ | |
17 | ||
18 | #include <asm/cacheflush.h> | |
19 | #include <linux/blkdev.h> | |
20 | #include <linux/hdreg.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/platform_device.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/moduleparam.h> | |
25 | #include <linux/slab.h> | |
61031952 | 26 | #include <linux/pmem.h> |
9f53f9fa DW |
27 | #include <linux/nd.h> |
28 | #include "nd.h" | |
9e853f23 RZ |
29 | |
30 | struct pmem_device { | |
31 | struct request_queue *pmem_queue; | |
32 | struct gendisk *pmem_disk; | |
33 | ||
34 | /* One contiguous memory region per device */ | |
35 | phys_addr_t phys_addr; | |
61031952 | 36 | void __pmem *virt_addr; |
9e853f23 RZ |
37 | size_t size; |
38 | }; | |
39 | ||
40 | static int pmem_major; | |
9e853f23 RZ |
41 | |
42 | static void pmem_do_bvec(struct pmem_device *pmem, struct page *page, | |
43 | unsigned int len, unsigned int off, int rw, | |
44 | sector_t sector) | |
45 | { | |
46 | void *mem = kmap_atomic(page); | |
47 | size_t pmem_off = sector << 9; | |
61031952 | 48 | void __pmem *pmem_addr = pmem->virt_addr + pmem_off; |
9e853f23 RZ |
49 | |
50 | if (rw == READ) { | |
61031952 | 51 | memcpy_from_pmem(mem + off, pmem_addr, len); |
9e853f23 RZ |
52 | flush_dcache_page(page); |
53 | } else { | |
54 | flush_dcache_page(page); | |
61031952 | 55 | memcpy_to_pmem(pmem_addr, mem + off, len); |
9e853f23 RZ |
56 | } |
57 | ||
58 | kunmap_atomic(mem); | |
59 | } | |
60 | ||
61 | static void pmem_make_request(struct request_queue *q, struct bio *bio) | |
62 | { | |
f0dc089c DW |
63 | bool do_acct; |
64 | unsigned long start; | |
9e853f23 | 65 | struct bio_vec bvec; |
9e853f23 | 66 | struct bvec_iter iter; |
edc870e5 DW |
67 | struct block_device *bdev = bio->bi_bdev; |
68 | struct pmem_device *pmem = bdev->bd_disk->private_data; | |
9e853f23 | 69 | |
f0dc089c | 70 | do_acct = nd_iostat_start(bio, &start); |
edc870e5 | 71 | bio_for_each_segment(bvec, bio, iter) |
9e853f23 | 72 | pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset, |
edc870e5 | 73 | bio_data_dir(bio), iter.bi_sector); |
f0dc089c DW |
74 | if (do_acct) |
75 | nd_iostat_end(bio, start); | |
61031952 RZ |
76 | |
77 | if (bio_data_dir(bio)) | |
78 | wmb_pmem(); | |
79 | ||
edc870e5 | 80 | bio_endio(bio, 0); |
9e853f23 RZ |
81 | } |
82 | ||
83 | static int pmem_rw_page(struct block_device *bdev, sector_t sector, | |
84 | struct page *page, int rw) | |
85 | { | |
86 | struct pmem_device *pmem = bdev->bd_disk->private_data; | |
87 | ||
88 | pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector); | |
89 | page_endio(page, rw & WRITE, 0); | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
94 | static long pmem_direct_access(struct block_device *bdev, sector_t sector, | |
95 | void **kaddr, unsigned long *pfn, long size) | |
96 | { | |
97 | struct pmem_device *pmem = bdev->bd_disk->private_data; | |
98 | size_t offset = sector << 9; | |
99 | ||
100 | if (!pmem) | |
101 | return -ENODEV; | |
102 | ||
61031952 RZ |
103 | /* FIXME convert DAX to comprehend that this mapping has a lifetime */ |
104 | *kaddr = (void __force *) pmem->virt_addr + offset; | |
9e853f23 RZ |
105 | *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT; |
106 | ||
107 | return pmem->size - offset; | |
108 | } | |
109 | ||
110 | static const struct block_device_operations pmem_fops = { | |
111 | .owner = THIS_MODULE, | |
112 | .rw_page = pmem_rw_page, | |
113 | .direct_access = pmem_direct_access, | |
58138820 | 114 | .revalidate_disk = nvdimm_revalidate_disk, |
9e853f23 RZ |
115 | }; |
116 | ||
9f53f9fa DW |
117 | static struct pmem_device *pmem_alloc(struct device *dev, |
118 | struct resource *res, int id) | |
9e853f23 RZ |
119 | { |
120 | struct pmem_device *pmem; | |
9e853f23 | 121 | |
708ab62b | 122 | pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL); |
9e853f23 | 123 | if (!pmem) |
8c2f7e86 | 124 | return ERR_PTR(-ENOMEM); |
9e853f23 RZ |
125 | |
126 | pmem->phys_addr = res->start; | |
127 | pmem->size = resource_size(res); | |
61031952 RZ |
128 | if (!arch_has_pmem_api()) |
129 | dev_warn(dev, "unable to guarantee persistence of writes\n"); | |
9e853f23 | 130 | |
708ab62b CH |
131 | if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size, |
132 | dev_name(dev))) { | |
9f53f9fa DW |
133 | dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n", |
134 | &pmem->phys_addr, pmem->size); | |
8c2f7e86 | 135 | return ERR_PTR(-EBUSY); |
9e853f23 RZ |
136 | } |
137 | ||
708ab62b CH |
138 | pmem->virt_addr = memremap_pmem(dev, pmem->phys_addr, pmem->size); |
139 | if (!pmem->virt_addr) | |
8c2f7e86 | 140 | return ERR_PTR(-ENXIO); |
8c2f7e86 DW |
141 | |
142 | return pmem; | |
143 | } | |
144 | ||
145 | static void pmem_detach_disk(struct pmem_device *pmem) | |
146 | { | |
147 | del_gendisk(pmem->pmem_disk); | |
148 | put_disk(pmem->pmem_disk); | |
149 | blk_cleanup_queue(pmem->pmem_queue); | |
150 | } | |
151 | ||
152 | static int pmem_attach_disk(struct nd_namespace_common *ndns, | |
153 | struct pmem_device *pmem) | |
154 | { | |
8c2f7e86 | 155 | struct gendisk *disk; |
9e853f23 RZ |
156 | |
157 | pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL); | |
158 | if (!pmem->pmem_queue) | |
8c2f7e86 | 159 | return -ENOMEM; |
9e853f23 RZ |
160 | |
161 | blk_queue_make_request(pmem->pmem_queue, pmem_make_request); | |
6b47496a | 162 | blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE); |
43d3fa3a | 163 | blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX); |
9e853f23 | 164 | blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY); |
0f51c4fa | 165 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue); |
9e853f23 | 166 | |
9f53f9fa | 167 | disk = alloc_disk(0); |
8c2f7e86 DW |
168 | if (!disk) { |
169 | blk_cleanup_queue(pmem->pmem_queue); | |
170 | return -ENOMEM; | |
171 | } | |
9e853f23 | 172 | |
9e853f23 | 173 | disk->major = pmem_major; |
9f53f9fa | 174 | disk->first_minor = 0; |
9e853f23 RZ |
175 | disk->fops = &pmem_fops; |
176 | disk->private_data = pmem; | |
177 | disk->queue = pmem->pmem_queue; | |
178 | disk->flags = GENHD_FL_EXT_DEVT; | |
5212e11f | 179 | nvdimm_namespace_disk_name(ndns, disk->disk_name); |
8c2f7e86 | 180 | disk->driverfs_dev = &ndns->dev; |
9e853f23 RZ |
181 | set_capacity(disk, pmem->size >> 9); |
182 | pmem->pmem_disk = disk; | |
183 | ||
184 | add_disk(disk); | |
58138820 | 185 | revalidate_disk(disk); |
9e853f23 | 186 | |
8c2f7e86 DW |
187 | return 0; |
188 | } | |
9e853f23 | 189 | |
8c2f7e86 DW |
190 | static int pmem_rw_bytes(struct nd_namespace_common *ndns, |
191 | resource_size_t offset, void *buf, size_t size, int rw) | |
192 | { | |
193 | struct pmem_device *pmem = dev_get_drvdata(ndns->claim); | |
194 | ||
195 | if (unlikely(offset + size > pmem->size)) { | |
196 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); | |
197 | return -EFAULT; | |
198 | } | |
199 | ||
200 | if (rw == READ) | |
61031952 RZ |
201 | memcpy_from_pmem(buf, pmem->virt_addr + offset, size); |
202 | else { | |
203 | memcpy_to_pmem(pmem->virt_addr + offset, buf, size); | |
204 | wmb_pmem(); | |
205 | } | |
8c2f7e86 DW |
206 | |
207 | return 0; | |
208 | } | |
209 | ||
9f53f9fa | 210 | static int nd_pmem_probe(struct device *dev) |
9e853f23 | 211 | { |
9f53f9fa | 212 | struct nd_region *nd_region = to_nd_region(dev->parent); |
8c2f7e86 DW |
213 | struct nd_namespace_common *ndns; |
214 | struct nd_namespace_io *nsio; | |
9e853f23 | 215 | struct pmem_device *pmem; |
9e853f23 | 216 | |
8c2f7e86 DW |
217 | ndns = nvdimm_namespace_common_probe(dev); |
218 | if (IS_ERR(ndns)) | |
219 | return PTR_ERR(ndns); | |
bf9bccc1 | 220 | |
8c2f7e86 | 221 | nsio = to_nd_namespace_io(&ndns->dev); |
9f53f9fa | 222 | pmem = pmem_alloc(dev, &nsio->res, nd_region->id); |
9e853f23 RZ |
223 | if (IS_ERR(pmem)) |
224 | return PTR_ERR(pmem); | |
225 | ||
9f53f9fa | 226 | dev_set_drvdata(dev, pmem); |
8c2f7e86 | 227 | ndns->rw_bytes = pmem_rw_bytes; |
708ab62b | 228 | |
8c2f7e86 | 229 | if (is_nd_btt(dev)) |
708ab62b CH |
230 | return nvdimm_namespace_attach_btt(ndns); |
231 | ||
232 | if (nd_btt_probe(ndns, pmem) == 0) | |
8c2f7e86 | 233 | /* we'll come back as btt-pmem */ |
708ab62b CH |
234 | return -ENXIO; |
235 | return pmem_attach_disk(ndns, pmem); | |
9e853f23 RZ |
236 | } |
237 | ||
9f53f9fa | 238 | static int nd_pmem_remove(struct device *dev) |
9e853f23 | 239 | { |
9f53f9fa | 240 | struct pmem_device *pmem = dev_get_drvdata(dev); |
9e853f23 | 241 | |
8c2f7e86 DW |
242 | if (is_nd_btt(dev)) |
243 | nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns); | |
244 | else | |
245 | pmem_detach_disk(pmem); | |
8c2f7e86 | 246 | |
9e853f23 RZ |
247 | return 0; |
248 | } | |
249 | ||
9f53f9fa DW |
250 | MODULE_ALIAS("pmem"); |
251 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); | |
bf9bccc1 | 252 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); |
9f53f9fa DW |
253 | static struct nd_device_driver nd_pmem_driver = { |
254 | .probe = nd_pmem_probe, | |
255 | .remove = nd_pmem_remove, | |
256 | .drv = { | |
257 | .name = "nd_pmem", | |
9e853f23 | 258 | }, |
bf9bccc1 | 259 | .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, |
9e853f23 RZ |
260 | }; |
261 | ||
262 | static int __init pmem_init(void) | |
263 | { | |
264 | int error; | |
265 | ||
266 | pmem_major = register_blkdev(0, "pmem"); | |
267 | if (pmem_major < 0) | |
268 | return pmem_major; | |
269 | ||
9f53f9fa DW |
270 | error = nd_driver_register(&nd_pmem_driver); |
271 | if (error) { | |
9e853f23 | 272 | unregister_blkdev(pmem_major, "pmem"); |
9f53f9fa DW |
273 | return error; |
274 | } | |
275 | ||
276 | return 0; | |
9e853f23 RZ |
277 | } |
278 | module_init(pmem_init); | |
279 | ||
280 | static void pmem_exit(void) | |
281 | { | |
9f53f9fa | 282 | driver_unregister(&nd_pmem_driver.drv); |
9e853f23 RZ |
283 | unregister_blkdev(pmem_major, "pmem"); |
284 | } | |
285 | module_exit(pmem_exit); | |
286 | ||
287 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); | |
288 | MODULE_LICENSE("GPL v2"); |