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> |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 25 | #include <linux/badblocks.h> |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 26 | #include <linux/memremap.h> |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 27 | #include <linux/vmalloc.h> |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 28 | #include <linux/pfn_t.h> |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 29 | #include <linux/slab.h> |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 30 | #include <linux/pmem.h> |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 31 | #include <linux/nd.h> |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 32 | #include "pfn.h" |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 33 | #include "nd.h" |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 34 | |
| 35 | struct pmem_device { |
| 36 | struct request_queue *pmem_queue; |
| 37 | struct gendisk *pmem_disk; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 38 | struct nd_namespace_common *ndns; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 39 | |
| 40 | /* One contiguous memory region per device */ |
| 41 | phys_addr_t phys_addr; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 42 | /* when non-zero this device is hosting a 'pfn' instance */ |
| 43 | phys_addr_t data_offset; |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 44 | unsigned long pfn_flags; |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 45 | void __pmem *virt_addr; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 46 | size_t size; |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 47 | struct badblocks bb; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static int pmem_major; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 51 | |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 52 | static bool is_bad_pmem(struct badblocks *bb, sector_t sector, unsigned int len) |
| 53 | { |
| 54 | if (bb->count) { |
| 55 | sector_t first_bad; |
| 56 | int num_bad; |
| 57 | |
| 58 | return !!badblocks_check(bb, sector, len / 512, &first_bad, |
| 59 | &num_bad); |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | static int pmem_do_bvec(struct pmem_device *pmem, struct page *page, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 66 | unsigned int len, unsigned int off, int rw, |
| 67 | sector_t sector) |
| 68 | { |
| 69 | void *mem = kmap_atomic(page); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 70 | phys_addr_t pmem_off = sector * 512 + pmem->data_offset; |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 71 | void __pmem *pmem_addr = pmem->virt_addr + pmem_off; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 72 | |
| 73 | if (rw == READ) { |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 74 | if (unlikely(is_bad_pmem(&pmem->bb, sector, len))) |
| 75 | return -EIO; |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 76 | memcpy_from_pmem(mem + off, pmem_addr, len); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 77 | flush_dcache_page(page); |
| 78 | } else { |
| 79 | flush_dcache_page(page); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 80 | memcpy_to_pmem(pmem_addr, mem + off, len); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | kunmap_atomic(mem); |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 84 | return 0; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 87 | static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 88 | { |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 89 | int rc = 0; |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 90 | bool do_acct; |
| 91 | unsigned long start; |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame] | 92 | struct bio_vec bvec; |
| 93 | struct bvec_iter iter; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 94 | struct block_device *bdev = bio->bi_bdev; |
| 95 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 96 | |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 97 | do_acct = nd_iostat_start(bio, &start); |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 98 | bio_for_each_segment(bvec, bio, iter) { |
| 99 | rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, |
| 100 | bvec.bv_offset, bio_data_dir(bio), |
| 101 | iter.bi_sector); |
| 102 | if (rc) { |
| 103 | bio->bi_error = rc; |
| 104 | break; |
| 105 | } |
| 106 | } |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 107 | if (do_acct) |
| 108 | nd_iostat_end(bio, start); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 109 | |
| 110 | if (bio_data_dir(bio)) |
| 111 | wmb_pmem(); |
| 112 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 113 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 114 | return BLK_QC_T_NONE; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int pmem_rw_page(struct block_device *bdev, sector_t sector, |
| 118 | struct page *page, int rw) |
| 119 | { |
| 120 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 121 | int rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 122 | |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 123 | rc = pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector); |
Ross Zwisler | ba8fe0f | 2015-09-16 14:52:21 -0600 | [diff] [blame] | 124 | if (rw & WRITE) |
| 125 | wmb_pmem(); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 126 | |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 127 | /* |
| 128 | * The ->rw_page interface is subtle and tricky. The core |
| 129 | * retries on any error, so we can only invoke page_endio() in |
| 130 | * the successful completion case. Otherwise, we'll see crashes |
| 131 | * caused by double completion. |
| 132 | */ |
| 133 | if (rc == 0) |
| 134 | page_endio(page, rw & WRITE, 0); |
| 135 | |
| 136 | return rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static long pmem_direct_access(struct block_device *bdev, sector_t sector, |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 140 | void __pmem **kaddr, pfn_t *pfn) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 141 | { |
| 142 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 143 | resource_size_t offset = sector * 512 + pmem->data_offset; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 144 | |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 145 | *kaddr = pmem->virt_addr + offset; |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 146 | *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 147 | |
Dan Williams | 589e75d | 2015-10-24 19:55:58 -0700 | [diff] [blame] | 148 | return pmem->size - offset; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static const struct block_device_operations pmem_fops = { |
| 152 | .owner = THIS_MODULE, |
| 153 | .rw_page = pmem_rw_page, |
| 154 | .direct_access = pmem_direct_access, |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 155 | .revalidate_disk = nvdimm_revalidate_disk, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 158 | static struct pmem_device *pmem_alloc(struct device *dev, |
| 159 | struct resource *res, int id) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 160 | { |
| 161 | struct pmem_device *pmem; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 162 | |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 163 | pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 164 | if (!pmem) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 165 | return ERR_PTR(-ENOMEM); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 166 | |
| 167 | pmem->phys_addr = res->start; |
| 168 | pmem->size = resource_size(res); |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 169 | if (!arch_has_wmb_pmem()) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 170 | dev_warn(dev, "unable to guarantee persistence of writes\n"); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 171 | |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 172 | if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size, |
| 173 | dev_name(dev))) { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 174 | dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n", |
| 175 | &pmem->phys_addr, pmem->size); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 176 | return ERR_PTR(-EBUSY); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 179 | pmem->pfn_flags = PFN_DEV; |
| 180 | if (pmem_should_map_pages(dev)) { |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 181 | pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, res, |
| 182 | NULL); |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 183 | pmem->pfn_flags |= PFN_MAP; |
| 184 | } else |
Dan Williams | a639315 | 2015-09-15 02:14:03 -0400 | [diff] [blame] | 185 | pmem->virt_addr = (void __pmem *) devm_memremap(dev, |
| 186 | pmem->phys_addr, pmem->size, |
| 187 | ARCH_MEMREMAP_PMEM); |
Dan Williams | b36f476 | 2015-09-15 02:42:20 -0400 | [diff] [blame] | 188 | |
| 189 | if (IS_ERR(pmem->virt_addr)) |
| 190 | return (void __force *) pmem->virt_addr; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 191 | |
| 192 | return pmem; |
| 193 | } |
| 194 | |
| 195 | static void pmem_detach_disk(struct pmem_device *pmem) |
| 196 | { |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 197 | if (!pmem->pmem_disk) |
| 198 | return; |
| 199 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 200 | del_gendisk(pmem->pmem_disk); |
| 201 | put_disk(pmem->pmem_disk); |
| 202 | blk_cleanup_queue(pmem->pmem_queue); |
| 203 | } |
| 204 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 205 | static int pmem_attach_disk(struct device *dev, |
| 206 | struct nd_namespace_common *ndns, struct pmem_device *pmem) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 207 | { |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 208 | int nid = dev_to_node(dev); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 209 | struct gendisk *disk; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 210 | |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 211 | pmem->pmem_queue = blk_alloc_queue_node(GFP_KERNEL, nid); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 212 | if (!pmem->pmem_queue) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 213 | return -ENOMEM; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 214 | |
| 215 | blk_queue_make_request(pmem->pmem_queue, pmem_make_request); |
Vishal Verma | 6b47496 | 2015-07-23 11:58:48 -0600 | [diff] [blame] | 216 | blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE); |
Dan Williams | 43d3fa3 | 2015-05-16 12:28:50 -0400 | [diff] [blame] | 217 | blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 218 | blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY); |
Dan Williams | 0f51c4f | 2015-05-16 12:28:54 -0400 | [diff] [blame] | 219 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 220 | |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 221 | disk = alloc_disk_node(0, nid); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 222 | if (!disk) { |
| 223 | blk_cleanup_queue(pmem->pmem_queue); |
| 224 | return -ENOMEM; |
| 225 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 226 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 227 | disk->major = pmem_major; |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 228 | disk->first_minor = 0; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 229 | disk->fops = &pmem_fops; |
| 230 | disk->private_data = pmem; |
| 231 | disk->queue = pmem->pmem_queue; |
| 232 | disk->flags = GENHD_FL_EXT_DEVT; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 233 | nvdimm_namespace_disk_name(ndns, disk->disk_name); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 234 | disk->driverfs_dev = dev; |
| 235 | set_capacity(disk, (pmem->size - pmem->data_offset) / 512); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 236 | pmem->pmem_disk = disk; |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 237 | devm_exit_badblocks(dev, &pmem->bb); |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 238 | if (devm_init_badblocks(dev, &pmem->bb)) |
| 239 | return -ENOMEM; |
| 240 | nvdimm_namespace_add_poison(ndns, &pmem->bb, pmem->data_offset); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 241 | |
Dan Williams | 57f7f31 | 2016-01-06 12:03:42 -0800 | [diff] [blame] | 242 | disk->bb = &pmem->bb; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 243 | add_disk(disk); |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 244 | revalidate_disk(disk); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 245 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 246 | return 0; |
| 247 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 248 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 249 | static int pmem_rw_bytes(struct nd_namespace_common *ndns, |
| 250 | resource_size_t offset, void *buf, size_t size, int rw) |
| 251 | { |
| 252 | struct pmem_device *pmem = dev_get_drvdata(ndns->claim); |
| 253 | |
| 254 | if (unlikely(offset + size > pmem->size)) { |
| 255 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); |
| 256 | return -EFAULT; |
| 257 | } |
| 258 | |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 259 | if (rw == READ) { |
| 260 | unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512); |
| 261 | |
| 262 | if (unlikely(is_bad_pmem(&pmem->bb, offset / 512, sz_align))) |
| 263 | return -EIO; |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 264 | memcpy_from_pmem(buf, pmem->virt_addr + offset, size); |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 265 | } else { |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 266 | memcpy_to_pmem(pmem->virt_addr + offset, buf, size); |
| 267 | wmb_pmem(); |
| 268 | } |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 273 | static int nd_pfn_init(struct nd_pfn *nd_pfn) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 274 | { |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 275 | struct nd_pfn_sb *pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL); |
| 276 | struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev); |
| 277 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 278 | struct nd_region *nd_region; |
| 279 | unsigned long npfns; |
| 280 | phys_addr_t offset; |
| 281 | u64 checksum; |
| 282 | int rc; |
| 283 | |
| 284 | if (!pfn_sb) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | nd_pfn->pfn_sb = pfn_sb; |
| 288 | rc = nd_pfn_validate(nd_pfn); |
Dan Williams | 3fa9626 | 2015-12-13 11:35:52 -0800 | [diff] [blame] | 289 | if (rc == -ENODEV) |
| 290 | /* no info block, do init */; |
| 291 | else |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 292 | return rc; |
| 293 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 294 | nd_region = to_nd_region(nd_pfn->dev.parent); |
| 295 | if (nd_region->ro) { |
| 296 | dev_info(&nd_pfn->dev, |
| 297 | "%s is read-only, unable to init metadata\n", |
| 298 | dev_name(&nd_region->dev)); |
| 299 | goto err; |
| 300 | } |
| 301 | |
| 302 | memset(pfn_sb, 0, sizeof(*pfn_sb)); |
| 303 | npfns = (pmem->size - SZ_8K) / SZ_4K; |
| 304 | /* |
| 305 | * Note, we use 64 here for the standard size of struct page, |
| 306 | * debugging options may cause it to be larger in which case the |
| 307 | * implementation will limit the pfns advertised through |
| 308 | * ->direct_access() to those that are included in the memmap. |
| 309 | */ |
| 310 | if (nd_pfn->mode == PFN_MODE_PMEM) |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 311 | offset = ALIGN(SZ_8K + 64 * npfns, nd_pfn->align); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 312 | else if (nd_pfn->mode == PFN_MODE_RAM) |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 313 | offset = ALIGN(SZ_8K, nd_pfn->align); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 314 | else |
| 315 | goto err; |
| 316 | |
| 317 | npfns = (pmem->size - offset) / SZ_4K; |
| 318 | pfn_sb->mode = cpu_to_le32(nd_pfn->mode); |
| 319 | pfn_sb->dataoff = cpu_to_le64(offset); |
| 320 | pfn_sb->npfns = cpu_to_le64(npfns); |
| 321 | memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN); |
| 322 | memcpy(pfn_sb->uuid, nd_pfn->uuid, 16); |
Dan Williams | a34d5e8 | 2015-12-12 16:09:14 -0800 | [diff] [blame] | 323 | memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 324 | pfn_sb->version_major = cpu_to_le16(1); |
| 325 | checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb); |
| 326 | pfn_sb->checksum = cpu_to_le64(checksum); |
| 327 | |
| 328 | rc = nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)); |
| 329 | if (rc) |
| 330 | goto err; |
| 331 | |
| 332 | return 0; |
| 333 | err: |
| 334 | nd_pfn->pfn_sb = NULL; |
| 335 | kfree(pfn_sb); |
| 336 | return -ENXIO; |
| 337 | } |
| 338 | |
| 339 | static int nvdimm_namespace_detach_pfn(struct nd_namespace_common *ndns) |
| 340 | { |
| 341 | struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim); |
| 342 | struct pmem_device *pmem; |
| 343 | |
| 344 | /* free pmem disk */ |
| 345 | pmem = dev_get_drvdata(&nd_pfn->dev); |
| 346 | pmem_detach_disk(pmem); |
| 347 | |
| 348 | /* release nd_pfn resources */ |
| 349 | kfree(nd_pfn->pfn_sb); |
| 350 | nd_pfn->pfn_sb = NULL; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns) |
| 356 | { |
| 357 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 358 | struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim); |
| 359 | struct device *dev = &nd_pfn->dev; |
| 360 | struct vmem_altmap *altmap; |
| 361 | struct nd_region *nd_region; |
| 362 | struct nd_pfn_sb *pfn_sb; |
| 363 | struct pmem_device *pmem; |
| 364 | phys_addr_t offset; |
| 365 | int rc; |
| 366 | |
| 367 | if (!nd_pfn->uuid || !nd_pfn->ndns) |
| 368 | return -ENODEV; |
| 369 | |
| 370 | nd_region = to_nd_region(dev->parent); |
| 371 | rc = nd_pfn_init(nd_pfn); |
| 372 | if (rc) |
| 373 | return rc; |
| 374 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 375 | pfn_sb = nd_pfn->pfn_sb; |
| 376 | offset = le64_to_cpu(pfn_sb->dataoff); |
| 377 | nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode); |
| 378 | if (nd_pfn->mode == PFN_MODE_RAM) { |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 379 | if (offset < SZ_8K) |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns); |
| 382 | altmap = NULL; |
| 383 | } else { |
| 384 | rc = -ENXIO; |
| 385 | goto err; |
| 386 | } |
| 387 | |
| 388 | /* establish pfn range for lookup, and switch to direct map */ |
| 389 | pmem = dev_get_drvdata(dev); |
Dan Williams | a639315 | 2015-09-15 02:14:03 -0400 | [diff] [blame] | 390 | devm_memunmap(dev, (void __force *) pmem->virt_addr); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 391 | pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, &nsio->res, |
| 392 | NULL); |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 393 | pmem->pfn_flags |= PFN_MAP; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 394 | if (IS_ERR(pmem->virt_addr)) { |
| 395 | rc = PTR_ERR(pmem->virt_addr); |
| 396 | goto err; |
| 397 | } |
| 398 | |
| 399 | /* attach pmem disk in "pfn-mode" */ |
| 400 | pmem->data_offset = offset; |
| 401 | rc = pmem_attach_disk(dev, ndns, pmem); |
| 402 | if (rc) |
| 403 | goto err; |
| 404 | |
| 405 | return rc; |
| 406 | err: |
| 407 | nvdimm_namespace_detach_pfn(ndns); |
| 408 | return rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 411 | static int nd_pmem_probe(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 412 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 413 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 414 | struct nd_namespace_common *ndns; |
| 415 | struct nd_namespace_io *nsio; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 416 | struct pmem_device *pmem; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 417 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 418 | ndns = nvdimm_namespace_common_probe(dev); |
| 419 | if (IS_ERR(ndns)) |
| 420 | return PTR_ERR(ndns); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 421 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 422 | nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 423 | pmem = pmem_alloc(dev, &nsio->res, nd_region->id); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 424 | if (IS_ERR(pmem)) |
| 425 | return PTR_ERR(pmem); |
| 426 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 427 | pmem->ndns = ndns; |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 428 | dev_set_drvdata(dev, pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 429 | ndns->rw_bytes = pmem_rw_bytes; |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 430 | if (devm_init_badblocks(dev, &pmem->bb)) |
| 431 | return -ENOMEM; |
| 432 | nvdimm_namespace_add_poison(ndns, &pmem->bb, 0); |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 433 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 434 | if (is_nd_btt(dev)) |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 435 | return nvdimm_namespace_attach_btt(ndns); |
| 436 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 437 | if (is_nd_pfn(dev)) |
| 438 | return nvdimm_namespace_attach_pfn(ndns); |
| 439 | |
| 440 | if (nd_btt_probe(ndns, pmem) == 0) { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 441 | /* we'll come back as btt-pmem */ |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 442 | return -ENXIO; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (nd_pfn_probe(ndns, pmem) == 0) { |
| 446 | /* we'll come back as pfn-pmem */ |
| 447 | return -ENXIO; |
| 448 | } |
| 449 | |
| 450 | return pmem_attach_disk(dev, ndns, pmem); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 453 | static int nd_pmem_remove(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 454 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 455 | struct pmem_device *pmem = dev_get_drvdata(dev); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 456 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 457 | if (is_nd_btt(dev)) |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 458 | nvdimm_namespace_detach_btt(pmem->ndns); |
| 459 | else if (is_nd_pfn(dev)) |
| 460 | nvdimm_namespace_detach_pfn(pmem->ndns); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 461 | else |
| 462 | pmem_detach_disk(pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 463 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 467 | MODULE_ALIAS("pmem"); |
| 468 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 469 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 470 | static struct nd_device_driver nd_pmem_driver = { |
| 471 | .probe = nd_pmem_probe, |
| 472 | .remove = nd_pmem_remove, |
| 473 | .drv = { |
| 474 | .name = "nd_pmem", |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 475 | }, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 476 | .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | static int __init pmem_init(void) |
| 480 | { |
| 481 | int error; |
| 482 | |
| 483 | pmem_major = register_blkdev(0, "pmem"); |
| 484 | if (pmem_major < 0) |
| 485 | return pmem_major; |
| 486 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 487 | error = nd_driver_register(&nd_pmem_driver); |
| 488 | if (error) { |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 489 | unregister_blkdev(pmem_major, "pmem"); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 490 | return error; |
| 491 | } |
| 492 | |
| 493 | return 0; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 494 | } |
| 495 | module_init(pmem_init); |
| 496 | |
| 497 | static void pmem_exit(void) |
| 498 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 499 | driver_unregister(&nd_pmem_driver.drv); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 500 | unregister_blkdev(pmem_major, "pmem"); |
| 501 | } |
| 502 | module_exit(pmem_exit); |
| 503 | |
| 504 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); |
| 505 | MODULE_LICENSE("GPL v2"); |