blob: a9709db0704c38d4d22cd30ac31c921f9bea644c [file] [log] [blame]
Ross Zwisler9e853f22015-04-01 09:12:19 +02001/*
2 * Persistent Memory Driver
3 *
Dan Williams9f53f9f2015-06-09 15:33:45 -04004 * Copyright (c) 2014-2015, Intel Corporation.
Ross Zwisler9e853f22015-04-01 09:12:19 +02005 * 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>
Dan Williams9f53f9f2015-06-09 15:33:45 -040026#include <linux/nd.h>
27#include "nd.h"
Ross Zwisler9e853f22015-04-01 09:12:19 +020028
29struct 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
39static int pmem_major;
Ross Zwisler9e853f22015-04-01 09:12:19 +020040
41static 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
59static void pmem_make_request(struct request_queue *q, struct bio *bio)
60{
Dan Williamsf0dc0892015-05-16 12:28:53 -040061 bool do_acct;
62 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -040063 struct bio_vec bvec;
64 struct bvec_iter iter;
Ross Zwisler9e853f22015-04-01 09:12:19 +020065 struct block_device *bdev = bio->bi_bdev;
66 struct pmem_device *pmem = bdev->bd_disk->private_data;
Ross Zwisler9e853f22015-04-01 09:12:19 +020067
Dan Williamsf0dc0892015-05-16 12:28:53 -040068 do_acct = nd_iostat_start(bio, &start);
Dan Williamsedc870e2015-05-16 12:28:51 -040069 bio_for_each_segment(bvec, bio, iter)
Ross Zwisler9e853f22015-04-01 09:12:19 +020070 pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset,
Dan Williamsedc870e2015-05-16 12:28:51 -040071 bio_data_dir(bio), iter.bi_sector);
Dan Williamsf0dc0892015-05-16 12:28:53 -040072 if (do_acct)
73 nd_iostat_end(bio, start);
Dan Williamsedc870e2015-05-16 12:28:51 -040074 bio_endio(bio, 0);
Ross Zwisler9e853f22015-04-01 09:12:19 +020075}
76
77static int pmem_rw_page(struct block_device *bdev, sector_t sector,
78 struct page *page, int rw)
79{
80 struct pmem_device *pmem = bdev->bd_disk->private_data;
81
82 pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector);
83 page_endio(page, rw & WRITE, 0);
84
85 return 0;
86}
87
88static long pmem_direct_access(struct block_device *bdev, sector_t sector,
89 void **kaddr, unsigned long *pfn, long size)
90{
91 struct pmem_device *pmem = bdev->bd_disk->private_data;
92 size_t offset = sector << 9;
93
94 if (!pmem)
95 return -ENODEV;
96
97 *kaddr = pmem->virt_addr + offset;
98 *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT;
99
100 return pmem->size - offset;
101}
102
103static const struct block_device_operations pmem_fops = {
104 .owner = THIS_MODULE,
105 .rw_page = pmem_rw_page,
106 .direct_access = pmem_direct_access,
107};
108
Dan Williams9f53f9f2015-06-09 15:33:45 -0400109static struct pmem_device *pmem_alloc(struct device *dev,
110 struct resource *res, int id)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200111{
112 struct pmem_device *pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200113
Ross Zwisler9e853f22015-04-01 09:12:19 +0200114 pmem = kzalloc(sizeof(*pmem), GFP_KERNEL);
115 if (!pmem)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400116 return ERR_PTR(-ENOMEM);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200117
118 pmem->phys_addr = res->start;
119 pmem->size = resource_size(res);
120
Dan Williams8c2f7e82015-06-25 04:20:04 -0400121 if (!request_mem_region(pmem->phys_addr, pmem->size, dev_name(dev))) {
Dan Williams9f53f9f2015-06-09 15:33:45 -0400122 dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
123 &pmem->phys_addr, pmem->size);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400124 kfree(pmem);
125 return ERR_PTR(-EBUSY);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200126 }
127
128 /*
129 * Map the memory as non-cachable, as we can't write back the contents
130 * of the CPU caches in case of a crash.
131 */
Ross Zwisler9e853f22015-04-01 09:12:19 +0200132 pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem->size);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400133 if (!pmem->virt_addr) {
134 release_mem_region(pmem->phys_addr, pmem->size);
135 kfree(pmem);
136 return ERR_PTR(-ENXIO);
137 }
138
139 return pmem;
140}
141
142static void pmem_detach_disk(struct pmem_device *pmem)
143{
144 del_gendisk(pmem->pmem_disk);
145 put_disk(pmem->pmem_disk);
146 blk_cleanup_queue(pmem->pmem_queue);
147}
148
149static int pmem_attach_disk(struct nd_namespace_common *ndns,
150 struct pmem_device *pmem)
151{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400152 struct gendisk *disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200153
154 pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL);
155 if (!pmem->pmem_queue)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400156 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200157
158 blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
Dan Williams43d3fa32015-05-16 12:28:50 -0400159 blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200160 blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
Dan Williams0f51c4f2015-05-16 12:28:54 -0400161 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200162
Dan Williams9f53f9f2015-06-09 15:33:45 -0400163 disk = alloc_disk(0);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400164 if (!disk) {
165 blk_cleanup_queue(pmem->pmem_queue);
166 return -ENOMEM;
167 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200168
Ross Zwisler9e853f22015-04-01 09:12:19 +0200169 disk->major = pmem_major;
Dan Williams9f53f9f2015-06-09 15:33:45 -0400170 disk->first_minor = 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200171 disk->fops = &pmem_fops;
172 disk->private_data = pmem;
173 disk->queue = pmem->pmem_queue;
174 disk->flags = GENHD_FL_EXT_DEVT;
Vishal Verma5212e112015-06-25 04:20:32 -0400175 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400176 disk->driverfs_dev = &ndns->dev;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200177 set_capacity(disk, pmem->size >> 9);
178 pmem->pmem_disk = disk;
179
180 add_disk(disk);
181
Dan Williams8c2f7e82015-06-25 04:20:04 -0400182 return 0;
183}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200184
Dan Williams8c2f7e82015-06-25 04:20:04 -0400185static int pmem_rw_bytes(struct nd_namespace_common *ndns,
186 resource_size_t offset, void *buf, size_t size, int rw)
187{
188 struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
189
190 if (unlikely(offset + size > pmem->size)) {
191 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
192 return -EFAULT;
193 }
194
195 if (rw == READ)
196 memcpy(buf, pmem->virt_addr + offset, size);
197 else
198 memcpy(pmem->virt_addr + offset, buf, size);
199
200 return 0;
201}
202
Ross Zwisler9e853f22015-04-01 09:12:19 +0200203static void pmem_free(struct pmem_device *pmem)
204{
Ross Zwisler9e853f22015-04-01 09:12:19 +0200205 iounmap(pmem->virt_addr);
206 release_mem_region(pmem->phys_addr, pmem->size);
207 kfree(pmem);
208}
209
Dan Williams9f53f9f2015-06-09 15:33:45 -0400210static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200211{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400212 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400213 struct nd_namespace_common *ndns;
214 struct nd_namespace_io *nsio;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200215 struct pmem_device *pmem;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400216 int rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200217
Dan Williams8c2f7e82015-06-25 04:20:04 -0400218 ndns = nvdimm_namespace_common_probe(dev);
219 if (IS_ERR(ndns))
220 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400221
Dan Williams8c2f7e82015-06-25 04:20:04 -0400222 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400223 pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200224 if (IS_ERR(pmem))
225 return PTR_ERR(pmem);
226
Dan Williams9f53f9f2015-06-09 15:33:45 -0400227 dev_set_drvdata(dev, pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400228 ndns->rw_bytes = pmem_rw_bytes;
229 if (is_nd_btt(dev))
230 rc = nvdimm_namespace_attach_btt(ndns);
231 else if (nd_btt_probe(ndns, pmem) == 0) {
232 /* we'll come back as btt-pmem */
233 rc = -ENXIO;
234 } else
235 rc = pmem_attach_disk(ndns, pmem);
236 if (rc)
237 pmem_free(pmem);
238 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200239}
240
Dan Williams9f53f9f2015-06-09 15:33:45 -0400241static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200242{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400243 struct pmem_device *pmem = dev_get_drvdata(dev);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200244
Dan Williams8c2f7e82015-06-25 04:20:04 -0400245 if (is_nd_btt(dev))
246 nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns);
247 else
248 pmem_detach_disk(pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200249 pmem_free(pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400250
Ross Zwisler9e853f22015-04-01 09:12:19 +0200251 return 0;
252}
253
Dan Williams9f53f9f2015-06-09 15:33:45 -0400254MODULE_ALIAS("pmem");
255MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400256MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400257static struct nd_device_driver nd_pmem_driver = {
258 .probe = nd_pmem_probe,
259 .remove = nd_pmem_remove,
260 .drv = {
261 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200262 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400263 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200264};
265
266static int __init pmem_init(void)
267{
268 int error;
269
270 pmem_major = register_blkdev(0, "pmem");
271 if (pmem_major < 0)
272 return pmem_major;
273
Dan Williams9f53f9f2015-06-09 15:33:45 -0400274 error = nd_driver_register(&nd_pmem_driver);
275 if (error) {
Ross Zwisler9e853f22015-04-01 09:12:19 +0200276 unregister_blkdev(pmem_major, "pmem");
Dan Williams9f53f9f2015-06-09 15:33:45 -0400277 return error;
278 }
279
280 return 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200281}
282module_init(pmem_init);
283
284static void pmem_exit(void)
285{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400286 driver_unregister(&nd_pmem_driver.drv);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200287 unregister_blkdev(pmem_major, "pmem");
288}
289module_exit(pmem_exit);
290
291MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
292MODULE_LICENSE("GPL v2");