Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Persistent Memory Driver |
| 3 | * |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 4 | * Copyright (c) 2014-2015, Intel Corporation. |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 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> |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 26 | #include <linux/nd.h> |
| 27 | #include "nd.h" |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 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; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 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 | { |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame^] | 61 | struct bio_vec bvec; |
| 62 | struct bvec_iter iter; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 63 | struct block_device *bdev = bio->bi_bdev; |
| 64 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 65 | |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame^] | 66 | bio_for_each_segment(bvec, bio, iter) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 67 | pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset, |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame^] | 68 | bio_data_dir(bio), iter.bi_sector); |
| 69 | bio_endio(bio, 0); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static 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 | |
| 83 | static 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 | |
| 98 | static 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 Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 104 | static struct pmem_device *pmem_alloc(struct device *dev, |
| 105 | struct resource *res, int id) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 106 | { |
| 107 | struct pmem_device *pmem; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 108 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 109 | pmem = kzalloc(sizeof(*pmem), GFP_KERNEL); |
| 110 | if (!pmem) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 111 | return ERR_PTR(-ENOMEM); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 112 | |
| 113 | pmem->phys_addr = res->start; |
| 114 | pmem->size = resource_size(res); |
| 115 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 116 | if (!request_mem_region(pmem->phys_addr, pmem->size, dev_name(dev))) { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 117 | dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n", |
| 118 | &pmem->phys_addr, pmem->size); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 119 | kfree(pmem); |
| 120 | return ERR_PTR(-EBUSY); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 121 | } |
| 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 Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 127 | pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem->size); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 128 | 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 | |
| 137 | static 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 | |
| 144 | static int pmem_attach_disk(struct nd_namespace_common *ndns, |
| 145 | struct pmem_device *pmem) |
| 146 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 147 | struct gendisk *disk; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 148 | |
| 149 | pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL); |
| 150 | if (!pmem->pmem_queue) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 151 | return -ENOMEM; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 152 | |
| 153 | blk_queue_make_request(pmem->pmem_queue, pmem_make_request); |
Dan Williams | 43d3fa3 | 2015-05-16 12:28:50 -0400 | [diff] [blame] | 154 | blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 155 | blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY); |
| 156 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 157 | disk = alloc_disk(0); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 158 | if (!disk) { |
| 159 | blk_cleanup_queue(pmem->pmem_queue); |
| 160 | return -ENOMEM; |
| 161 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 162 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 163 | disk->major = pmem_major; |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 164 | disk->first_minor = 0; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 165 | disk->fops = &pmem_fops; |
| 166 | disk->private_data = pmem; |
| 167 | disk->queue = pmem->pmem_queue; |
| 168 | disk->flags = GENHD_FL_EXT_DEVT; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 169 | nvdimm_namespace_disk_name(ndns, disk->disk_name); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 170 | disk->driverfs_dev = &ndns->dev; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 171 | set_capacity(disk, pmem->size >> 9); |
| 172 | pmem->pmem_disk = disk; |
| 173 | |
| 174 | add_disk(disk); |
| 175 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 176 | return 0; |
| 177 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 178 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 179 | static 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 Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 197 | static void pmem_free(struct pmem_device *pmem) |
| 198 | { |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 199 | iounmap(pmem->virt_addr); |
| 200 | release_mem_region(pmem->phys_addr, pmem->size); |
| 201 | kfree(pmem); |
| 202 | } |
| 203 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 204 | static int nd_pmem_probe(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 205 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 206 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 207 | struct nd_namespace_common *ndns; |
| 208 | struct nd_namespace_io *nsio; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 209 | struct pmem_device *pmem; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 210 | int rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 211 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 212 | ndns = nvdimm_namespace_common_probe(dev); |
| 213 | if (IS_ERR(ndns)) |
| 214 | return PTR_ERR(ndns); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 215 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 216 | nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 217 | pmem = pmem_alloc(dev, &nsio->res, nd_region->id); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 218 | if (IS_ERR(pmem)) |
| 219 | return PTR_ERR(pmem); |
| 220 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 221 | dev_set_drvdata(dev, pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 222 | 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 Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 235 | static int nd_pmem_remove(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 236 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 237 | struct pmem_device *pmem = dev_get_drvdata(dev); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 238 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 239 | if (is_nd_btt(dev)) |
| 240 | nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns); |
| 241 | else |
| 242 | pmem_detach_disk(pmem); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 243 | pmem_free(pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 244 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 248 | MODULE_ALIAS("pmem"); |
| 249 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 250 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 251 | static struct nd_device_driver nd_pmem_driver = { |
| 252 | .probe = nd_pmem_probe, |
| 253 | .remove = nd_pmem_remove, |
| 254 | .drv = { |
| 255 | .name = "nd_pmem", |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 256 | }, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 257 | .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | static 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 Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 268 | error = nd_driver_register(&nd_pmem_driver); |
| 269 | if (error) { |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 270 | unregister_blkdev(pmem_major, "pmem"); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 271 | return error; |
| 272 | } |
| 273 | |
| 274 | return 0; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 275 | } |
| 276 | module_init(pmem_init); |
| 277 | |
| 278 | static void pmem_exit(void) |
| 279 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 280 | driver_unregister(&nd_pmem_driver.drv); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 281 | unregister_blkdev(pmem_major, "pmem"); |
| 282 | } |
| 283 | module_exit(pmem_exit); |
| 284 | |
| 285 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); |
| 286 | MODULE_LICENSE("GPL v2"); |