Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Persistent Memory Driver |
| 4 | * |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 5 | * Copyright (c) 2014-2015, Intel Corporation. |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>. |
| 7 | * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>. |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/hdreg.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/platform_device.h> |
Dan Williams | c953cc9 | 2018-07-13 21:50:37 -0700 | [diff] [blame] | 14 | #include <linux/set_memory.h> |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 17 | #include <linux/badblocks.h> |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 18 | #include <linux/memremap.h> |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 19 | #include <linux/vmalloc.h> |
Dan Williams | 7138970 | 2017-04-28 10:23:37 -0700 | [diff] [blame] | 20 | #include <linux/blk-mq.h> |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 21 | #include <linux/pfn_t.h> |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 22 | #include <linux/slab.h> |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 23 | #include <linux/uio.h> |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 24 | #include <linux/dax.h> |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 25 | #include <linux/nd.h> |
Minchan Kim | 23c47d2 | 2017-11-15 17:33:00 -0800 | [diff] [blame] | 26 | #include <linux/backing-dev.h> |
Christoph Hellwig | e0cf615 | 2020-06-07 21:41:42 -0700 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <asm/cacheflush.h> |
Dan Williams | f295e53 | 2016-06-17 11:08:06 -0700 | [diff] [blame] | 29 | #include "pmem.h" |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 30 | #include "pfn.h" |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 31 | #include "nd.h" |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 32 | |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 33 | static struct device *to_dev(struct pmem_device *pmem) |
| 34 | { |
| 35 | /* |
| 36 | * nvdimm bus services need a 'dev' parameter, and we record the device |
| 37 | * at init in bb.dev. |
| 38 | */ |
| 39 | return pmem->bb.dev; |
| 40 | } |
| 41 | |
| 42 | static struct nd_region *to_region(struct pmem_device *pmem) |
| 43 | { |
| 44 | return to_nd_region(to_dev(pmem)->parent); |
| 45 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 46 | |
Dan Williams | c953cc9 | 2018-07-13 21:50:37 -0700 | [diff] [blame] | 47 | static void hwpoison_clear(struct pmem_device *pmem, |
| 48 | phys_addr_t phys, unsigned int len) |
| 49 | { |
| 50 | unsigned long pfn_start, pfn_end, pfn; |
| 51 | |
| 52 | /* only pmem in the linear map supports HWPoison */ |
| 53 | if (is_vmalloc_addr(pmem->virt_addr)) |
| 54 | return; |
| 55 | |
| 56 | pfn_start = PHYS_PFN(phys); |
| 57 | pfn_end = pfn_start + PHYS_PFN(len); |
| 58 | for (pfn = pfn_start; pfn < pfn_end; pfn++) { |
| 59 | struct page *page = pfn_to_page(pfn); |
| 60 | |
| 61 | /* |
| 62 | * Note, no need to hold a get_dev_pagemap() reference |
| 63 | * here since we're in the driver I/O path and |
| 64 | * outstanding I/O requests pin the dev_pagemap. |
| 65 | */ |
| 66 | if (test_and_clear_pmem_poison(page)) |
| 67 | clear_mce_nospec(pfn); |
| 68 | } |
| 69 | } |
| 70 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 71 | static blk_status_t pmem_clear_poison(struct pmem_device *pmem, |
| 72 | phys_addr_t offset, unsigned int len) |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 73 | { |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 74 | struct device *dev = to_dev(pmem); |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 75 | sector_t sector; |
| 76 | long cleared; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 77 | blk_status_t rc = BLK_STS_OK; |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 78 | |
| 79 | sector = (offset - pmem->data_offset) / 512; |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 80 | |
Dan Williams | 868f036f | 2016-12-16 08:10:31 -0800 | [diff] [blame] | 81 | cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len); |
| 82 | if (cleared < len) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 83 | rc = BLK_STS_IOERR; |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 84 | if (cleared > 0 && cleared / 512) { |
Dan Williams | c953cc9 | 2018-07-13 21:50:37 -0700 | [diff] [blame] | 85 | hwpoison_clear(pmem, pmem->phys_addr + offset, cleared); |
Dan Williams | 868f036f | 2016-12-16 08:10:31 -0800 | [diff] [blame] | 86 | cleared /= 512; |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 87 | dev_dbg(dev, "%#llx clear %ld sector%s\n", |
Dan Williams | 868f036f | 2016-12-16 08:10:31 -0800 | [diff] [blame] | 88 | (unsigned long long) sector, cleared, |
| 89 | cleared > 1 ? "s" : ""); |
Fabian Frederick | 0a3f27b | 2016-12-04 10:48:58 -0800 | [diff] [blame] | 90 | badblocks_clear(&pmem->bb, sector, cleared); |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 91 | if (pmem->bb_state) |
| 92 | sysfs_notify_dirent(pmem->bb_state); |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 93 | } |
Toshi Kani | 3115bb0 | 2016-10-13 09:54:21 -0600 | [diff] [blame] | 94 | |
Dan Williams | f2b6125 | 2017-05-29 23:00:34 -0700 | [diff] [blame] | 95 | arch_invalidate_pmem(pmem->virt_addr + offset, len); |
Dan Williams | 868f036f | 2016-12-16 08:10:31 -0800 | [diff] [blame] | 96 | |
| 97 | return rc; |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Vishal Verma | bd697a8 | 2016-09-30 17:19:30 -0600 | [diff] [blame] | 100 | static void write_pmem(void *pmem_addr, struct page *page, |
| 101 | unsigned int off, unsigned int len) |
| 102 | { |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 103 | unsigned int chunk; |
| 104 | void *mem; |
Vishal Verma | bd697a8 | 2016-09-30 17:19:30 -0600 | [diff] [blame] | 105 | |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 106 | while (len) { |
| 107 | mem = kmap_atomic(page); |
Li RongQing | 9dc6488 | 2019-04-04 10:58:01 +0800 | [diff] [blame] | 108 | chunk = min_t(unsigned int, len, PAGE_SIZE - off); |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 109 | memcpy_flushcache(pmem_addr, mem + off, chunk); |
| 110 | kunmap_atomic(mem); |
| 111 | len -= chunk; |
| 112 | off = 0; |
| 113 | page++; |
Li RongQing | 9dc6488 | 2019-04-04 10:58:01 +0800 | [diff] [blame] | 114 | pmem_addr += chunk; |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 115 | } |
Vishal Verma | bd697a8 | 2016-09-30 17:19:30 -0600 | [diff] [blame] | 116 | } |
| 117 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 118 | static blk_status_t read_pmem(struct page *page, unsigned int off, |
Vishal Verma | bd697a8 | 2016-09-30 17:19:30 -0600 | [diff] [blame] | 119 | void *pmem_addr, unsigned int len) |
| 120 | { |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 121 | unsigned int chunk; |
Dan Williams | 60622d6 | 2018-05-03 17:06:21 -0700 | [diff] [blame] | 122 | unsigned long rem; |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 123 | void *mem; |
Vishal Verma | bd697a8 | 2016-09-30 17:19:30 -0600 | [diff] [blame] | 124 | |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 125 | while (len) { |
| 126 | mem = kmap_atomic(page); |
Li RongQing | 9dc6488 | 2019-04-04 10:58:01 +0800 | [diff] [blame] | 127 | chunk = min_t(unsigned int, len, PAGE_SIZE - off); |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame^] | 128 | rem = copy_mc_to_kernel(mem + off, pmem_addr, chunk); |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 129 | kunmap_atomic(mem); |
Dan Williams | 60622d6 | 2018-05-03 17:06:21 -0700 | [diff] [blame] | 130 | if (rem) |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 131 | return BLK_STS_IOERR; |
| 132 | len -= chunk; |
| 133 | off = 0; |
| 134 | page++; |
Li RongQing | 9dc6488 | 2019-04-04 10:58:01 +0800 | [diff] [blame] | 135 | pmem_addr += chunk; |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame] | 136 | } |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 137 | return BLK_STS_OK; |
Vishal Verma | bd697a8 | 2016-09-30 17:19:30 -0600 | [diff] [blame] | 138 | } |
| 139 | |
Vivek Goyal | 5d64efe | 2020-02-28 11:34:51 -0500 | [diff] [blame] | 140 | static blk_status_t pmem_do_read(struct pmem_device *pmem, |
| 141 | struct page *page, unsigned int page_off, |
| 142 | sector_t sector, unsigned int len) |
| 143 | { |
| 144 | blk_status_t rc; |
| 145 | phys_addr_t pmem_off = sector * 512 + pmem->data_offset; |
| 146 | void *pmem_addr = pmem->virt_addr + pmem_off; |
| 147 | |
| 148 | if (unlikely(is_bad_pmem(&pmem->bb, sector, len))) |
| 149 | return BLK_STS_IOERR; |
| 150 | |
| 151 | rc = read_pmem(page, page_off, pmem_addr, len); |
| 152 | flush_dcache_page(page); |
| 153 | return rc; |
| 154 | } |
| 155 | |
| 156 | static blk_status_t pmem_do_write(struct pmem_device *pmem, |
| 157 | struct page *page, unsigned int page_off, |
| 158 | sector_t sector, unsigned int len) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 159 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 160 | blk_status_t rc = BLK_STS_OK; |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 161 | bool bad_pmem = false; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 162 | phys_addr_t pmem_off = sector * 512 + pmem->data_offset; |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 163 | void *pmem_addr = pmem->virt_addr + pmem_off; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 164 | |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 165 | if (unlikely(is_bad_pmem(&pmem->bb, sector, len))) |
| 166 | bad_pmem = true; |
| 167 | |
Vivek Goyal | 5d64efe | 2020-02-28 11:34:51 -0500 | [diff] [blame] | 168 | /* |
| 169 | * Note that we write the data both before and after |
| 170 | * clearing poison. The write before clear poison |
| 171 | * handles situations where the latest written data is |
| 172 | * preserved and the clear poison operation simply marks |
| 173 | * the address range as valid without changing the data. |
| 174 | * In this case application software can assume that an |
| 175 | * interrupted write will either return the new good |
| 176 | * data or an error. |
| 177 | * |
| 178 | * However, if pmem_clear_poison() leaves the data in an |
| 179 | * indeterminate state we need to perform the write |
| 180 | * after clear poison. |
| 181 | */ |
| 182 | flush_dcache_page(page); |
| 183 | write_pmem(pmem_addr, page, page_off, len); |
| 184 | if (unlikely(bad_pmem)) { |
| 185 | rc = pmem_clear_poison(pmem, pmem_off, len); |
| 186 | write_pmem(pmem_addr, page, page_off, len); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Dan Williams | b5ebc8e | 2016-03-06 15:20:51 -0800 | [diff] [blame] | 189 | return rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 192 | static blk_qc_t pmem_submit_bio(struct bio *bio) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 193 | { |
Pankaj Gupta | c5d4355 | 2019-07-05 19:33:22 +0530 | [diff] [blame] | 194 | int ret = 0; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 195 | blk_status_t rc = 0; |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 196 | bool do_acct; |
| 197 | unsigned long start; |
Dan Williams | edc870e | 2015-05-16 12:28:51 -0400 | [diff] [blame] | 198 | struct bio_vec bvec; |
| 199 | struct bvec_iter iter; |
Christoph Hellwig | 6ec26b8 | 2020-05-08 18:15:17 +0200 | [diff] [blame] | 200 | struct pmem_device *pmem = bio->bi_disk->private_data; |
Dan Williams | 7e267a8 | 2016-06-01 20:48:15 -0700 | [diff] [blame] | 201 | struct nd_region *nd_region = to_region(pmem); |
| 202 | |
Ross Zwisler | d2d6364 | 2018-06-06 10:45:12 -0600 | [diff] [blame] | 203 | if (bio->bi_opf & REQ_PREFLUSH) |
Pankaj Gupta | c5d4355 | 2019-07-05 19:33:22 +0530 | [diff] [blame] | 204 | ret = nvdimm_flush(nd_region, bio); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 205 | |
Christoph Hellwig | 0fd92f8 | 2020-05-27 07:24:10 +0200 | [diff] [blame] | 206 | do_acct = blk_queue_io_stat(bio->bi_disk->queue); |
| 207 | if (do_acct) |
| 208 | start = bio_start_io_acct(bio); |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 209 | bio_for_each_segment(bvec, bio, iter) { |
Vivek Goyal | 5d64efe | 2020-02-28 11:34:51 -0500 | [diff] [blame] | 210 | if (op_is_write(bio_op(bio))) |
| 211 | rc = pmem_do_write(pmem, bvec.bv_page, bvec.bv_offset, |
| 212 | iter.bi_sector, bvec.bv_len); |
| 213 | else |
| 214 | rc = pmem_do_read(pmem, bvec.bv_page, bvec.bv_offset, |
| 215 | iter.bi_sector, bvec.bv_len); |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 216 | if (rc) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 217 | bio->bi_status = rc; |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | } |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 221 | if (do_acct) |
Christoph Hellwig | 0fd92f8 | 2020-05-27 07:24:10 +0200 | [diff] [blame] | 222 | bio_end_io_acct(bio, start); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 223 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 224 | if (bio->bi_opf & REQ_FUA) |
Pankaj Gupta | c5d4355 | 2019-07-05 19:33:22 +0530 | [diff] [blame] | 225 | ret = nvdimm_flush(nd_region, bio); |
| 226 | |
| 227 | if (ret) |
| 228 | bio->bi_status = errno_to_blk_status(ret); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 229 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 230 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 231 | return BLK_QC_T_NONE; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int pmem_rw_page(struct block_device *bdev, sector_t sector, |
Tejun Heo | 3f289dc | 2018-07-18 04:47:36 -0700 | [diff] [blame] | 235 | struct page *page, unsigned int op) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 236 | { |
Christoph Hellwig | 6ec26b8 | 2020-05-08 18:15:17 +0200 | [diff] [blame] | 237 | struct pmem_device *pmem = bdev->bd_disk->private_data; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 238 | blk_status_t rc; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 239 | |
Vivek Goyal | 5d64efe | 2020-02-28 11:34:51 -0500 | [diff] [blame] | 240 | if (op_is_write(op)) |
Matthew Wilcox (Oracle) | af3bbc1 | 2020-08-14 17:30:33 -0700 | [diff] [blame] | 241 | rc = pmem_do_write(pmem, page, 0, sector, thp_size(page)); |
Vivek Goyal | 5d64efe | 2020-02-28 11:34:51 -0500 | [diff] [blame] | 242 | else |
Matthew Wilcox (Oracle) | af3bbc1 | 2020-08-14 17:30:33 -0700 | [diff] [blame] | 243 | rc = pmem_do_read(pmem, page, 0, sector, thp_size(page)); |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 244 | /* |
| 245 | * The ->rw_page interface is subtle and tricky. The core |
| 246 | * retries on any error, so we can only invoke page_endio() in |
| 247 | * the successful completion case. Otherwise, we'll see crashes |
| 248 | * caused by double completion. |
| 249 | */ |
| 250 | if (rc == 0) |
Tejun Heo | 3f289dc | 2018-07-18 04:47:36 -0700 | [diff] [blame] | 251 | page_endio(page, op_is_write(op), 0); |
Dan Williams | e10624f | 2016-01-06 12:03:41 -0800 | [diff] [blame] | 252 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 253 | return blk_status_to_errno(rc); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Dan Williams | f295e53 | 2016-06-17 11:08:06 -0700 | [diff] [blame] | 256 | /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */ |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 257 | __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff, |
| 258 | long nr_pages, void **kaddr, pfn_t *pfn) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 259 | { |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 260 | resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 261 | |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 262 | if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, |
| 263 | PFN_PHYS(nr_pages)))) |
Dan Williams | 0a70bd4 | 2016-02-24 14:02:11 -0800 | [diff] [blame] | 264 | return -EIO; |
Huaisheng Ye | 46a590c | 2018-07-30 15:15:43 +0800 | [diff] [blame] | 265 | |
| 266 | if (kaddr) |
| 267 | *kaddr = pmem->virt_addr + offset; |
| 268 | if (pfn) |
| 269 | *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 270 | |
Dan Williams | 0a70bd4 | 2016-02-24 14:02:11 -0800 | [diff] [blame] | 271 | /* |
| 272 | * If badblocks are present, limit known good range to the |
| 273 | * requested range. |
| 274 | */ |
| 275 | if (unlikely(pmem->bb.count)) |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 276 | return nr_pages; |
| 277 | return PHYS_PFN(pmem->size - pmem->pfn_pad - offset); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static const struct block_device_operations pmem_fops = { |
| 281 | .owner = THIS_MODULE, |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 282 | .submit_bio = pmem_submit_bio, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 283 | .rw_page = pmem_rw_page, |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 284 | .revalidate_disk = nvdimm_revalidate_disk, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 285 | }; |
| 286 | |
Vivek Goyal | f605a26 | 2020-02-28 11:34:52 -0500 | [diff] [blame] | 287 | static int pmem_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, |
| 288 | size_t nr_pages) |
| 289 | { |
| 290 | struct pmem_device *pmem = dax_get_private(dax_dev); |
| 291 | |
| 292 | return blk_status_to_errno(pmem_do_write(pmem, ZERO_PAGE(0), 0, |
| 293 | PFN_PHYS(pgoff) >> SECTOR_SHIFT, |
| 294 | PAGE_SIZE)); |
| 295 | } |
| 296 | |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 297 | static long pmem_dax_direct_access(struct dax_device *dax_dev, |
| 298 | pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn) |
| 299 | { |
| 300 | struct pmem_device *pmem = dax_get_private(dax_dev); |
| 301 | |
| 302 | return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn); |
| 303 | } |
| 304 | |
Dan Williams | 52f476a | 2019-05-16 17:05:21 -0700 | [diff] [blame] | 305 | /* |
| 306 | * Use the 'no check' versions of copy_from_iter_flushcache() and |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame^] | 307 | * copy_mc_to_iter() to bypass HARDENED_USERCOPY overhead. Bounds |
Dan Williams | 52f476a | 2019-05-16 17:05:21 -0700 | [diff] [blame] | 308 | * checking, both file offset and device offset, is handled by |
| 309 | * dax_iomap_actor() |
| 310 | */ |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 311 | static size_t pmem_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, |
| 312 | void *addr, size_t bytes, struct iov_iter *i) |
| 313 | { |
Dan Williams | 52f476a | 2019-05-16 17:05:21 -0700 | [diff] [blame] | 314 | return _copy_from_iter_flushcache(addr, bytes, i); |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Dan Williams | b3a9a0c | 2018-05-02 06:46:33 -0700 | [diff] [blame] | 317 | static size_t pmem_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, |
| 318 | void *addr, size_t bytes, struct iov_iter *i) |
| 319 | { |
Dan Williams | ec6347b | 2020-10-05 20:40:16 -0700 | [diff] [blame^] | 320 | return _copy_mc_to_iter(addr, bytes, i); |
Dan Williams | b3a9a0c | 2018-05-02 06:46:33 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 323 | static const struct dax_operations pmem_dax_ops = { |
| 324 | .direct_access = pmem_dax_direct_access, |
Dan Williams | 7bf7eac | 2019-05-16 13:26:29 -0700 | [diff] [blame] | 325 | .dax_supported = generic_fsdax_supported, |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 326 | .copy_from_iter = pmem_copy_from_iter, |
Dan Williams | b3a9a0c | 2018-05-02 06:46:33 -0700 | [diff] [blame] | 327 | .copy_to_iter = pmem_copy_to_iter, |
Vivek Goyal | f605a26 | 2020-02-28 11:34:52 -0500 | [diff] [blame] | 328 | .zero_page_range = pmem_dax_zero_page_range, |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 329 | }; |
| 330 | |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 331 | static const struct attribute_group *pmem_attribute_groups[] = { |
| 332 | &dax_attribute_group, |
| 333 | NULL, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 334 | }; |
| 335 | |
Christoph Hellwig | d8668bb | 2019-06-26 14:27:09 +0200 | [diff] [blame] | 336 | static void pmem_pagemap_cleanup(struct dev_pagemap *pgmap) |
Dan Williams | 030b99e | 2016-03-17 20:24:31 -0700 | [diff] [blame] | 337 | { |
Christoph Hellwig | d8668bb | 2019-06-26 14:27:09 +0200 | [diff] [blame] | 338 | struct request_queue *q = |
| 339 | container_of(pgmap->ref, struct request_queue, q_usage_counter); |
Dan Williams | 50f44ee | 2019-06-13 15:56:33 -0700 | [diff] [blame] | 340 | |
Dan Williams | 030b99e | 2016-03-17 20:24:31 -0700 | [diff] [blame] | 341 | blk_cleanup_queue(q); |
| 342 | } |
| 343 | |
Christoph Hellwig | d8668bb | 2019-06-26 14:27:09 +0200 | [diff] [blame] | 344 | static void pmem_release_queue(void *pgmap) |
Dan Williams | 50f44ee | 2019-06-13 15:56:33 -0700 | [diff] [blame] | 345 | { |
Christoph Hellwig | d8668bb | 2019-06-26 14:27:09 +0200 | [diff] [blame] | 346 | pmem_pagemap_cleanup(pgmap); |
Dan Williams | 50f44ee | 2019-06-13 15:56:33 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Christoph Hellwig | d8668bb | 2019-06-26 14:27:09 +0200 | [diff] [blame] | 349 | static void pmem_pagemap_kill(struct dev_pagemap *pgmap) |
Dan Williams | 7138970 | 2017-04-28 10:23:37 -0700 | [diff] [blame] | 350 | { |
Christoph Hellwig | d8668bb | 2019-06-26 14:27:09 +0200 | [diff] [blame] | 351 | struct request_queue *q = |
| 352 | container_of(pgmap->ref, struct request_queue, q_usage_counter); |
Dan Williams | a95c90f | 2018-12-28 00:34:57 -0800 | [diff] [blame] | 353 | |
Linus Torvalds | d3b5d35 | 2017-05-01 23:54:56 -0700 | [diff] [blame] | 354 | blk_freeze_queue_start(q); |
Dan Williams | 7138970 | 2017-04-28 10:23:37 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 357 | static void pmem_release_disk(void *__pmem) |
Dan Williams | 030b99e | 2016-03-17 20:24:31 -0700 | [diff] [blame] | 358 | { |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 359 | struct pmem_device *pmem = __pmem; |
| 360 | |
| 361 | kill_dax(pmem->dax_dev); |
| 362 | put_dax(pmem->dax_dev); |
| 363 | del_gendisk(pmem->disk); |
| 364 | put_disk(pmem->disk); |
Dan Williams | 030b99e | 2016-03-17 20:24:31 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Christoph Hellwig | 1e240e8 | 2019-06-26 14:27:08 +0200 | [diff] [blame] | 367 | static const struct dev_pagemap_ops fsdax_pagemap_ops = { |
Christoph Hellwig | 1e240e8 | 2019-06-26 14:27:08 +0200 | [diff] [blame] | 368 | .kill = pmem_pagemap_kill, |
| 369 | .cleanup = pmem_pagemap_cleanup, |
| 370 | }; |
| 371 | |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 372 | static int pmem_attach_disk(struct device *dev, |
| 373 | struct nd_namespace_common *ndns) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 374 | { |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 375 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 376 | struct nd_region *nd_region = to_nd_region(dev->parent); |
Ross Zwisler | ce7f11a | 2018-06-06 10:45:13 -0600 | [diff] [blame] | 377 | int nid = dev_to_node(dev), fua; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 378 | struct resource *res = &nsio->res; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 379 | struct resource bb_res; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 380 | struct nd_pfn *nd_pfn = NULL; |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 381 | struct dax_device *dax_dev; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 382 | struct nd_pfn_sb *pfn_sb; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 383 | struct pmem_device *pmem; |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 384 | struct request_queue *q; |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 385 | struct device *gendev; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 386 | struct gendisk *disk; |
| 387 | void *addr; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 388 | int rc; |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 389 | unsigned long flags = 0UL; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 390 | |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 391 | pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 392 | if (!pmem) |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 393 | return -ENOMEM; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 394 | |
Aneesh Kumar K.V | 8f4b01f | 2019-10-31 16:27:41 +0530 | [diff] [blame] | 395 | rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve()); |
| 396 | if (rc) |
| 397 | return rc; |
| 398 | |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 399 | /* while nsio_rw_bytes is active, parse a pfn info block if present */ |
| 400 | if (is_nd_pfn(dev)) { |
| 401 | nd_pfn = to_nd_pfn(dev); |
| 402 | rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap); |
| 403 | if (rc) |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | /* we're attaching a block device, disable raw namespace access */ |
Aneesh Kumar K.V | 8f4b01f | 2019-10-31 16:27:41 +0530 | [diff] [blame] | 408 | devm_namespace_disable(dev, ndns); |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 409 | |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 410 | dev_set_drvdata(dev, pmem); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 411 | pmem->phys_addr = res->start; |
| 412 | pmem->size = resource_size(res); |
Dan Williams | 0b27796 | 2017-06-09 09:46:50 -0700 | [diff] [blame] | 413 | fua = nvdimm_has_flush(nd_region); |
| 414 | if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) || fua < 0) { |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 415 | dev_warn(dev, "unable to guarantee persistence of writes\n"); |
Dan Williams | 0b27796 | 2017-06-09 09:46:50 -0700 | [diff] [blame] | 416 | fua = 0; |
| 417 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 418 | |
Dan Williams | 947df02 | 2016-03-21 22:28:40 -0700 | [diff] [blame] | 419 | if (!devm_request_mem_region(dev, res->start, resource_size(res), |
Dan Williams | 450c663 | 2016-11-28 11:15:18 -0800 | [diff] [blame] | 420 | dev_name(&ndns->dev))) { |
Dan Williams | 947df02 | 2016-03-21 22:28:40 -0700 | [diff] [blame] | 421 | dev_warn(dev, "could not reserve region %pR\n", res); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 422 | return -EBUSY; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 425 | q = blk_alloc_queue(dev_to_node(dev)); |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 426 | if (!q) |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 427 | return -ENOMEM; |
Dan Williams | 468ded0 | 2016-01-15 16:56:46 -0800 | [diff] [blame] | 428 | |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 429 | pmem->pfn_flags = PFN_DEV; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 430 | pmem->pgmap.ref = &q->q_usage_counter; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 431 | if (is_nd_pfn(dev)) { |
Christoph Hellwig | f6a55e1 | 2019-06-26 14:27:10 +0200 | [diff] [blame] | 432 | pmem->pgmap.type = MEMORY_DEVICE_FS_DAX; |
| 433 | pmem->pgmap.ops = &fsdax_pagemap_ops; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 434 | addr = devm_memremap_pages(dev, &pmem->pgmap); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 435 | pfn_sb = nd_pfn->pfn_sb; |
| 436 | pmem->data_offset = le64_to_cpu(pfn_sb->dataoff); |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 437 | pmem->pfn_pad = resource_size(res) - |
| 438 | resource_size(&pmem->pgmap.res); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 439 | pmem->pfn_flags |= PFN_MAP; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 440 | memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res)); |
| 441 | bb_res.start += pmem->data_offset; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 442 | } else if (pmem_should_map_pages(dev)) { |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 443 | memcpy(&pmem->pgmap.res, &nsio->res, sizeof(pmem->pgmap.res)); |
Christoph Hellwig | f6a55e1 | 2019-06-26 14:27:10 +0200 | [diff] [blame] | 444 | pmem->pgmap.type = MEMORY_DEVICE_FS_DAX; |
| 445 | pmem->pgmap.ops = &fsdax_pagemap_ops; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 446 | addr = devm_memremap_pages(dev, &pmem->pgmap); |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 447 | pmem->pfn_flags |= PFN_MAP; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 448 | memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res)); |
Dan Williams | 91ed7ac | 2018-10-04 16:32:08 -0700 | [diff] [blame] | 449 | } else { |
Dan Williams | 50f44ee | 2019-06-13 15:56:33 -0700 | [diff] [blame] | 450 | if (devm_add_action_or_reset(dev, pmem_release_queue, |
Christoph Hellwig | d8668bb | 2019-06-26 14:27:09 +0200 | [diff] [blame] | 451 | &pmem->pgmap)) |
Dan Williams | 50f44ee | 2019-06-13 15:56:33 -0700 | [diff] [blame] | 452 | return -ENOMEM; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 453 | addr = devm_memremap(dev, pmem->phys_addr, |
| 454 | pmem->size, ARCH_MEMREMAP_PMEM); |
Dan Williams | 91ed7ac | 2018-10-04 16:32:08 -0700 | [diff] [blame] | 455 | memcpy(&bb_res, &nsio->res, sizeof(bb_res)); |
| 456 | } |
Dan Williams | b36f476 | 2015-09-15 02:42:20 -0400 | [diff] [blame] | 457 | |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 458 | if (IS_ERR(addr)) |
| 459 | return PTR_ERR(addr); |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 460 | pmem->virt_addr = addr; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 461 | |
Ross Zwisler | ce7f11a | 2018-06-06 10:45:13 -0600 | [diff] [blame] | 462 | blk_queue_write_cache(q, true, fua); |
Dan Williams | 5a92289 | 2016-03-21 15:43:53 -0700 | [diff] [blame] | 463 | blk_queue_physical_block_size(q, PAGE_SIZE); |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 464 | blk_queue_logical_block_size(q, pmem_sector_size(ndns)); |
Dan Williams | 5a92289 | 2016-03-21 15:43:53 -0700 | [diff] [blame] | 465 | blk_queue_max_hw_sectors(q, UINT_MAX); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 466 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Ross Zwisler | 4557641 | 2018-06-26 16:30:39 -0600 | [diff] [blame] | 467 | if (pmem->pfn_flags & PFN_MAP) |
| 468 | blk_queue_flag_set(QUEUE_FLAG_DAX, q); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 469 | |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 470 | disk = alloc_disk_node(0, nid); |
Dan Williams | 030b99e | 2016-03-17 20:24:31 -0700 | [diff] [blame] | 471 | if (!disk) |
| 472 | return -ENOMEM; |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 473 | pmem->disk = disk; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 474 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 475 | disk->fops = &pmem_fops; |
Dan Williams | 5a92289 | 2016-03-21 15:43:53 -0700 | [diff] [blame] | 476 | disk->queue = q; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 477 | disk->flags = GENHD_FL_EXT_DEVT; |
Christoph Hellwig | 6ec26b8 | 2020-05-08 18:15:17 +0200 | [diff] [blame] | 478 | disk->private_data = pmem; |
Minchan Kim | 23c47d2 | 2017-11-15 17:33:00 -0800 | [diff] [blame] | 479 | disk->queue->backing_dev_info->capabilities |= BDI_CAP_SYNCHRONOUS_IO; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 480 | nvdimm_namespace_disk_name(ndns, disk->disk_name); |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 481 | set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset) |
| 482 | / 512); |
Dan Williams | b95f5f4 | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 483 | if (devm_init_badblocks(dev, &pmem->bb)) |
| 484 | return -ENOMEM; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 485 | nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_res); |
Dan Williams | 57f7f31 | 2016-01-06 12:03:42 -0800 | [diff] [blame] | 486 | disk->bb = &pmem->bb; |
Dan Williams | f02716d | 2016-06-15 14:59:17 -0700 | [diff] [blame] | 487 | |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 488 | if (is_nvdimm_sync(nd_region)) |
| 489 | flags = DAXDEV_F_SYNC; |
| 490 | dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops, flags); |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 491 | if (IS_ERR(dax_dev)) { |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 492 | put_disk(disk); |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 493 | return PTR_ERR(dax_dev); |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 494 | } |
Ross Zwisler | ce7f11a | 2018-06-06 10:45:13 -0600 | [diff] [blame] | 495 | dax_write_cache(dax_dev, nvdimm_has_cache(nd_region)); |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 496 | pmem->dax_dev = dax_dev; |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 497 | gendev = disk_to_dev(disk); |
| 498 | gendev->groups = pmem_attribute_groups; |
| 499 | |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 500 | device_add_disk(dev, disk, NULL); |
Dan Williams | c1d6e82 | 2017-01-24 23:02:09 -0800 | [diff] [blame] | 501 | if (devm_add_action_or_reset(dev, pmem_release_disk, pmem)) |
Dan Williams | f02716d | 2016-06-15 14:59:17 -0700 | [diff] [blame] | 502 | return -ENOMEM; |
| 503 | |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 504 | revalidate_disk(disk); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 505 | |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 506 | pmem->bb_state = sysfs_get_dirent(disk_to_dev(disk)->kobj.sd, |
| 507 | "badblocks"); |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame] | 508 | if (!pmem->bb_state) |
| 509 | dev_warn(dev, "'badblocks' notification disabled\n"); |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 510 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 511 | return 0; |
| 512 | } |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 513 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 514 | static int nd_pmem_probe(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 515 | { |
Aneesh Kumar K.V | 1c97afa | 2019-09-05 21:15:58 +0530 | [diff] [blame] | 516 | int ret; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 517 | struct nd_namespace_common *ndns; |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 518 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 519 | ndns = nvdimm_namespace_common_probe(dev); |
| 520 | if (IS_ERR(ndns)) |
| 521 | return PTR_ERR(ndns); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 522 | |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 523 | if (is_nd_btt(dev)) |
Christoph Hellwig | 708ab62 | 2015-08-10 23:07:08 -0400 | [diff] [blame] | 524 | return nvdimm_namespace_attach_btt(ndns); |
| 525 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 526 | if (is_nd_pfn(dev)) |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 527 | return pmem_attach_disk(dev, ndns); |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 528 | |
Aneesh Kumar K.V | 8f4b01f | 2019-10-31 16:27:41 +0530 | [diff] [blame] | 529 | ret = devm_namespace_enable(dev, ndns, nd_info_block_reserve()); |
| 530 | if (ret) |
| 531 | return ret; |
| 532 | |
Aneesh Kumar K.V | 1c97afa | 2019-09-05 21:15:58 +0530 | [diff] [blame] | 533 | ret = nd_btt_probe(dev, ndns); |
| 534 | if (ret == 0) |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 535 | return -ENXIO; |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 536 | |
Aneesh Kumar K.V | 1c97afa | 2019-09-05 21:15:58 +0530 | [diff] [blame] | 537 | /* |
| 538 | * We have two failure conditions here, there is no |
| 539 | * info reserver block or we found a valid info reserve block |
| 540 | * but failed to initialize the pfn superblock. |
| 541 | * |
| 542 | * For the first case consider namespace as a raw pmem namespace |
| 543 | * and attach a disk. |
| 544 | * |
| 545 | * For the latter, consider this a success and advance the namespace |
| 546 | * seed. |
| 547 | */ |
| 548 | ret = nd_pfn_probe(dev, ndns); |
| 549 | if (ret == 0) |
| 550 | return -ENXIO; |
| 551 | else if (ret == -EOPNOTSUPP) |
| 552 | return ret; |
| 553 | |
| 554 | ret = nd_dax_probe(dev, ndns); |
| 555 | if (ret == 0) |
| 556 | return -ENXIO; |
| 557 | else if (ret == -EOPNOTSUPP) |
| 558 | return ret; |
Aneesh Kumar K.V | 8f4b01f | 2019-10-31 16:27:41 +0530 | [diff] [blame] | 559 | |
| 560 | /* probe complete, attach handles namespace enabling */ |
| 561 | devm_namespace_disable(dev, ndns); |
| 562 | |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 563 | return pmem_attach_disk(dev, ndns); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 566 | static int nd_pmem_remove(struct device *dev) |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 567 | { |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame] | 568 | struct pmem_device *pmem = dev_get_drvdata(dev); |
| 569 | |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 570 | if (is_nd_btt(dev)) |
Dan Williams | 298f2bc | 2016-03-15 16:41:04 -0700 | [diff] [blame] | 571 | nvdimm_namespace_detach_btt(to_nd_btt(dev)); |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame] | 572 | else { |
| 573 | /* |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 574 | * Note, this assumes nd_device_lock() context to not |
| 575 | * race nd_pmem_notify() |
Dan Williams | 6aa734a | 2017-06-30 18:56:03 -0700 | [diff] [blame] | 576 | */ |
| 577 | sysfs_put(pmem->bb_state); |
| 578 | pmem->bb_state = NULL; |
| 579 | } |
Pankaj Gupta | c5d4355 | 2019-07-05 19:33:22 +0530 | [diff] [blame] | 580 | nvdimm_flush(to_nd_region(dev->parent), NULL); |
Dan Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 581 | |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
Dan Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 585 | static void nd_pmem_shutdown(struct device *dev) |
| 586 | { |
Pankaj Gupta | c5d4355 | 2019-07-05 19:33:22 +0530 | [diff] [blame] | 587 | nvdimm_flush(to_nd_region(dev->parent), NULL); |
Dan Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 590 | static void nd_pmem_notify(struct device *dev, enum nvdimm_event event) |
| 591 | { |
Toshi Kani | b2518c7 | 2017-04-25 17:04:13 -0600 | [diff] [blame] | 592 | struct nd_region *nd_region; |
Dan Williams | 298f2bc | 2016-03-15 16:41:04 -0700 | [diff] [blame] | 593 | resource_size_t offset = 0, end_trunc = 0; |
| 594 | struct nd_namespace_common *ndns; |
| 595 | struct nd_namespace_io *nsio; |
| 596 | struct resource res; |
Toshi Kani | b2518c7 | 2017-04-25 17:04:13 -0600 | [diff] [blame] | 597 | struct badblocks *bb; |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 598 | struct kernfs_node *bb_state; |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 599 | |
| 600 | if (event != NVDIMM_REVALIDATE_POISON) |
| 601 | return; |
| 602 | |
Dan Williams | 298f2bc | 2016-03-15 16:41:04 -0700 | [diff] [blame] | 603 | if (is_nd_btt(dev)) { |
| 604 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 605 | |
| 606 | ndns = nd_btt->ndns; |
Toshi Kani | b2518c7 | 2017-04-25 17:04:13 -0600 | [diff] [blame] | 607 | nd_region = to_nd_region(ndns->dev.parent); |
| 608 | nsio = to_nd_namespace_io(&ndns->dev); |
| 609 | bb = &nsio->bb; |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 610 | bb_state = NULL; |
Toshi Kani | b2518c7 | 2017-04-25 17:04:13 -0600 | [diff] [blame] | 611 | } else { |
| 612 | struct pmem_device *pmem = dev_get_drvdata(dev); |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 613 | |
Toshi Kani | b2518c7 | 2017-04-25 17:04:13 -0600 | [diff] [blame] | 614 | nd_region = to_region(pmem); |
| 615 | bb = &pmem->bb; |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 616 | bb_state = pmem->bb_state; |
Dan Williams | a390180 | 2016-04-07 20:02:06 -0700 | [diff] [blame] | 617 | |
Toshi Kani | b2518c7 | 2017-04-25 17:04:13 -0600 | [diff] [blame] | 618 | if (is_nd_pfn(dev)) { |
| 619 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 620 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 621 | |
| 622 | ndns = nd_pfn->ndns; |
| 623 | offset = pmem->data_offset + |
| 624 | __le32_to_cpu(pfn_sb->start_pad); |
| 625 | end_trunc = __le32_to_cpu(pfn_sb->end_trunc); |
| 626 | } else { |
| 627 | ndns = to_ndns(dev); |
| 628 | } |
| 629 | |
| 630 | nsio = to_nd_namespace_io(&ndns->dev); |
| 631 | } |
| 632 | |
Dan Williams | 298f2bc | 2016-03-15 16:41:04 -0700 | [diff] [blame] | 633 | res.start = nsio->res.start + offset; |
| 634 | res.end = nsio->res.end - end_trunc; |
Toshi Kani | b2518c7 | 2017-04-25 17:04:13 -0600 | [diff] [blame] | 635 | nvdimm_badblocks_populate(nd_region, bb, &res); |
Toshi Kani | 975750a | 2017-06-12 16:25:11 -0600 | [diff] [blame] | 636 | if (bb_state) |
| 637 | sysfs_notify_dirent(bb_state); |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 640 | MODULE_ALIAS("pmem"); |
| 641 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 642 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 643 | static struct nd_device_driver nd_pmem_driver = { |
| 644 | .probe = nd_pmem_probe, |
| 645 | .remove = nd_pmem_remove, |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 646 | .notify = nd_pmem_notify, |
Dan Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 647 | .shutdown = nd_pmem_shutdown, |
Dan Williams | 9f53f9f | 2015-06-09 15:33:45 -0400 | [diff] [blame] | 648 | .drv = { |
| 649 | .name = "nd_pmem", |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 650 | }, |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 651 | .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 652 | }; |
| 653 | |
Johannes Thumshirn | 03e9084 | 2018-03-14 19:25:07 +0100 | [diff] [blame] | 654 | module_nd_driver(nd_pmem_driver); |
Ross Zwisler | 9e853f2 | 2015-04-01 09:12:19 +0200 | [diff] [blame] | 655 | |
| 656 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); |
| 657 | MODULE_LICENSE("GPL v2"); |