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; |
Arnd Bergmann | c454420 | 2016-02-22 22:58:34 +0100 | [diff] [blame] | 44 | u64 pfn_flags; |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 45 | void __pmem *virt_addr; |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 46 | /* immutable base size of the namespace */ |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 47 | size_t size; |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 48 | /* trim size when namespace capacity has been section aligned */ |
| 49 | u32 pfn_pad; |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 50 | struct badblocks bb; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 53 | static bool is_bad_pmem(struct badblocks *bb, sector_t sector, unsigned int len) |
| 54 | { |
| 55 | if (bb->count) { |
| 56 | sector_t first_bad; |
| 57 | int num_bad; |
| 58 | |
| 59 | return !!badblocks_check(bb, sector, len / 512, &first_bad, |
| 60 | &num_bad); |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 66 | static void pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset, |
| 67 | unsigned int len) |
| 68 | { |
| 69 | struct device *dev = disk_to_dev(pmem->pmem_disk); |
| 70 | sector_t sector; |
| 71 | long cleared; |
| 72 | |
| 73 | sector = (offset - pmem->data_offset) / 512; |
| 74 | cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len); |
| 75 | |
| 76 | if (cleared > 0 && cleared / 512) { |
| 77 | dev_dbg(dev, "%s: %llx clear %ld sector%s\n", |
| 78 | __func__, (unsigned long long) sector, |
| 79 | cleared / 512, cleared / 512 > 1 ? "s" : ""); |
| 80 | badblocks_clear(&pmem->bb, sector, cleared / 512); |
| 81 | } |
| 82 | invalidate_pmem(pmem->virt_addr + offset, len); |
| 83 | } |
| 84 | |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 85 | static int pmem_do_bvec(struct pmem_device *pmem, struct page *page, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 86 | unsigned int len, unsigned int off, int rw, |
| 87 | sector_t sector) |
| 88 | { |
Dan Williams | b5ebc8e | 2016-03-06 15:20:51 -0800 | [diff] [blame] | 89 | int rc = 0; |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 90 | bool bad_pmem = false; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 91 | void *mem = kmap_atomic(page); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 92 | phys_addr_t pmem_off = sector * 512 + pmem->data_offset; |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 93 | void __pmem *pmem_addr = pmem->virt_addr + pmem_off; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 94 | |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 95 | if (unlikely(is_bad_pmem(&pmem->bb, sector, len))) |
| 96 | bad_pmem = true; |
| 97 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 98 | if (rw == READ) { |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 99 | if (unlikely(bad_pmem)) |
Dan Williams | b5ebc8e | 2016-03-06 15:20:51 -0800 | [diff] [blame] | 100 | rc = -EIO; |
| 101 | else { |
Dan Williams | fc0c202 | 2016-03-08 10:30:19 -0800 | [diff] [blame] | 102 | rc = memcpy_from_pmem(mem + off, pmem_addr, len); |
Dan Williams | b5ebc8e | 2016-03-06 15:20:51 -0800 | [diff] [blame] | 103 | flush_dcache_page(page); |
| 104 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 105 | } else { |
Dan Williams | 0a370d26 | 2016-04-14 19:40:47 -0700 | [diff] [blame] | 106 | /* |
| 107 | * Note that we write the data both before and after |
| 108 | * clearing poison. The write before clear poison |
| 109 | * handles situations where the latest written data is |
| 110 | * preserved and the clear poison operation simply marks |
| 111 | * the address range as valid without changing the data. |
| 112 | * In this case application software can assume that an |
| 113 | * interrupted write will either return the new good |
| 114 | * data or an error. |
| 115 | * |
| 116 | * However, if pmem_clear_poison() leaves the data in an |
| 117 | * indeterminate state we need to perform the write |
| 118 | * after clear poison. |
| 119 | */ |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 120 | flush_dcache_page(page); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 121 | memcpy_to_pmem(pmem_addr, mem + off, len); |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 122 | if (unlikely(bad_pmem)) { |
| 123 | pmem_clear_poison(pmem, pmem_off, len); |
| 124 | memcpy_to_pmem(pmem_addr, mem + off, len); |
| 125 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | kunmap_atomic(mem); |
Dan Williams | b5ebc8e | 2016-03-06 15:20:51 -0800 | [diff] [blame] | 129 | return rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 132 | 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] | 133 | { |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 134 | int rc = 0; |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 135 | bool do_acct; |
| 136 | unsigned long start; |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame] | 137 | struct bio_vec bvec; |
| 138 | struct bvec_iter iter; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 139 | struct block_device *bdev = bio->bi_bdev; |
| 140 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 141 | |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 142 | do_acct = nd_iostat_start(bio, &start); |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 143 | bio_for_each_segment(bvec, bio, iter) { |
| 144 | rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, |
| 145 | bvec.bv_offset, bio_data_dir(bio), |
| 146 | iter.bi_sector); |
| 147 | if (rc) { |
| 148 | bio->bi_error = rc; |
| 149 | break; |
| 150 | } |
| 151 | } |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 152 | if (do_acct) |
| 153 | nd_iostat_end(bio, start); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 154 | |
| 155 | if (bio_data_dir(bio)) |
| 156 | wmb_pmem(); |
| 157 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 158 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 159 | return BLK_QC_T_NONE; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int pmem_rw_page(struct block_device *bdev, sector_t sector, |
| 163 | struct page *page, int rw) |
| 164 | { |
| 165 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 166 | int rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 167 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 168 | rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, rw, sector); |
Ross Zwisler | ba8fe0f | 2015-09-16 14:52:21 -0600 | [diff] [blame] | 169 | if (rw & WRITE) |
| 170 | wmb_pmem(); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 171 | |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 172 | /* |
| 173 | * The ->rw_page interface is subtle and tricky. The core |
| 174 | * retries on any error, so we can only invoke page_endio() in |
| 175 | * the successful completion case. Otherwise, we'll see crashes |
| 176 | * caused by double completion. |
| 177 | */ |
| 178 | if (rc == 0) |
| 179 | page_endio(page, rw & WRITE, 0); |
| 180 | |
| 181 | return rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static long pmem_direct_access(struct block_device *bdev, sector_t sector, |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 185 | void __pmem **kaddr, pfn_t *pfn) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 186 | { |
| 187 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 188 | resource_size_t offset = sector * 512 + pmem->data_offset; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 189 | |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 190 | *kaddr = pmem->virt_addr + offset; |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 191 | *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 192 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 193 | return pmem->size - pmem->pfn_pad - offset; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static const struct block_device_operations pmem_fops = { |
| 197 | .owner = THIS_MODULE, |
| 198 | .rw_page = pmem_rw_page, |
| 199 | .direct_access = pmem_direct_access, |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 200 | .revalidate_disk = nvdimm_revalidate_disk, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 201 | }; |
| 202 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 203 | static struct pmem_device *pmem_alloc(struct device *dev, |
| 204 | struct resource *res, int id) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 205 | { |
| 206 | struct pmem_device *pmem; |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 207 | struct request_queue *q; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 208 | |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 209 | pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 210 | if (!pmem) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 211 | return ERR_PTR(-ENOMEM); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 212 | |
| 213 | pmem->phys_addr = res->start; |
| 214 | pmem->size = resource_size(res); |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 215 | if (!arch_has_wmb_pmem()) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 216 | dev_warn(dev, "unable to guarantee persistence of writes\n"); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 217 | |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 218 | if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size, |
| 219 | dev_name(dev))) { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 220 | dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n", |
| 221 | &pmem->phys_addr, pmem->size); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 222 | return ERR_PTR(-EBUSY); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 225 | q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev)); |
| 226 | if (!q) |
| 227 | return ERR_PTR(-ENOMEM); |
| 228 | |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 229 | pmem->pfn_flags = PFN_DEV; |
| 230 | if (pmem_should_map_pages(dev)) { |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 231 | pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, res, |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 232 | &q->q_usage_counter, NULL); |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 233 | pmem->pfn_flags |= PFN_MAP; |
| 234 | } else |
Dan Williams | a639315 | 2015-09-15 02:14:03 -0400 | [diff] [blame] | 235 | pmem->virt_addr = (void __pmem *) devm_memremap(dev, |
| 236 | pmem->phys_addr, pmem->size, |
| 237 | ARCH_MEMREMAP_PMEM); |
Dan Williams | b36f476 | 2015-09-15 02:42:20 -0400 | [diff] [blame] | 238 | |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 239 | if (IS_ERR(pmem->virt_addr)) { |
| 240 | blk_cleanup_queue(q); |
Dan Williams | b36f476 | 2015-09-15 02:42:20 -0400 | [diff] [blame] | 241 | return (void __force *) pmem->virt_addr; |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 242 | } |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 243 | |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 244 | pmem->pmem_queue = q; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 245 | return pmem; |
| 246 | } |
| 247 | |
| 248 | static void pmem_detach_disk(struct pmem_device *pmem) |
| 249 | { |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 250 | if (!pmem->pmem_disk) |
| 251 | return; |
| 252 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 253 | del_gendisk(pmem->pmem_disk); |
| 254 | put_disk(pmem->pmem_disk); |
| 255 | blk_cleanup_queue(pmem->pmem_queue); |
| 256 | } |
| 257 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 258 | static int pmem_attach_disk(struct device *dev, |
| 259 | struct nd_namespace_common *ndns, struct pmem_device *pmem) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 260 | { |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 261 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 262 | int nid = dev_to_node(dev); |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 263 | struct resource bb_res; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 264 | struct gendisk *disk; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 265 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 266 | blk_queue_make_request(pmem->pmem_queue, pmem_make_request); |
Vishal Verma | 6b47496 | 2015-07-23 11:58:48 -0600 | [diff] [blame] | 267 | blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE); |
Dan Williams | 43d3fa3 | 2015-05-16 12:28:50 -0400 | [diff] [blame] | 268 | blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 269 | blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY); |
Dan Williams | 0f51c4f | 2015-05-16 12:28:54 -0400 | [diff] [blame] | 270 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 271 | |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 272 | disk = alloc_disk_node(0, nid); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 273 | if (!disk) { |
| 274 | blk_cleanup_queue(pmem->pmem_queue); |
| 275 | return -ENOMEM; |
| 276 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 277 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 278 | disk->fops = &pmem_fops; |
| 279 | disk->private_data = pmem; |
| 280 | disk->queue = pmem->pmem_queue; |
| 281 | disk->flags = GENHD_FL_EXT_DEVT; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 282 | nvdimm_namespace_disk_name(ndns, disk->disk_name); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 283 | disk->driverfs_dev = dev; |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 284 | set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset) |
| 285 | / 512); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 286 | pmem->pmem_disk = disk; |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 287 | devm_exit_badblocks(dev, &pmem->bb); |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 288 | if (devm_init_badblocks(dev, &pmem->bb)) |
| 289 | return -ENOMEM; |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 290 | bb_res.start = nsio->res.start + pmem->data_offset; |
| 291 | bb_res.end = nsio->res.end; |
| 292 | if (is_nd_pfn(dev)) { |
| 293 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 294 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 295 | |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 296 | bb_res.start += __le32_to_cpu(pfn_sb->start_pad); |
| 297 | bb_res.end -= __le32_to_cpu(pfn_sb->end_trunc); |
| 298 | } |
| 299 | nvdimm_badblocks_populate(to_nd_region(dev->parent), &pmem->bb, |
| 300 | &bb_res); |
Dan Williams | 57f7f31 | 2016-01-06 12:03:42 -0800 | [diff] [blame] | 301 | disk->bb = &pmem->bb; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 302 | add_disk(disk); |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 303 | revalidate_disk(disk); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 304 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 305 | return 0; |
| 306 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 307 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 308 | static int pmem_rw_bytes(struct nd_namespace_common *ndns, |
| 309 | resource_size_t offset, void *buf, size_t size, int rw) |
| 310 | { |
| 311 | struct pmem_device *pmem = dev_get_drvdata(ndns->claim); |
| 312 | |
| 313 | if (unlikely(offset + size > pmem->size)) { |
| 314 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); |
| 315 | return -EFAULT; |
| 316 | } |
| 317 | |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 318 | if (rw == READ) { |
| 319 | unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512); |
| 320 | |
| 321 | if (unlikely(is_bad_pmem(&pmem->bb, offset / 512, sz_align))) |
| 322 | return -EIO; |
Dan Williams | fc0c202 | 2016-03-08 10:30:19 -0800 | [diff] [blame] | 323 | return memcpy_from_pmem(buf, pmem->virt_addr + offset, size); |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 324 | } else { |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 325 | memcpy_to_pmem(pmem->virt_addr + offset, buf, size); |
| 326 | wmb_pmem(); |
| 327 | } |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 332 | static int nd_pfn_init(struct nd_pfn *nd_pfn) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 333 | { |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 334 | struct nd_pfn_sb *pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL); |
| 335 | struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev); |
| 336 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 337 | u32 start_pad = 0, end_trunc = 0; |
| 338 | resource_size_t start, size; |
| 339 | struct nd_namespace_io *nsio; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 340 | struct nd_region *nd_region; |
| 341 | unsigned long npfns; |
| 342 | phys_addr_t offset; |
| 343 | u64 checksum; |
| 344 | int rc; |
| 345 | |
| 346 | if (!pfn_sb) |
| 347 | return -ENOMEM; |
| 348 | |
| 349 | nd_pfn->pfn_sb = pfn_sb; |
| 350 | rc = nd_pfn_validate(nd_pfn); |
Dan Williams | 3fa9626 | 2015-12-13 11:35:52 -0800 | [diff] [blame] | 351 | if (rc == -ENODEV) |
| 352 | /* no info block, do init */; |
| 353 | else |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 354 | return rc; |
| 355 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 356 | nd_region = to_nd_region(nd_pfn->dev.parent); |
| 357 | if (nd_region->ro) { |
| 358 | dev_info(&nd_pfn->dev, |
| 359 | "%s is read-only, unable to init metadata\n", |
| 360 | dev_name(&nd_region->dev)); |
| 361 | goto err; |
| 362 | } |
| 363 | |
| 364 | memset(pfn_sb, 0, sizeof(*pfn_sb)); |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * Check if pmem collides with 'System RAM' when section aligned and |
| 368 | * trim it accordingly |
| 369 | */ |
| 370 | nsio = to_nd_namespace_io(&ndns->dev); |
| 371 | start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start); |
| 372 | size = resource_size(&nsio->res); |
| 373 | if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, |
| 374 | IORES_DESC_NONE) == REGION_MIXED) { |
| 375 | |
| 376 | start = nsio->res.start; |
| 377 | start_pad = PHYS_SECTION_ALIGN_UP(start) - start; |
| 378 | } |
| 379 | |
| 380 | start = nsio->res.start; |
| 381 | size = PHYS_SECTION_ALIGN_UP(start + size) - start; |
| 382 | if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, |
| 383 | IORES_DESC_NONE) == REGION_MIXED) { |
| 384 | size = resource_size(&nsio->res); |
| 385 | end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size); |
| 386 | } |
| 387 | |
| 388 | if (start_pad + end_trunc) |
| 389 | dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n", |
| 390 | dev_name(&ndns->dev), start_pad + end_trunc); |
| 391 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 392 | /* |
| 393 | * Note, we use 64 here for the standard size of struct page, |
| 394 | * debugging options may cause it to be larger in which case the |
| 395 | * implementation will limit the pfns advertised through |
| 396 | * ->direct_access() to those that are included in the memmap. |
| 397 | */ |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 398 | start += start_pad; |
| 399 | npfns = (pmem->size - start_pad - end_trunc - SZ_8K) / SZ_4K; |
Dan Williams | 658922e | 2016-04-30 13:07:06 -0700 | [diff] [blame] | 400 | if (nd_pfn->mode == PFN_MODE_PMEM) { |
| 401 | unsigned long memmap_size; |
| 402 | |
| 403 | /* |
| 404 | * vmemmap_populate_hugepages() allocates the memmap array in |
Dan Williams | 1b8d2af | 2016-05-06 10:20:10 -0700 | [diff] [blame] | 405 | * PMD_SIZE chunks. |
Dan Williams | 658922e | 2016-04-30 13:07:06 -0700 | [diff] [blame] | 406 | */ |
Dan Williams | 1b8d2af | 2016-05-06 10:20:10 -0700 | [diff] [blame] | 407 | memmap_size = ALIGN(64 * npfns, PMD_SIZE); |
Dan Williams | 658922e | 2016-04-30 13:07:06 -0700 | [diff] [blame] | 408 | offset = ALIGN(start + SZ_8K + memmap_size, nd_pfn->align) |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 409 | - start; |
Dan Williams | 658922e | 2016-04-30 13:07:06 -0700 | [diff] [blame] | 410 | } else if (nd_pfn->mode == PFN_MODE_RAM) |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 411 | offset = ALIGN(start + SZ_8K, nd_pfn->align) - start; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 412 | else |
| 413 | goto err; |
| 414 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 415 | if (offset + start_pad + end_trunc >= pmem->size) { |
| 416 | dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n", |
| 417 | dev_name(&ndns->dev)); |
| 418 | goto err; |
| 419 | } |
| 420 | |
| 421 | npfns = (pmem->size - offset - start_pad - end_trunc) / SZ_4K; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 422 | pfn_sb->mode = cpu_to_le32(nd_pfn->mode); |
| 423 | pfn_sb->dataoff = cpu_to_le64(offset); |
| 424 | pfn_sb->npfns = cpu_to_le64(npfns); |
| 425 | memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN); |
| 426 | memcpy(pfn_sb->uuid, nd_pfn->uuid, 16); |
Dan Williams | a34d5e8 | 2015-12-12 16:09:14 -0800 | [diff] [blame] | 427 | memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 428 | pfn_sb->version_major = cpu_to_le16(1); |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 429 | pfn_sb->version_minor = cpu_to_le16(1); |
| 430 | pfn_sb->start_pad = cpu_to_le32(start_pad); |
| 431 | pfn_sb->end_trunc = cpu_to_le32(end_trunc); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 432 | checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb); |
| 433 | pfn_sb->checksum = cpu_to_le64(checksum); |
| 434 | |
| 435 | rc = nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)); |
| 436 | if (rc) |
| 437 | goto err; |
| 438 | |
| 439 | return 0; |
| 440 | err: |
| 441 | nd_pfn->pfn_sb = NULL; |
| 442 | kfree(pfn_sb); |
| 443 | return -ENXIO; |
| 444 | } |
| 445 | |
| 446 | static int nvdimm_namespace_detach_pfn(struct nd_namespace_common *ndns) |
| 447 | { |
| 448 | struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim); |
| 449 | struct pmem_device *pmem; |
| 450 | |
| 451 | /* free pmem disk */ |
| 452 | pmem = dev_get_drvdata(&nd_pfn->dev); |
| 453 | pmem_detach_disk(pmem); |
| 454 | |
| 455 | /* release nd_pfn resources */ |
| 456 | kfree(nd_pfn->pfn_sb); |
| 457 | nd_pfn->pfn_sb = NULL; |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Dan Williams | d9cbe09 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 462 | /* |
| 463 | * We hotplug memory at section granularity, pad the reserved area from |
| 464 | * the previous section base to the namespace base address. |
| 465 | */ |
| 466 | static unsigned long init_altmap_base(resource_size_t base) |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 467 | { |
Dan Williams | 45f6880 | 2016-03-06 08:04:12 -0800 | [diff] [blame] | 468 | unsigned long base_pfn = PHYS_PFN(base); |
Dan Williams | d9cbe09 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 469 | |
| 470 | return PFN_SECTION_ALIGN_DOWN(base_pfn); |
| 471 | } |
| 472 | |
| 473 | static unsigned long init_altmap_reserve(resource_size_t base) |
| 474 | { |
Dan Williams | 45f6880 | 2016-03-06 08:04:12 -0800 | [diff] [blame] | 475 | unsigned long reserve = PHYS_PFN(SZ_8K); |
| 476 | unsigned long base_pfn = PHYS_PFN(base); |
Dan Williams | d9cbe09 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 477 | |
| 478 | reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn); |
| 479 | return reserve; |
| 480 | } |
| 481 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 482 | static int __nvdimm_namespace_attach_pfn(struct nd_pfn *nd_pfn) |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 483 | { |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 484 | int rc; |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 485 | struct resource res; |
| 486 | struct request_queue *q; |
| 487 | struct pmem_device *pmem; |
| 488 | struct vmem_altmap *altmap; |
| 489 | struct device *dev = &nd_pfn->dev; |
| 490 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 491 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 492 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); |
| 493 | u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); |
| 494 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 495 | resource_size_t base = nsio->res.start + start_pad; |
Dan Williams | d2c0f04 | 2016-01-15 16:56:26 -0800 | [diff] [blame] | 496 | struct vmem_altmap __altmap = { |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 497 | .base_pfn = init_altmap_base(base), |
| 498 | .reserve = init_altmap_reserve(base), |
Dan Williams | d2c0f04 | 2016-01-15 16:56:26 -0800 | [diff] [blame] | 499 | }; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 500 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 501 | pmem = dev_get_drvdata(dev); |
| 502 | pmem->data_offset = le64_to_cpu(pfn_sb->dataoff); |
| 503 | pmem->pfn_pad = start_pad + end_trunc; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 504 | nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode); |
| 505 | if (nd_pfn->mode == PFN_MODE_RAM) { |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 506 | if (pmem->data_offset < SZ_8K) |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 507 | return -EINVAL; |
| 508 | nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns); |
| 509 | altmap = NULL; |
Dan Williams | d2c0f04 | 2016-01-15 16:56:26 -0800 | [diff] [blame] | 510 | } else if (nd_pfn->mode == PFN_MODE_PMEM) { |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 511 | nd_pfn->npfns = (pmem->size - pmem->pfn_pad - pmem->data_offset) |
Dan Williams | d2c0f04 | 2016-01-15 16:56:26 -0800 | [diff] [blame] | 512 | / PAGE_SIZE; |
| 513 | if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns) |
| 514 | dev_info(&nd_pfn->dev, |
| 515 | "number of pfns truncated from %lld to %ld\n", |
| 516 | le64_to_cpu(nd_pfn->pfn_sb->npfns), |
| 517 | nd_pfn->npfns); |
| 518 | altmap = & __altmap; |
Dan Williams | 45f6880 | 2016-03-06 08:04:12 -0800 | [diff] [blame] | 519 | altmap->free = PHYS_PFN(pmem->data_offset - SZ_8K); |
Dan Williams | d2c0f04 | 2016-01-15 16:56:26 -0800 | [diff] [blame] | 520 | altmap->alloc = 0; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 521 | } else { |
| 522 | rc = -ENXIO; |
| 523 | goto err; |
| 524 | } |
| 525 | |
| 526 | /* establish pfn range for lookup, and switch to direct map */ |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 527 | q = pmem->pmem_queue; |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 528 | memcpy(&res, &nsio->res, sizeof(res)); |
| 529 | res.start += start_pad; |
| 530 | res.end -= end_trunc; |
Dan Williams | a639315 | 2015-09-15 02:14:03 -0400 | [diff] [blame] | 531 | devm_memunmap(dev, (void __force *) pmem->virt_addr); |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 532 | pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, &res, |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 533 | &q->q_usage_counter, altmap); |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 534 | pmem->pfn_flags |= PFN_MAP; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 535 | if (IS_ERR(pmem->virt_addr)) { |
| 536 | rc = PTR_ERR(pmem->virt_addr); |
| 537 | goto err; |
| 538 | } |
| 539 | |
| 540 | /* attach pmem disk in "pfn-mode" */ |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 541 | rc = pmem_attach_disk(dev, ndns, pmem); |
| 542 | if (rc) |
| 543 | goto err; |
| 544 | |
| 545 | return rc; |
| 546 | err: |
| 547 | nvdimm_namespace_detach_pfn(ndns); |
| 548 | return rc; |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 549 | |
| 550 | } |
| 551 | |
| 552 | static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns) |
| 553 | { |
| 554 | struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim); |
| 555 | int rc; |
| 556 | |
| 557 | if (!nd_pfn->uuid || !nd_pfn->ndns) |
| 558 | return -ENODEV; |
| 559 | |
| 560 | rc = nd_pfn_init(nd_pfn); |
| 561 | if (rc) |
| 562 | return rc; |
| 563 | /* we need a valid pfn_sb before we can init a vmem_altmap */ |
| 564 | return __nvdimm_namespace_attach_pfn(nd_pfn); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 567 | static int nd_pmem_probe(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 568 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 569 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 570 | struct nd_namespace_common *ndns; |
| 571 | struct nd_namespace_io *nsio; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 572 | struct pmem_device *pmem; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 573 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 574 | ndns = nvdimm_namespace_common_probe(dev); |
| 575 | if (IS_ERR(ndns)) |
| 576 | return PTR_ERR(ndns); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 577 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 578 | nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 579 | pmem = pmem_alloc(dev, &nsio->res, nd_region->id); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 580 | if (IS_ERR(pmem)) |
| 581 | return PTR_ERR(pmem); |
| 582 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 583 | pmem->ndns = ndns; |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 584 | dev_set_drvdata(dev, pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 585 | ndns->rw_bytes = pmem_rw_bytes; |
Dan Williams | 710d69c | 2016-01-04 23:31:24 -0800 | [diff] [blame] | 586 | if (devm_init_badblocks(dev, &pmem->bb)) |
| 587 | return -ENOMEM; |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 588 | nvdimm_badblocks_populate(nd_region, &pmem->bb, &nsio->res); |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 589 | |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 590 | if (is_nd_btt(dev)) { |
| 591 | /* btt allocates its own request_queue */ |
| 592 | blk_cleanup_queue(pmem->pmem_queue); |
| 593 | pmem->pmem_queue = NULL; |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 594 | return nvdimm_namespace_attach_btt(ndns); |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 595 | } |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 596 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 597 | if (is_nd_pfn(dev)) |
| 598 | return nvdimm_namespace_attach_pfn(ndns); |
| 599 | |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 600 | if (nd_btt_probe(ndns, pmem) == 0 || nd_pfn_probe(ndns, pmem) == 0) { |
| 601 | /* |
| 602 | * We'll come back as either btt-pmem, or pfn-pmem, so |
| 603 | * drop the queue allocation for now. |
| 604 | */ |
| 605 | blk_cleanup_queue(pmem->pmem_queue); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 606 | return -ENXIO; |
| 607 | } |
| 608 | |
| 609 | return pmem_attach_disk(dev, ndns, pmem); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 610 | } |
| 611 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 612 | static int nd_pmem_remove(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 613 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 614 | struct pmem_device *pmem = dev_get_drvdata(dev); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 615 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 616 | if (is_nd_btt(dev)) |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 617 | nvdimm_namespace_detach_btt(pmem->ndns); |
| 618 | else if (is_nd_pfn(dev)) |
| 619 | nvdimm_namespace_detach_pfn(pmem->ndns); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 620 | else |
| 621 | pmem_detach_disk(pmem); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 622 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 626 | static void nd_pmem_notify(struct device *dev, enum nvdimm_event event) |
| 627 | { |
| 628 | struct pmem_device *pmem = dev_get_drvdata(dev); |
| 629 | struct nd_namespace_common *ndns = pmem->ndns; |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 630 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 631 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 632 | struct resource res = { |
| 633 | .start = nsio->res.start + pmem->data_offset, |
| 634 | .end = nsio->res.end, |
| 635 | }; |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 636 | |
| 637 | if (event != NVDIMM_REVALIDATE_POISON) |
| 638 | return; |
| 639 | |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 640 | if (is_nd_pfn(dev)) { |
| 641 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 642 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 643 | |
| 644 | res.start += __le32_to_cpu(pfn_sb->start_pad); |
| 645 | res.end -= __le32_to_cpu(pfn_sb->end_trunc); |
| 646 | } |
| 647 | |
| 648 | nvdimm_badblocks_populate(nd_region, &pmem->bb, &res); |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 651 | MODULE_ALIAS("pmem"); |
| 652 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 653 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 654 | static struct nd_device_driver nd_pmem_driver = { |
| 655 | .probe = nd_pmem_probe, |
| 656 | .remove = nd_pmem_remove, |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 657 | .notify = nd_pmem_notify, |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 658 | .drv = { |
| 659 | .name = "nd_pmem", |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 660 | }, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 661 | .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | static int __init pmem_init(void) |
| 665 | { |
NeilBrown | 5515529 | 2016-03-09 09:21:54 +1100 | [diff] [blame] | 666 | return nd_driver_register(&nd_pmem_driver); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 667 | } |
| 668 | module_init(pmem_init); |
| 669 | |
| 670 | static void pmem_exit(void) |
| 671 | { |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 672 | driver_unregister(&nd_pmem_driver.drv); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 673 | } |
| 674 | module_exit(pmem_exit); |
| 675 | |
| 676 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); |
| 677 | MODULE_LICENSE("GPL v2"); |