blob: 5c6939e004e2d887de426ad73f5aa8837ceee607 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Ross Zwisler9e853f22015-04-01 09:12:19 +02002/*
3 * Persistent Memory Driver
4 *
Dan Williams9f53f9f2015-06-09 15:33:45 -04005 * Copyright (c) 2014-2015, Intel Corporation.
Ross Zwisler9e853f22015-04-01 09:12:19 +02006 * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
7 * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
Ross Zwisler9e853f22015-04-01 09:12:19 +02008 */
9
Ross Zwisler9e853f22015-04-01 09:12:19 +020010#include <linux/blkdev.h>
11#include <linux/hdreg.h>
12#include <linux/init.h>
13#include <linux/platform_device.h>
Dan Williamsc953cc92018-07-13 21:50:37 -070014#include <linux/set_memory.h>
Ross Zwisler9e853f22015-04-01 09:12:19 +020015#include <linux/module.h>
16#include <linux/moduleparam.h>
Dan Williamsb95f5f42016-01-04 23:50:23 -080017#include <linux/badblocks.h>
Dan Williams9476df72016-01-15 16:56:19 -080018#include <linux/memremap.h>
Dan Williams32ab0a3f2015-08-01 02:16:37 -040019#include <linux/vmalloc.h>
Dan Williams71389702017-04-28 10:23:37 -070020#include <linux/blk-mq.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080021#include <linux/pfn_t.h>
Ross Zwisler9e853f22015-04-01 09:12:19 +020022#include <linux/slab.h>
Dan Williams0aed55a2017-05-29 12:22:50 -070023#include <linux/uio.h>
Dan Williamsc1d6e822017-01-24 23:02:09 -080024#include <linux/dax.h>
Dan Williams9f53f9f2015-06-09 15:33:45 -040025#include <linux/nd.h>
Minchan Kim23c47d22017-11-15 17:33:00 -080026#include <linux/backing-dev.h>
Christoph Hellwige0cf6152020-06-07 21:41:42 -070027#include <linux/mm.h>
28#include <asm/cacheflush.h>
Dan Williamsf295e532016-06-17 11:08:06 -070029#include "pmem.h"
Dan Williams32ab0a3f2015-08-01 02:16:37 -040030#include "pfn.h"
Dan Williams9f53f9f2015-06-09 15:33:45 -040031#include "nd.h"
Ross Zwisler9e853f22015-04-01 09:12:19 +020032
Dan Williamsf284a4f2016-07-07 19:44:50 -070033static 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
42static struct nd_region *to_region(struct pmem_device *pmem)
43{
44 return to_nd_region(to_dev(pmem)->parent);
45}
Ross Zwisler9e853f22015-04-01 09:12:19 +020046
Dan Williamsc953cc92018-07-13 21:50:37 -070047static 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 Hellwig4e4cbee2017-06-03 09:38:06 +020071static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
72 phys_addr_t offset, unsigned int len)
Dan Williams59e64732016-03-08 07:16:07 -080073{
Dan Williamsf284a4f2016-07-07 19:44:50 -070074 struct device *dev = to_dev(pmem);
Dan Williams59e64732016-03-08 07:16:07 -080075 sector_t sector;
76 long cleared;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020077 blk_status_t rc = BLK_STS_OK;
Dan Williams59e64732016-03-08 07:16:07 -080078
79 sector = (offset - pmem->data_offset) / 512;
Dan Williams59e64732016-03-08 07:16:07 -080080
Dan Williams868f036f2016-12-16 08:10:31 -080081 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
82 if (cleared < len)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020083 rc = BLK_STS_IOERR;
Dan Williams59e64732016-03-08 07:16:07 -080084 if (cleared > 0 && cleared / 512) {
Dan Williamsc953cc92018-07-13 21:50:37 -070085 hwpoison_clear(pmem, pmem->phys_addr + offset, cleared);
Dan Williams868f036f2016-12-16 08:10:31 -080086 cleared /= 512;
Dan Williams426824d2018-03-05 16:39:31 -080087 dev_dbg(dev, "%#llx clear %ld sector%s\n",
Dan Williams868f036f2016-12-16 08:10:31 -080088 (unsigned long long) sector, cleared,
89 cleared > 1 ? "s" : "");
Fabian Frederick0a3f27b2016-12-04 10:48:58 -080090 badblocks_clear(&pmem->bb, sector, cleared);
Toshi Kani975750a2017-06-12 16:25:11 -060091 if (pmem->bb_state)
92 sysfs_notify_dirent(pmem->bb_state);
Dan Williams59e64732016-03-08 07:16:07 -080093 }
Toshi Kani3115bb02016-10-13 09:54:21 -060094
Dan Williamsf2b61252017-05-29 23:00:34 -070095 arch_invalidate_pmem(pmem->virt_addr + offset, len);
Dan Williams868f036f2016-12-16 08:10:31 -080096
97 return rc;
Dan Williams59e64732016-03-08 07:16:07 -080098}
99
Vishal Vermabd697a82016-09-30 17:19:30 -0600100static void write_pmem(void *pmem_addr, struct page *page,
101 unsigned int off, unsigned int len)
102{
Huang Ying98cc0932017-09-06 16:22:27 -0700103 unsigned int chunk;
104 void *mem;
Vishal Vermabd697a82016-09-30 17:19:30 -0600105
Huang Ying98cc0932017-09-06 16:22:27 -0700106 while (len) {
107 mem = kmap_atomic(page);
Li RongQing9dc64882019-04-04 10:58:01 +0800108 chunk = min_t(unsigned int, len, PAGE_SIZE - off);
Huang Ying98cc0932017-09-06 16:22:27 -0700109 memcpy_flushcache(pmem_addr, mem + off, chunk);
110 kunmap_atomic(mem);
111 len -= chunk;
112 off = 0;
113 page++;
Li RongQing9dc64882019-04-04 10:58:01 +0800114 pmem_addr += chunk;
Huang Ying98cc0932017-09-06 16:22:27 -0700115 }
Vishal Vermabd697a82016-09-30 17:19:30 -0600116}
117
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200118static blk_status_t read_pmem(struct page *page, unsigned int off,
Vishal Vermabd697a82016-09-30 17:19:30 -0600119 void *pmem_addr, unsigned int len)
120{
Huang Ying98cc0932017-09-06 16:22:27 -0700121 unsigned int chunk;
Dan Williams60622d62018-05-03 17:06:21 -0700122 unsigned long rem;
Huang Ying98cc0932017-09-06 16:22:27 -0700123 void *mem;
Vishal Vermabd697a82016-09-30 17:19:30 -0600124
Huang Ying98cc0932017-09-06 16:22:27 -0700125 while (len) {
126 mem = kmap_atomic(page);
Li RongQing9dc64882019-04-04 10:58:01 +0800127 chunk = min_t(unsigned int, len, PAGE_SIZE - off);
Dan Williamsec6347b2020-10-05 20:40:16 -0700128 rem = copy_mc_to_kernel(mem + off, pmem_addr, chunk);
Huang Ying98cc0932017-09-06 16:22:27 -0700129 kunmap_atomic(mem);
Dan Williams60622d62018-05-03 17:06:21 -0700130 if (rem)
Huang Ying98cc0932017-09-06 16:22:27 -0700131 return BLK_STS_IOERR;
132 len -= chunk;
133 off = 0;
134 page++;
Li RongQing9dc64882019-04-04 10:58:01 +0800135 pmem_addr += chunk;
Huang Ying98cc0932017-09-06 16:22:27 -0700136 }
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200137 return BLK_STS_OK;
Vishal Vermabd697a82016-09-30 17:19:30 -0600138}
139
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500140static 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
156static 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 Zwisler9e853f22015-04-01 09:12:19 +0200159{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200160 blk_status_t rc = BLK_STS_OK;
Dan Williams59e64732016-03-08 07:16:07 -0800161 bool bad_pmem = false;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400162 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
Dan Williams7a9eb202016-06-03 18:06:47 -0700163 void *pmem_addr = pmem->virt_addr + pmem_off;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200164
Dan Williams59e64732016-03-08 07:16:07 -0800165 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
166 bad_pmem = true;
167
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500168 /*
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 Zwisler9e853f22015-04-01 09:12:19 +0200187 }
188
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800189 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200190}
191
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200192static blk_qc_t pmem_submit_bio(struct bio *bio)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200193{
Pankaj Guptac5d43552019-07-05 19:33:22 +0530194 int ret = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200195 blk_status_t rc = 0;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400196 bool do_acct;
197 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -0400198 struct bio_vec bvec;
199 struct bvec_iter iter;
Christoph Hellwig6ec26b82020-05-08 18:15:17 +0200200 struct pmem_device *pmem = bio->bi_disk->private_data;
Dan Williams7e267a82016-06-01 20:48:15 -0700201 struct nd_region *nd_region = to_region(pmem);
202
Ross Zwislerd2d63642018-06-06 10:45:12 -0600203 if (bio->bi_opf & REQ_PREFLUSH)
Pankaj Guptac5d43552019-07-05 19:33:22 +0530204 ret = nvdimm_flush(nd_region, bio);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200205
Christoph Hellwig0fd92f82020-05-27 07:24:10 +0200206 do_acct = blk_queue_io_stat(bio->bi_disk->queue);
207 if (do_acct)
208 start = bio_start_io_acct(bio);
Dan Williamse10624f2016-01-06 12:03:41 -0800209 bio_for_each_segment(bvec, bio, iter) {
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500210 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 Williamse10624f2016-01-06 12:03:41 -0800216 if (rc) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200217 bio->bi_status = rc;
Dan Williamse10624f2016-01-06 12:03:41 -0800218 break;
219 }
220 }
Dan Williamsf0dc0892015-05-16 12:28:53 -0400221 if (do_acct)
Christoph Hellwig0fd92f82020-05-27 07:24:10 +0200222 bio_end_io_acct(bio, start);
Ross Zwisler61031952015-06-25 03:08:39 -0400223
Jens Axboe1eff9d32016-08-05 15:35:16 -0600224 if (bio->bi_opf & REQ_FUA)
Pankaj Guptac5d43552019-07-05 19:33:22 +0530225 ret = nvdimm_flush(nd_region, bio);
226
227 if (ret)
228 bio->bi_status = errno_to_blk_status(ret);
Ross Zwisler61031952015-06-25 03:08:39 -0400229
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200230 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700231 return BLK_QC_T_NONE;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200232}
233
234static int pmem_rw_page(struct block_device *bdev, sector_t sector,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700235 struct page *page, unsigned int op)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200236{
Christoph Hellwig6ec26b82020-05-08 18:15:17 +0200237 struct pmem_device *pmem = bdev->bd_disk->private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200238 blk_status_t rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200239
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500240 if (op_is_write(op))
Matthew Wilcox (Oracle)af3bbc12020-08-14 17:30:33 -0700241 rc = pmem_do_write(pmem, page, 0, sector, thp_size(page));
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500242 else
Matthew Wilcox (Oracle)af3bbc12020-08-14 17:30:33 -0700243 rc = pmem_do_read(pmem, page, 0, sector, thp_size(page));
Dan Williamse10624f2016-01-06 12:03:41 -0800244 /*
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 Heo3f289dc2018-07-18 04:47:36 -0700251 page_endio(page, op_is_write(op), 0);
Dan Williamse10624f2016-01-06 12:03:41 -0800252
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200253 return blk_status_to_errno(rc);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200254}
255
Dan Williamsf295e532016-06-17 11:08:06 -0700256/* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
Dan Williamsc1d6e822017-01-24 23:02:09 -0800257__weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
258 long nr_pages, void **kaddr, pfn_t *pfn)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200259{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800260 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200261
Dan Williamsc1d6e822017-01-24 23:02:09 -0800262 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
263 PFN_PHYS(nr_pages))))
Dan Williams0a70bd42016-02-24 14:02:11 -0800264 return -EIO;
Huaisheng Ye46a590c2018-07-30 15:15:43 +0800265
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 Zwisler9e853f22015-04-01 09:12:19 +0200270
Dan Williams0a70bd42016-02-24 14:02:11 -0800271 /*
272 * If badblocks are present, limit known good range to the
273 * requested range.
274 */
275 if (unlikely(pmem->bb.count))
Dan Williamsc1d6e822017-01-24 23:02:09 -0800276 return nr_pages;
277 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200278}
279
280static const struct block_device_operations pmem_fops = {
281 .owner = THIS_MODULE,
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200282 .submit_bio = pmem_submit_bio,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200283 .rw_page = pmem_rw_page,
Dan Williams58138822015-06-23 20:08:34 -0400284 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200285};
286
Vivek Goyalf605a262020-02-28 11:34:52 -0500287static 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 Williamsc1d6e822017-01-24 23:02:09 -0800297static 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 Williams52f476a2019-05-16 17:05:21 -0700305/*
306 * Use the 'no check' versions of copy_from_iter_flushcache() and
Dan Williamsec6347b2020-10-05 20:40:16 -0700307 * copy_mc_to_iter() to bypass HARDENED_USERCOPY overhead. Bounds
Dan Williams52f476a2019-05-16 17:05:21 -0700308 * checking, both file offset and device offset, is handled by
309 * dax_iomap_actor()
310 */
Dan Williams0aed55a2017-05-29 12:22:50 -0700311static 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 Williams52f476a2019-05-16 17:05:21 -0700314 return _copy_from_iter_flushcache(addr, bytes, i);
Dan Williams0aed55a2017-05-29 12:22:50 -0700315}
316
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700317static 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 Williamsec6347b2020-10-05 20:40:16 -0700320 return _copy_mc_to_iter(addr, bytes, i);
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700321}
322
Dan Williamsc1d6e822017-01-24 23:02:09 -0800323static const struct dax_operations pmem_dax_ops = {
324 .direct_access = pmem_dax_direct_access,
Dan Williams7bf7eac2019-05-16 13:26:29 -0700325 .dax_supported = generic_fsdax_supported,
Dan Williams0aed55a2017-05-29 12:22:50 -0700326 .copy_from_iter = pmem_copy_from_iter,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700327 .copy_to_iter = pmem_copy_to_iter,
Vivek Goyalf605a262020-02-28 11:34:52 -0500328 .zero_page_range = pmem_dax_zero_page_range,
Dan Williamsc1d6e822017-01-24 23:02:09 -0800329};
330
Dan Williams6e0c90d2017-06-26 21:28:41 -0700331static const struct attribute_group *pmem_attribute_groups[] = {
332 &dax_attribute_group,
333 NULL,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200334};
335
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200336static void pmem_pagemap_cleanup(struct dev_pagemap *pgmap)
Dan Williams030b99e2016-03-17 20:24:31 -0700337{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200338 struct request_queue *q =
339 container_of(pgmap->ref, struct request_queue, q_usage_counter);
Dan Williams50f44ee2019-06-13 15:56:33 -0700340
Dan Williams030b99e2016-03-17 20:24:31 -0700341 blk_cleanup_queue(q);
342}
343
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200344static void pmem_release_queue(void *pgmap)
Dan Williams50f44ee2019-06-13 15:56:33 -0700345{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200346 pmem_pagemap_cleanup(pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700347}
348
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200349static void pmem_pagemap_kill(struct dev_pagemap *pgmap)
Dan Williams71389702017-04-28 10:23:37 -0700350{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200351 struct request_queue *q =
352 container_of(pgmap->ref, struct request_queue, q_usage_counter);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800353
Linus Torvaldsd3b5d352017-05-01 23:54:56 -0700354 blk_freeze_queue_start(q);
Dan Williams71389702017-04-28 10:23:37 -0700355}
356
Dan Williamsc1d6e822017-01-24 23:02:09 -0800357static void pmem_release_disk(void *__pmem)
Dan Williams030b99e2016-03-17 20:24:31 -0700358{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800359 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 Williams030b99e2016-03-17 20:24:31 -0700365}
366
Christoph Hellwig1e240e82019-06-26 14:27:08 +0200367static const struct dev_pagemap_ops fsdax_pagemap_ops = {
Christoph Hellwig1e240e82019-06-26 14:27:08 +0200368 .kill = pmem_pagemap_kill,
369 .cleanup = pmem_pagemap_cleanup,
370};
371
Dan Williams200c79d2016-03-22 00:22:16 -0700372static int pmem_attach_disk(struct device *dev,
373 struct nd_namespace_common *ndns)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200374{
Dan Williams200c79d2016-03-22 00:22:16 -0700375 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
Dan Williamsf284a4f2016-07-07 19:44:50 -0700376 struct nd_region *nd_region = to_nd_region(dev->parent);
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600377 int nid = dev_to_node(dev), fua;
Dan Williams200c79d2016-03-22 00:22:16 -0700378 struct resource *res = &nsio->res;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100379 struct resource bb_res;
Dan Williams200c79d2016-03-22 00:22:16 -0700380 struct nd_pfn *nd_pfn = NULL;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800381 struct dax_device *dax_dev;
Dan Williams200c79d2016-03-22 00:22:16 -0700382 struct nd_pfn_sb *pfn_sb;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200383 struct pmem_device *pmem;
Dan Williams468ded02016-01-15 16:56:46 -0800384 struct request_queue *q;
Dan Williams6e0c90d2017-06-26 21:28:41 -0700385 struct device *gendev;
Dan Williams200c79d2016-03-22 00:22:16 -0700386 struct gendisk *disk;
387 void *addr;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100388 int rc;
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530389 unsigned long flags = 0UL;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200390
Christoph Hellwig708ab622015-08-10 23:07:08 -0400391 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200392 if (!pmem)
Dan Williams200c79d2016-03-22 00:22:16 -0700393 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200394
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530395 rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
396 if (rc)
397 return rc;
398
Christoph Hellwige8d51342017-12-29 08:54:05 +0100399 /* 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.V8f4b01f2019-10-31 16:27:41 +0530408 devm_namespace_disable(dev, ndns);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100409
Dan Williams200c79d2016-03-22 00:22:16 -0700410 dev_set_drvdata(dev, pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200411 pmem->phys_addr = res->start;
412 pmem->size = resource_size(res);
Dan Williams0b277962017-06-09 09:46:50 -0700413 fua = nvdimm_has_flush(nd_region);
414 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) || fua < 0) {
Ross Zwisler61031952015-06-25 03:08:39 -0400415 dev_warn(dev, "unable to guarantee persistence of writes\n");
Dan Williams0b277962017-06-09 09:46:50 -0700416 fua = 0;
417 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200418
Dan Williams947df022016-03-21 22:28:40 -0700419 if (!devm_request_mem_region(dev, res->start, resource_size(res),
Dan Williams450c6632016-11-28 11:15:18 -0800420 dev_name(&ndns->dev))) {
Dan Williams947df022016-03-21 22:28:40 -0700421 dev_warn(dev, "could not reserve region %pR\n", res);
Dan Williams200c79d2016-03-22 00:22:16 -0700422 return -EBUSY;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200423 }
424
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200425 q = blk_alloc_queue(dev_to_node(dev));
Dan Williams468ded02016-01-15 16:56:46 -0800426 if (!q)
Dan Williams200c79d2016-03-22 00:22:16 -0700427 return -ENOMEM;
Dan Williams468ded02016-01-15 16:56:46 -0800428
Dan Williams34c0fd52016-01-15 16:56:14 -0800429 pmem->pfn_flags = PFN_DEV;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100430 pmem->pgmap.ref = &q->q_usage_counter;
Dan Williams200c79d2016-03-22 00:22:16 -0700431 if (is_nd_pfn(dev)) {
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200432 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
433 pmem->pgmap.ops = &fsdax_pagemap_ops;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100434 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams200c79d2016-03-22 00:22:16 -0700435 pfn_sb = nd_pfn->pfn_sb;
436 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100437 pmem->pfn_pad = resource_size(res) -
438 resource_size(&pmem->pgmap.res);
Dan Williams200c79d2016-03-22 00:22:16 -0700439 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100440 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
441 bb_res.start += pmem->data_offset;
Dan Williams200c79d2016-03-22 00:22:16 -0700442 } else if (pmem_should_map_pages(dev)) {
Christoph Hellwige8d51342017-12-29 08:54:05 +0100443 memcpy(&pmem->pgmap.res, &nsio->res, sizeof(pmem->pgmap.res));
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200444 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
445 pmem->pgmap.ops = &fsdax_pagemap_ops;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100446 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams34c0fd52016-01-15 16:56:14 -0800447 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100448 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
Dan Williams91ed7ac2018-10-04 16:32:08 -0700449 } else {
Dan Williams50f44ee2019-06-13 15:56:33 -0700450 if (devm_add_action_or_reset(dev, pmem_release_queue,
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200451 &pmem->pgmap))
Dan Williams50f44ee2019-06-13 15:56:33 -0700452 return -ENOMEM;
Dan Williams200c79d2016-03-22 00:22:16 -0700453 addr = devm_memremap(dev, pmem->phys_addr,
454 pmem->size, ARCH_MEMREMAP_PMEM);
Dan Williams91ed7ac2018-10-04 16:32:08 -0700455 memcpy(&bb_res, &nsio->res, sizeof(bb_res));
456 }
Dan Williamsb36f4762015-09-15 02:42:20 -0400457
Dan Williams200c79d2016-03-22 00:22:16 -0700458 if (IS_ERR(addr))
459 return PTR_ERR(addr);
Dan Williams7a9eb202016-06-03 18:06:47 -0700460 pmem->virt_addr = addr;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200461
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600462 blk_queue_write_cache(q, true, fua);
Dan Williams5a922892016-03-21 15:43:53 -0700463 blk_queue_physical_block_size(q, PAGE_SIZE);
Dan Williamsf979b132017-06-04 12:12:07 +0900464 blk_queue_logical_block_size(q, pmem_sector_size(ndns));
Dan Williams5a922892016-03-21 15:43:53 -0700465 blk_queue_max_hw_sectors(q, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -0800466 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Ross Zwisler45576412018-06-26 16:30:39 -0600467 if (pmem->pfn_flags & PFN_MAP)
468 blk_queue_flag_set(QUEUE_FLAG_DAX, q);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200469
Dan Williams538ea4a2015-10-05 20:35:56 -0400470 disk = alloc_disk_node(0, nid);
Dan Williams030b99e2016-03-17 20:24:31 -0700471 if (!disk)
472 return -ENOMEM;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800473 pmem->disk = disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200474
Ross Zwisler9e853f22015-04-01 09:12:19 +0200475 disk->fops = &pmem_fops;
Dan Williams5a922892016-03-21 15:43:53 -0700476 disk->queue = q;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200477 disk->flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig6ec26b82020-05-08 18:15:17 +0200478 disk->private_data = pmem;
Minchan Kim23c47d22017-11-15 17:33:00 -0800479 disk->queue->backing_dev_info->capabilities |= BDI_CAP_SYNCHRONOUS_IO;
Vishal Verma5212e112015-06-25 04:20:32 -0400480 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williamscfe30b82016-03-03 09:38:00 -0800481 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
482 / 512);
Dan Williamsb95f5f42016-01-04 23:50:23 -0800483 if (devm_init_badblocks(dev, &pmem->bb))
484 return -ENOMEM;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100485 nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_res);
Dan Williams57f7f312016-01-06 12:03:42 -0800486 disk->bb = &pmem->bb;
Dan Williamsf02716d2016-06-15 14:59:17 -0700487
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530488 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 Goyal4e4ced92020-04-01 12:11:25 -0400491 if (IS_ERR(dax_dev)) {
Dan Williamsc1d6e822017-01-24 23:02:09 -0800492 put_disk(disk);
Vivek Goyal4e4ced92020-04-01 12:11:25 -0400493 return PTR_ERR(dax_dev);
Dan Williamsc1d6e822017-01-24 23:02:09 -0800494 }
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600495 dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
Dan Williamsc1d6e822017-01-24 23:02:09 -0800496 pmem->dax_dev = dax_dev;
Dan Williams6e0c90d2017-06-26 21:28:41 -0700497 gendev = disk_to_dev(disk);
498 gendev->groups = pmem_attribute_groups;
499
Hannes Reineckefef912b2018-09-28 08:17:19 +0200500 device_add_disk(dev, disk, NULL);
Dan Williamsc1d6e822017-01-24 23:02:09 -0800501 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
Dan Williamsf02716d2016-06-15 14:59:17 -0700502 return -ENOMEM;
503
Dan Williams58138822015-06-23 20:08:34 -0400504 revalidate_disk(disk);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200505
Toshi Kani975750a2017-06-12 16:25:11 -0600506 pmem->bb_state = sysfs_get_dirent(disk_to_dev(disk)->kobj.sd,
507 "badblocks");
Dan Williams6aa734a2017-06-30 18:56:03 -0700508 if (!pmem->bb_state)
509 dev_warn(dev, "'badblocks' notification disabled\n");
Toshi Kani975750a2017-06-12 16:25:11 -0600510
Dan Williams8c2f7e82015-06-25 04:20:04 -0400511 return 0;
512}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200513
Dan Williams9f53f9f2015-06-09 15:33:45 -0400514static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200515{
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530516 int ret;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400517 struct nd_namespace_common *ndns;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200518
Dan Williams8c2f7e82015-06-25 04:20:04 -0400519 ndns = nvdimm_namespace_common_probe(dev);
520 if (IS_ERR(ndns))
521 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400522
Dan Williams200c79d2016-03-22 00:22:16 -0700523 if (is_nd_btt(dev))
Christoph Hellwig708ab622015-08-10 23:07:08 -0400524 return nvdimm_namespace_attach_btt(ndns);
525
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400526 if (is_nd_pfn(dev))
Dan Williams200c79d2016-03-22 00:22:16 -0700527 return pmem_attach_disk(dev, ndns);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400528
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530529 ret = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
530 if (ret)
531 return ret;
532
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530533 ret = nd_btt_probe(dev, ndns);
534 if (ret == 0)
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400535 return -ENXIO;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400536
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530537 /*
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.V8f4b01f2019-10-31 16:27:41 +0530559
560 /* probe complete, attach handles namespace enabling */
561 devm_namespace_disable(dev, ndns);
562
Dan Williams200c79d2016-03-22 00:22:16 -0700563 return pmem_attach_disk(dev, ndns);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200564}
565
Dan Williams9f53f9f2015-06-09 15:33:45 -0400566static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200567{
Dan Williams6aa734a2017-06-30 18:56:03 -0700568 struct pmem_device *pmem = dev_get_drvdata(dev);
569
Dan Williams8c2f7e82015-06-25 04:20:04 -0400570 if (is_nd_btt(dev))
Dan Williams298f2bc2016-03-15 16:41:04 -0700571 nvdimm_namespace_detach_btt(to_nd_btt(dev));
Dan Williams6aa734a2017-06-30 18:56:03 -0700572 else {
573 /*
Dan Williams87a30e12019-07-17 18:08:26 -0700574 * Note, this assumes nd_device_lock() context to not
575 * race nd_pmem_notify()
Dan Williams6aa734a2017-06-30 18:56:03 -0700576 */
577 sysfs_put(pmem->bb_state);
578 pmem->bb_state = NULL;
579 }
Pankaj Guptac5d43552019-07-05 19:33:22 +0530580 nvdimm_flush(to_nd_region(dev->parent), NULL);
Dan Williams476f8482016-07-09 00:12:52 -0700581
Ross Zwisler9e853f22015-04-01 09:12:19 +0200582 return 0;
583}
584
Dan Williams476f8482016-07-09 00:12:52 -0700585static void nd_pmem_shutdown(struct device *dev)
586{
Pankaj Guptac5d43552019-07-05 19:33:22 +0530587 nvdimm_flush(to_nd_region(dev->parent), NULL);
Dan Williams476f8482016-07-09 00:12:52 -0700588}
589
Dan Williams71999462016-02-18 10:29:49 -0800590static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
591{
Toshi Kanib2518c72017-04-25 17:04:13 -0600592 struct nd_region *nd_region;
Dan Williams298f2bc2016-03-15 16:41:04 -0700593 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 Kanib2518c72017-04-25 17:04:13 -0600597 struct badblocks *bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600598 struct kernfs_node *bb_state;
Dan Williams71999462016-02-18 10:29:49 -0800599
600 if (event != NVDIMM_REVALIDATE_POISON)
601 return;
602
Dan Williams298f2bc2016-03-15 16:41:04 -0700603 if (is_nd_btt(dev)) {
604 struct nd_btt *nd_btt = to_nd_btt(dev);
605
606 ndns = nd_btt->ndns;
Toshi Kanib2518c72017-04-25 17:04:13 -0600607 nd_region = to_nd_region(ndns->dev.parent);
608 nsio = to_nd_namespace_io(&ndns->dev);
609 bb = &nsio->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600610 bb_state = NULL;
Toshi Kanib2518c72017-04-25 17:04:13 -0600611 } else {
612 struct pmem_device *pmem = dev_get_drvdata(dev);
Dan Williamsa3901802016-04-07 20:02:06 -0700613
Toshi Kanib2518c72017-04-25 17:04:13 -0600614 nd_region = to_region(pmem);
615 bb = &pmem->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600616 bb_state = pmem->bb_state;
Dan Williamsa3901802016-04-07 20:02:06 -0700617
Toshi Kanib2518c72017-04-25 17:04:13 -0600618 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 Williams298f2bc2016-03-15 16:41:04 -0700633 res.start = nsio->res.start + offset;
634 res.end = nsio->res.end - end_trunc;
Toshi Kanib2518c72017-04-25 17:04:13 -0600635 nvdimm_badblocks_populate(nd_region, bb, &res);
Toshi Kani975750a2017-06-12 16:25:11 -0600636 if (bb_state)
637 sysfs_notify_dirent(bb_state);
Dan Williams71999462016-02-18 10:29:49 -0800638}
639
Dan Williams9f53f9f2015-06-09 15:33:45 -0400640MODULE_ALIAS("pmem");
641MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400642MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400643static struct nd_device_driver nd_pmem_driver = {
644 .probe = nd_pmem_probe,
645 .remove = nd_pmem_remove,
Dan Williams71999462016-02-18 10:29:49 -0800646 .notify = nd_pmem_notify,
Dan Williams476f8482016-07-09 00:12:52 -0700647 .shutdown = nd_pmem_shutdown,
Dan Williams9f53f9f2015-06-09 15:33:45 -0400648 .drv = {
649 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200650 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400651 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200652};
653
Johannes Thumshirn03e90842018-03-14 19:25:07 +0100654module_nd_driver(nd_pmem_driver);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200655
656MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
657MODULE_LICENSE("GPL v2");