]>
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> | |
9f53f9fa DW |
26 | #include <linux/nd.h> |
27 | #include "nd.h" | |
9e853f23 RZ |
28 | |
29 | struct pmem_device { | |
30 | struct request_queue *pmem_queue; | |
31 | struct gendisk *pmem_disk; | |
32 | ||
33 | /* One contiguous memory region per device */ | |
34 | phys_addr_t phys_addr; | |
35 | void *virt_addr; | |
36 | size_t size; | |
37 | }; | |
38 | ||
39 | static int pmem_major; | |
9e853f23 RZ |
40 | |
41 | static void pmem_do_bvec(struct pmem_device *pmem, struct page *page, | |
42 | unsigned int len, unsigned int off, int rw, | |
43 | sector_t sector) | |
44 | { | |
45 | void *mem = kmap_atomic(page); | |
46 | size_t pmem_off = sector << 9; | |
47 | ||
48 | if (rw == READ) { | |
49 | memcpy(mem + off, pmem->virt_addr + pmem_off, len); | |
50 | flush_dcache_page(page); | |
51 | } else { | |
52 | flush_dcache_page(page); | |
53 | memcpy(pmem->virt_addr + pmem_off, mem + off, len); | |
54 | } | |
55 | ||
56 | kunmap_atomic(mem); | |
57 | } | |
58 | ||
59 | static void pmem_make_request(struct request_queue *q, struct bio *bio) | |
60 | { | |
61 | struct block_device *bdev = bio->bi_bdev; | |
62 | struct pmem_device *pmem = bdev->bd_disk->private_data; | |
63 | int rw; | |
64 | struct bio_vec bvec; | |
65 | sector_t sector; | |
66 | struct bvec_iter iter; | |
67 | int err = 0; | |
68 | ||
69 | if (bio_end_sector(bio) > get_capacity(bdev->bd_disk)) { | |
70 | err = -EIO; | |
71 | goto out; | |
72 | } | |
73 | ||
74 | BUG_ON(bio->bi_rw & REQ_DISCARD); | |
75 | ||
76 | rw = bio_data_dir(bio); | |
77 | sector = bio->bi_iter.bi_sector; | |
78 | bio_for_each_segment(bvec, bio, iter) { | |
79 | pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset, | |
80 | rw, sector); | |
81 | sector += bvec.bv_len >> 9; | |
82 | } | |
83 | ||
84 | out: | |
85 | bio_endio(bio, err); | |
86 | } | |
87 | ||
88 | static int pmem_rw_page(struct block_device *bdev, sector_t sector, | |
89 | struct page *page, int rw) | |
90 | { | |
91 | struct pmem_device *pmem = bdev->bd_disk->private_data; | |
92 | ||
93 | pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector); | |
94 | page_endio(page, rw & WRITE, 0); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static long pmem_direct_access(struct block_device *bdev, sector_t sector, | |
100 | void **kaddr, unsigned long *pfn, long size) | |
101 | { | |
102 | struct pmem_device *pmem = bdev->bd_disk->private_data; | |
103 | size_t offset = sector << 9; | |
104 | ||
105 | if (!pmem) | |
106 | return -ENODEV; | |
107 | ||
108 | *kaddr = pmem->virt_addr + offset; | |
109 | *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT; | |
110 | ||
111 | return pmem->size - offset; | |
112 | } | |
113 | ||
114 | static const struct block_device_operations pmem_fops = { | |
115 | .owner = THIS_MODULE, | |
116 | .rw_page = pmem_rw_page, | |
117 | .direct_access = pmem_direct_access, | |
118 | }; | |
119 | ||
9f53f9fa DW |
120 | static struct pmem_device *pmem_alloc(struct device *dev, |
121 | struct resource *res, int id) | |
9e853f23 RZ |
122 | { |
123 | struct pmem_device *pmem; | |
9e853f23 | 124 | |
9e853f23 RZ |
125 | pmem = kzalloc(sizeof(*pmem), GFP_KERNEL); |
126 | if (!pmem) | |
8c2f7e86 | 127 | return ERR_PTR(-ENOMEM); |
9e853f23 RZ |
128 | |
129 | pmem->phys_addr = res->start; | |
130 | pmem->size = resource_size(res); | |
131 | ||
8c2f7e86 | 132 | if (!request_mem_region(pmem->phys_addr, pmem->size, 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 DW |
135 | kfree(pmem); |
136 | return ERR_PTR(-EBUSY); | |
9e853f23 RZ |
137 | } |
138 | ||
139 | /* | |
140 | * Map the memory as non-cachable, as we can't write back the contents | |
141 | * of the CPU caches in case of a crash. | |
142 | */ | |
9e853f23 | 143 | pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem->size); |
8c2f7e86 DW |
144 | if (!pmem->virt_addr) { |
145 | release_mem_region(pmem->phys_addr, pmem->size); | |
146 | kfree(pmem); | |
147 | return ERR_PTR(-ENXIO); | |
148 | } | |
149 | ||
150 | return pmem; | |
151 | } | |
152 | ||
153 | static void pmem_detach_disk(struct pmem_device *pmem) | |
154 | { | |
155 | del_gendisk(pmem->pmem_disk); | |
156 | put_disk(pmem->pmem_disk); | |
157 | blk_cleanup_queue(pmem->pmem_queue); | |
158 | } | |
159 | ||
160 | static int pmem_attach_disk(struct nd_namespace_common *ndns, | |
161 | struct pmem_device *pmem) | |
162 | { | |
163 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); | |
164 | struct gendisk *disk; | |
9e853f23 RZ |
165 | |
166 | pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL); | |
167 | if (!pmem->pmem_queue) | |
8c2f7e86 | 168 | return -ENOMEM; |
9e853f23 RZ |
169 | |
170 | blk_queue_make_request(pmem->pmem_queue, pmem_make_request); | |
171 | blk_queue_max_hw_sectors(pmem->pmem_queue, 1024); | |
172 | blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY); | |
173 | ||
9f53f9fa | 174 | disk = alloc_disk(0); |
8c2f7e86 DW |
175 | if (!disk) { |
176 | blk_cleanup_queue(pmem->pmem_queue); | |
177 | return -ENOMEM; | |
178 | } | |
9e853f23 | 179 | |
9e853f23 | 180 | disk->major = pmem_major; |
9f53f9fa | 181 | disk->first_minor = 0; |
9e853f23 RZ |
182 | disk->fops = &pmem_fops; |
183 | disk->private_data = pmem; | |
184 | disk->queue = pmem->pmem_queue; | |
185 | disk->flags = GENHD_FL_EXT_DEVT; | |
8c2f7e86 DW |
186 | sprintf(disk->disk_name, "pmem%d", nd_region->id); |
187 | disk->driverfs_dev = &ndns->dev; | |
9e853f23 RZ |
188 | set_capacity(disk, pmem->size >> 9); |
189 | pmem->pmem_disk = disk; | |
190 | ||
191 | add_disk(disk); | |
192 | ||
8c2f7e86 DW |
193 | return 0; |
194 | } | |
9e853f23 | 195 | |
8c2f7e86 DW |
196 | static int pmem_rw_bytes(struct nd_namespace_common *ndns, |
197 | resource_size_t offset, void *buf, size_t size, int rw) | |
198 | { | |
199 | struct pmem_device *pmem = dev_get_drvdata(ndns->claim); | |
200 | ||
201 | if (unlikely(offset + size > pmem->size)) { | |
202 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); | |
203 | return -EFAULT; | |
204 | } | |
205 | ||
206 | if (rw == READ) | |
207 | memcpy(buf, pmem->virt_addr + offset, size); | |
208 | else | |
209 | memcpy(pmem->virt_addr + offset, buf, size); | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
214 | static int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns) | |
215 | { | |
216 | /* TODO */ | |
217 | return -ENXIO; | |
218 | } | |
219 | ||
220 | static void nvdimm_namespace_detach_btt(struct nd_namespace_common *ndns) | |
221 | { | |
222 | /* TODO */ | |
9e853f23 RZ |
223 | } |
224 | ||
225 | static void pmem_free(struct pmem_device *pmem) | |
226 | { | |
9e853f23 RZ |
227 | iounmap(pmem->virt_addr); |
228 | release_mem_region(pmem->phys_addr, pmem->size); | |
229 | kfree(pmem); | |
230 | } | |
231 | ||
9f53f9fa | 232 | static int nd_pmem_probe(struct device *dev) |
9e853f23 | 233 | { |
9f53f9fa | 234 | struct nd_region *nd_region = to_nd_region(dev->parent); |
8c2f7e86 DW |
235 | struct nd_namespace_common *ndns; |
236 | struct nd_namespace_io *nsio; | |
9e853f23 | 237 | struct pmem_device *pmem; |
8c2f7e86 | 238 | int rc; |
9e853f23 | 239 | |
8c2f7e86 DW |
240 | ndns = nvdimm_namespace_common_probe(dev); |
241 | if (IS_ERR(ndns)) | |
242 | return PTR_ERR(ndns); | |
bf9bccc1 | 243 | |
8c2f7e86 | 244 | nsio = to_nd_namespace_io(&ndns->dev); |
9f53f9fa | 245 | pmem = pmem_alloc(dev, &nsio->res, nd_region->id); |
9e853f23 RZ |
246 | if (IS_ERR(pmem)) |
247 | return PTR_ERR(pmem); | |
248 | ||
9f53f9fa | 249 | dev_set_drvdata(dev, pmem); |
8c2f7e86 DW |
250 | ndns->rw_bytes = pmem_rw_bytes; |
251 | if (is_nd_btt(dev)) | |
252 | rc = nvdimm_namespace_attach_btt(ndns); | |
253 | else if (nd_btt_probe(ndns, pmem) == 0) { | |
254 | /* we'll come back as btt-pmem */ | |
255 | rc = -ENXIO; | |
256 | } else | |
257 | rc = pmem_attach_disk(ndns, pmem); | |
258 | if (rc) | |
259 | pmem_free(pmem); | |
260 | return rc; | |
9e853f23 RZ |
261 | } |
262 | ||
9f53f9fa | 263 | static int nd_pmem_remove(struct device *dev) |
9e853f23 | 264 | { |
9f53f9fa | 265 | struct pmem_device *pmem = dev_get_drvdata(dev); |
9e853f23 | 266 | |
8c2f7e86 DW |
267 | if (is_nd_btt(dev)) |
268 | nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns); | |
269 | else | |
270 | pmem_detach_disk(pmem); | |
9e853f23 | 271 | pmem_free(pmem); |
8c2f7e86 | 272 | |
9e853f23 RZ |
273 | return 0; |
274 | } | |
275 | ||
9f53f9fa DW |
276 | MODULE_ALIAS("pmem"); |
277 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); | |
bf9bccc1 | 278 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); |
9f53f9fa DW |
279 | static struct nd_device_driver nd_pmem_driver = { |
280 | .probe = nd_pmem_probe, | |
281 | .remove = nd_pmem_remove, | |
282 | .drv = { | |
283 | .name = "nd_pmem", | |
9e853f23 | 284 | }, |
bf9bccc1 | 285 | .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, |
9e853f23 RZ |
286 | }; |
287 | ||
288 | static int __init pmem_init(void) | |
289 | { | |
290 | int error; | |
291 | ||
292 | pmem_major = register_blkdev(0, "pmem"); | |
293 | if (pmem_major < 0) | |
294 | return pmem_major; | |
295 | ||
9f53f9fa DW |
296 | error = nd_driver_register(&nd_pmem_driver); |
297 | if (error) { | |
9e853f23 | 298 | unregister_blkdev(pmem_major, "pmem"); |
9f53f9fa DW |
299 | return error; |
300 | } | |
301 | ||
302 | return 0; | |
9e853f23 RZ |
303 | } |
304 | module_init(pmem_init); | |
305 | ||
306 | static void pmem_exit(void) | |
307 | { | |
9f53f9fa | 308 | driver_unregister(&nd_pmem_driver.drv); |
9e853f23 RZ |
309 | unregister_blkdev(pmem_major, "pmem"); |
310 | } | |
311 | module_exit(pmem_exit); | |
312 | ||
313 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); | |
314 | MODULE_LICENSE("GPL v2"); |