blob: e846a627ebdf9952352c4db1b7aa69988ac1a57f [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 Williamsedc870e2015-05-16 12:28:51 -040061 struct bio_vec bvec;
62 struct bvec_iter iter;
Ross Zwisler9e853f22015-04-01 09:12:19 +020063 struct block_device *bdev = bio->bi_bdev;
64 struct pmem_device *pmem = bdev->bd_disk->private_data;
Ross Zwisler9e853f22015-04-01 09:12:19 +020065
Dan Williamsedc870e2015-05-16 12:28:51 -040066 bio_for_each_segment(bvec, bio, iter)
Ross Zwisler9e853f22015-04-01 09:12:19 +020067 pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset,
Dan Williamsedc870e2015-05-16 12:28:51 -040068 bio_data_dir(bio), iter.bi_sector);
69 bio_endio(bio, 0);
Ross Zwisler9e853f22015-04-01 09:12:19 +020070}
71
72static int pmem_rw_page(struct block_device *bdev, sector_t sector,
73 struct page *page, int rw)
74{
75 struct pmem_device *pmem = bdev->bd_disk->private_data;
76
77 pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector);
78 page_endio(page, rw & WRITE, 0);
79
80 return 0;
81}
82
83static long pmem_direct_access(struct block_device *bdev, sector_t sector,
84 void **kaddr, unsigned long *pfn, long size)
85{
86 struct pmem_device *pmem = bdev->bd_disk->private_data;
87 size_t offset = sector << 9;
88
89 if (!pmem)
90 return -ENODEV;
91
92 *kaddr = pmem->virt_addr + offset;
93 *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT;
94
95 return pmem->size - offset;
96}
97
98static const struct block_device_operations pmem_fops = {
99 .owner = THIS_MODULE,
100 .rw_page = pmem_rw_page,
101 .direct_access = pmem_direct_access,
102};
103
Dan Williams9f53f9f2015-06-09 15:33:45 -0400104static struct pmem_device *pmem_alloc(struct device *dev,
105 struct resource *res, int id)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200106{
107 struct pmem_device *pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200108
Ross Zwisler9e853f22015-04-01 09:12:19 +0200109 pmem = kzalloc(sizeof(*pmem), GFP_KERNEL);
110 if (!pmem)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400111 return ERR_PTR(-ENOMEM);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200112
113 pmem->phys_addr = res->start;
114 pmem->size = resource_size(res);
115
Dan Williams8c2f7e82015-06-25 04:20:04 -0400116 if (!request_mem_region(pmem->phys_addr, pmem->size, dev_name(dev))) {
Dan Williams9f53f9f2015-06-09 15:33:45 -0400117 dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
118 &pmem->phys_addr, pmem->size);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400119 kfree(pmem);
120 return ERR_PTR(-EBUSY);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200121 }
122
123 /*
124 * Map the memory as non-cachable, as we can't write back the contents
125 * of the CPU caches in case of a crash.
126 */
Ross Zwisler9e853f22015-04-01 09:12:19 +0200127 pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem->size);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400128 if (!pmem->virt_addr) {
129 release_mem_region(pmem->phys_addr, pmem->size);
130 kfree(pmem);
131 return ERR_PTR(-ENXIO);
132 }
133
134 return pmem;
135}
136
137static void pmem_detach_disk(struct pmem_device *pmem)
138{
139 del_gendisk(pmem->pmem_disk);
140 put_disk(pmem->pmem_disk);
141 blk_cleanup_queue(pmem->pmem_queue);
142}
143
144static int pmem_attach_disk(struct nd_namespace_common *ndns,
145 struct pmem_device *pmem)
146{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400147 struct gendisk *disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200148
149 pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL);
150 if (!pmem->pmem_queue)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400151 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200152
153 blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
Dan Williams43d3fa32015-05-16 12:28:50 -0400154 blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200155 blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
156
Dan Williams9f53f9f2015-06-09 15:33:45 -0400157 disk = alloc_disk(0);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400158 if (!disk) {
159 blk_cleanup_queue(pmem->pmem_queue);
160 return -ENOMEM;
161 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200162
Ross Zwisler9e853f22015-04-01 09:12:19 +0200163 disk->major = pmem_major;
Dan Williams9f53f9f2015-06-09 15:33:45 -0400164 disk->first_minor = 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200165 disk->fops = &pmem_fops;
166 disk->private_data = pmem;
167 disk->queue = pmem->pmem_queue;
168 disk->flags = GENHD_FL_EXT_DEVT;
Vishal Verma5212e112015-06-25 04:20:32 -0400169 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400170 disk->driverfs_dev = &ndns->dev;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200171 set_capacity(disk, pmem->size >> 9);
172 pmem->pmem_disk = disk;
173
174 add_disk(disk);
175
Dan Williams8c2f7e82015-06-25 04:20:04 -0400176 return 0;
177}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200178
Dan Williams8c2f7e82015-06-25 04:20:04 -0400179static int pmem_rw_bytes(struct nd_namespace_common *ndns,
180 resource_size_t offset, void *buf, size_t size, int rw)
181{
182 struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
183
184 if (unlikely(offset + size > pmem->size)) {
185 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
186 return -EFAULT;
187 }
188
189 if (rw == READ)
190 memcpy(buf, pmem->virt_addr + offset, size);
191 else
192 memcpy(pmem->virt_addr + offset, buf, size);
193
194 return 0;
195}
196
Ross Zwisler9e853f22015-04-01 09:12:19 +0200197static void pmem_free(struct pmem_device *pmem)
198{
Ross Zwisler9e853f22015-04-01 09:12:19 +0200199 iounmap(pmem->virt_addr);
200 release_mem_region(pmem->phys_addr, pmem->size);
201 kfree(pmem);
202}
203
Dan Williams9f53f9f2015-06-09 15:33:45 -0400204static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200205{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400206 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400207 struct nd_namespace_common *ndns;
208 struct nd_namespace_io *nsio;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200209 struct pmem_device *pmem;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400210 int rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200211
Dan Williams8c2f7e82015-06-25 04:20:04 -0400212 ndns = nvdimm_namespace_common_probe(dev);
213 if (IS_ERR(ndns))
214 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400215
Dan Williams8c2f7e82015-06-25 04:20:04 -0400216 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400217 pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200218 if (IS_ERR(pmem))
219 return PTR_ERR(pmem);
220
Dan Williams9f53f9f2015-06-09 15:33:45 -0400221 dev_set_drvdata(dev, pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400222 ndns->rw_bytes = pmem_rw_bytes;
223 if (is_nd_btt(dev))
224 rc = nvdimm_namespace_attach_btt(ndns);
225 else if (nd_btt_probe(ndns, pmem) == 0) {
226 /* we'll come back as btt-pmem */
227 rc = -ENXIO;
228 } else
229 rc = pmem_attach_disk(ndns, pmem);
230 if (rc)
231 pmem_free(pmem);
232 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200233}
234
Dan Williams9f53f9f2015-06-09 15:33:45 -0400235static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200236{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400237 struct pmem_device *pmem = dev_get_drvdata(dev);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200238
Dan Williams8c2f7e82015-06-25 04:20:04 -0400239 if (is_nd_btt(dev))
240 nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns);
241 else
242 pmem_detach_disk(pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200243 pmem_free(pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400244
Ross Zwisler9e853f22015-04-01 09:12:19 +0200245 return 0;
246}
247
Dan Williams9f53f9f2015-06-09 15:33:45 -0400248MODULE_ALIAS("pmem");
249MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400250MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400251static struct nd_device_driver nd_pmem_driver = {
252 .probe = nd_pmem_probe,
253 .remove = nd_pmem_remove,
254 .drv = {
255 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200256 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400257 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200258};
259
260static int __init pmem_init(void)
261{
262 int error;
263
264 pmem_major = register_blkdev(0, "pmem");
265 if (pmem_major < 0)
266 return pmem_major;
267
Dan Williams9f53f9f2015-06-09 15:33:45 -0400268 error = nd_driver_register(&nd_pmem_driver);
269 if (error) {
Ross Zwisler9e853f22015-04-01 09:12:19 +0200270 unregister_blkdev(pmem_major, "pmem");
Dan Williams9f53f9f2015-06-09 15:33:45 -0400271 return error;
272 }
273
274 return 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200275}
276module_init(pmem_init);
277
278static void pmem_exit(void)
279{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400280 driver_unregister(&nd_pmem_driver.drv);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200281 unregister_blkdev(pmem_major, "pmem");
282}
283module_exit(pmem_exit);
284
285MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
286MODULE_LICENSE("GPL v2");