blob: 09195e3b7453cdc32b9eecde4f190b268ae3d1d1 [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);
161
Dan Williams9f53f9f2015-06-09 15:33:45 -0400162 disk = alloc_disk(0);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400163 if (!disk) {
164 blk_cleanup_queue(pmem->pmem_queue);
165 return -ENOMEM;
166 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200167
Ross Zwisler9e853f22015-04-01 09:12:19 +0200168 disk->major = pmem_major;
Dan Williams9f53f9f2015-06-09 15:33:45 -0400169 disk->first_minor = 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200170 disk->fops = &pmem_fops;
171 disk->private_data = pmem;
172 disk->queue = pmem->pmem_queue;
173 disk->flags = GENHD_FL_EXT_DEVT;
Vishal Verma5212e112015-06-25 04:20:32 -0400174 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400175 disk->driverfs_dev = &ndns->dev;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200176 set_capacity(disk, pmem->size >> 9);
177 pmem->pmem_disk = disk;
178
179 add_disk(disk);
180
Dan Williams8c2f7e82015-06-25 04:20:04 -0400181 return 0;
182}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200183
Dan Williams8c2f7e82015-06-25 04:20:04 -0400184static int pmem_rw_bytes(struct nd_namespace_common *ndns,
185 resource_size_t offset, void *buf, size_t size, int rw)
186{
187 struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
188
189 if (unlikely(offset + size > pmem->size)) {
190 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
191 return -EFAULT;
192 }
193
194 if (rw == READ)
195 memcpy(buf, pmem->virt_addr + offset, size);
196 else
197 memcpy(pmem->virt_addr + offset, buf, size);
198
199 return 0;
200}
201
Ross Zwisler9e853f22015-04-01 09:12:19 +0200202static void pmem_free(struct pmem_device *pmem)
203{
Ross Zwisler9e853f22015-04-01 09:12:19 +0200204 iounmap(pmem->virt_addr);
205 release_mem_region(pmem->phys_addr, pmem->size);
206 kfree(pmem);
207}
208
Dan Williams9f53f9f2015-06-09 15:33:45 -0400209static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200210{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400211 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400212 struct nd_namespace_common *ndns;
213 struct nd_namespace_io *nsio;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200214 struct pmem_device *pmem;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400215 int rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200216
Dan Williams8c2f7e82015-06-25 04:20:04 -0400217 ndns = nvdimm_namespace_common_probe(dev);
218 if (IS_ERR(ndns))
219 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400220
Dan Williams8c2f7e82015-06-25 04:20:04 -0400221 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400222 pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200223 if (IS_ERR(pmem))
224 return PTR_ERR(pmem);
225
Dan Williams9f53f9f2015-06-09 15:33:45 -0400226 dev_set_drvdata(dev, pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400227 ndns->rw_bytes = pmem_rw_bytes;
228 if (is_nd_btt(dev))
229 rc = nvdimm_namespace_attach_btt(ndns);
230 else if (nd_btt_probe(ndns, pmem) == 0) {
231 /* we'll come back as btt-pmem */
232 rc = -ENXIO;
233 } else
234 rc = pmem_attach_disk(ndns, pmem);
235 if (rc)
236 pmem_free(pmem);
237 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200238}
239
Dan Williams9f53f9f2015-06-09 15:33:45 -0400240static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200241{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400242 struct pmem_device *pmem = dev_get_drvdata(dev);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200243
Dan Williams8c2f7e82015-06-25 04:20:04 -0400244 if (is_nd_btt(dev))
245 nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns);
246 else
247 pmem_detach_disk(pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200248 pmem_free(pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400249
Ross Zwisler9e853f22015-04-01 09:12:19 +0200250 return 0;
251}
252
Dan Williams9f53f9f2015-06-09 15:33:45 -0400253MODULE_ALIAS("pmem");
254MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400255MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400256static struct nd_device_driver nd_pmem_driver = {
257 .probe = nd_pmem_probe,
258 .remove = nd_pmem_remove,
259 .drv = {
260 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200261 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400262 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200263};
264
265static int __init pmem_init(void)
266{
267 int error;
268
269 pmem_major = register_blkdev(0, "pmem");
270 if (pmem_major < 0)
271 return pmem_major;
272
Dan Williams9f53f9f2015-06-09 15:33:45 -0400273 error = nd_driver_register(&nd_pmem_driver);
274 if (error) {
Ross Zwisler9e853f22015-04-01 09:12:19 +0200275 unregister_blkdev(pmem_major, "pmem");
Dan Williams9f53f9f2015-06-09 15:33:45 -0400276 return error;
277 }
278
279 return 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200280}
281module_init(pmem_init);
282
283static void pmem_exit(void)
284{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400285 driver_unregister(&nd_pmem_driver.drv);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200286 unregister_blkdev(pmem_major, "pmem");
287}
288module_exit(pmem_exit);
289
290MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
291MODULE_LICENSE("GPL v2");