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 | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame^] | 61 | bool do_acct; |
| 62 | unsigned long start; |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame] | 63 | struct bio_vec bvec; |
| 64 | struct bvec_iter iter; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 65 | struct block_device *bdev = bio->bi_bdev; |
| 66 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 67 | |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame^] | 68 | do_acct = nd_iostat_start(bio, &start); |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame] | 69 | bio_for_each_segment(bvec, bio, iter) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 70 | 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] | 71 | bio_data_dir(bio), iter.bi_sector); |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame^] | 72 | if (do_acct) |
| 73 | nd_iostat_end(bio, start); |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame] | 74 | bio_endio(bio, 0); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static 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 | |
| 88 | static 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 | |
| 103 | static 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 Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 109 | static struct pmem_device *pmem_alloc(struct device *dev, |
| 110 | struct resource *res, int id) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 111 | { |
| 112 | struct pmem_device *pmem; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 113 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 114 | pmem = kzalloc(sizeof(*pmem), GFP_KERNEL); |
| 115 | if (!pmem) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 116 | return ERR_PTR(-ENOMEM); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 117 | |
| 118 | pmem->phys_addr = res->start; |
| 119 | pmem->size = resource_size(res); |
| 120 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 121 | if (!request_mem_region(pmem->phys_addr, pmem->size, dev_name(dev))) { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 122 | dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n", |
| 123 | &pmem->phys_addr, pmem->size); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 124 | kfree(pmem); |
| 125 | return ERR_PTR(-EBUSY); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 126 | } |
| 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 Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 132 | pmem->virt_addr = ioremap_nocache(pmem->phys_addr, pmem->size); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 133 | 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 | |
| 142 | static 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 | |
| 149 | static int pmem_attach_disk(struct nd_namespace_common *ndns, |
| 150 | struct pmem_device *pmem) |
| 151 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 152 | struct gendisk *disk; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 153 | |
| 154 | pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL); |
| 155 | if (!pmem->pmem_queue) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 156 | return -ENOMEM; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 157 | |
| 158 | blk_queue_make_request(pmem->pmem_queue, pmem_make_request); |
Dan Williams | 43d3fa3 | 2015-05-16 12:28:50 -0400 | [diff] [blame] | 159 | blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 160 | blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY); |
| 161 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 162 | disk = alloc_disk(0); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 163 | if (!disk) { |
| 164 | blk_cleanup_queue(pmem->pmem_queue); |
| 165 | return -ENOMEM; |
| 166 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 167 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 168 | disk->major = pmem_major; |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 169 | disk->first_minor = 0; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 170 | disk->fops = &pmem_fops; |
| 171 | disk->private_data = pmem; |
| 172 | disk->queue = pmem->pmem_queue; |
| 173 | disk->flags = GENHD_FL_EXT_DEVT; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 174 | nvdimm_namespace_disk_name(ndns, disk->disk_name); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 175 | disk->driverfs_dev = &ndns->dev; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 176 | set_capacity(disk, pmem->size >> 9); |
| 177 | pmem->pmem_disk = disk; |
| 178 | |
| 179 | add_disk(disk); |
| 180 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 181 | return 0; |
| 182 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 183 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 184 | static 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 Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 202 | static void pmem_free(struct pmem_device *pmem) |
| 203 | { |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 204 | iounmap(pmem->virt_addr); |
| 205 | release_mem_region(pmem->phys_addr, pmem->size); |
| 206 | kfree(pmem); |
| 207 | } |
| 208 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 209 | static int nd_pmem_probe(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 210 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 211 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 212 | struct nd_namespace_common *ndns; |
| 213 | struct nd_namespace_io *nsio; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 214 | struct pmem_device *pmem; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 215 | int rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 216 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 217 | ndns = nvdimm_namespace_common_probe(dev); |
| 218 | if (IS_ERR(ndns)) |
| 219 | return PTR_ERR(ndns); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 220 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 221 | nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 222 | pmem = pmem_alloc(dev, &nsio->res, nd_region->id); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 223 | if (IS_ERR(pmem)) |
| 224 | return PTR_ERR(pmem); |
| 225 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 226 | dev_set_drvdata(dev, pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 227 | 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 Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 240 | static int nd_pmem_remove(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 241 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 242 | struct pmem_device *pmem = dev_get_drvdata(dev); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 243 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 244 | if (is_nd_btt(dev)) |
| 245 | nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns); |
| 246 | else |
| 247 | pmem_detach_disk(pmem); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 248 | pmem_free(pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 249 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 253 | MODULE_ALIAS("pmem"); |
| 254 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 255 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 256 | static struct nd_device_driver nd_pmem_driver = { |
| 257 | .probe = nd_pmem_probe, |
| 258 | .remove = nd_pmem_remove, |
| 259 | .drv = { |
| 260 | .name = "nd_pmem", |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 261 | }, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 262 | .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | static 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 Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 273 | error = nd_driver_register(&nd_pmem_driver); |
| 274 | if (error) { |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 275 | unregister_blkdev(pmem_major, "pmem"); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 276 | return error; |
| 277 | } |
| 278 | |
| 279 | return 0; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 280 | } |
| 281 | module_init(pmem_init); |
| 282 | |
| 283 | static void pmem_exit(void) |
| 284 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 285 | driver_unregister(&nd_pmem_driver.drv); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 286 | unregister_blkdev(pmem_major, "pmem"); |
| 287 | } |
| 288 | module_exit(pmem_exit); |
| 289 | |
| 290 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); |
| 291 | MODULE_LICENSE("GPL v2"); |