blob: f8dc5941215bfff681b20bf5b1c07f79690d8aff [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
10#include <asm/cacheflush.h>
11#include <linux/blkdev.h>
12#include <linux/hdreg.h>
13#include <linux/init.h>
14#include <linux/platform_device.h>
Dan Williamsc953cc92018-07-13 21:50:37 -070015#include <linux/set_memory.h>
Ross Zwisler9e853f22015-04-01 09:12:19 +020016#include <linux/module.h>
17#include <linux/moduleparam.h>
Dan Williamsb95f5f42016-01-04 23:50:23 -080018#include <linux/badblocks.h>
Dan Williams9476df72016-01-15 16:56:19 -080019#include <linux/memremap.h>
Dan Williams32ab0a3f2015-08-01 02:16:37 -040020#include <linux/vmalloc.h>
Dan Williams71389702017-04-28 10:23:37 -070021#include <linux/blk-mq.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080022#include <linux/pfn_t.h>
Ross Zwisler9e853f22015-04-01 09:12:19 +020023#include <linux/slab.h>
Dan Williams0aed55a2017-05-29 12:22:50 -070024#include <linux/uio.h>
Dan Williamsc1d6e822017-01-24 23:02:09 -080025#include <linux/dax.h>
Dan Williams9f53f9f2015-06-09 15:33:45 -040026#include <linux/nd.h>
Minchan Kim23c47d22017-11-15 17:33:00 -080027#include <linux/backing-dev.h>
Dan Williamsf295e532016-06-17 11:08:06 -070028#include "pmem.h"
Dan Williams32ab0a3f2015-08-01 02:16:37 -040029#include "pfn.h"
Dan Williams9f53f9f2015-06-09 15:33:45 -040030#include "nd.h"
Ross Zwisler9e853f22015-04-01 09:12:19 +020031
Dan Williamsf284a4f2016-07-07 19:44:50 -070032static struct device *to_dev(struct pmem_device *pmem)
33{
34 /*
35 * nvdimm bus services need a 'dev' parameter, and we record the device
36 * at init in bb.dev.
37 */
38 return pmem->bb.dev;
39}
40
41static struct nd_region *to_region(struct pmem_device *pmem)
42{
43 return to_nd_region(to_dev(pmem)->parent);
44}
Ross Zwisler9e853f22015-04-01 09:12:19 +020045
Dan Williamsc953cc92018-07-13 21:50:37 -070046static void hwpoison_clear(struct pmem_device *pmem,
47 phys_addr_t phys, unsigned int len)
48{
49 unsigned long pfn_start, pfn_end, pfn;
50
51 /* only pmem in the linear map supports HWPoison */
52 if (is_vmalloc_addr(pmem->virt_addr))
53 return;
54
55 pfn_start = PHYS_PFN(phys);
56 pfn_end = pfn_start + PHYS_PFN(len);
57 for (pfn = pfn_start; pfn < pfn_end; pfn++) {
58 struct page *page = pfn_to_page(pfn);
59
60 /*
61 * Note, no need to hold a get_dev_pagemap() reference
62 * here since we're in the driver I/O path and
63 * outstanding I/O requests pin the dev_pagemap.
64 */
65 if (test_and_clear_pmem_poison(page))
66 clear_mce_nospec(pfn);
67 }
68}
69
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020070static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
71 phys_addr_t offset, unsigned int len)
Dan Williams59e64732016-03-08 07:16:07 -080072{
Dan Williamsf284a4f2016-07-07 19:44:50 -070073 struct device *dev = to_dev(pmem);
Dan Williams59e64732016-03-08 07:16:07 -080074 sector_t sector;
75 long cleared;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020076 blk_status_t rc = BLK_STS_OK;
Dan Williams59e64732016-03-08 07:16:07 -080077
78 sector = (offset - pmem->data_offset) / 512;
Dan Williams59e64732016-03-08 07:16:07 -080079
Dan Williams868f036f2016-12-16 08:10:31 -080080 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
81 if (cleared < len)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020082 rc = BLK_STS_IOERR;
Dan Williams59e64732016-03-08 07:16:07 -080083 if (cleared > 0 && cleared / 512) {
Dan Williamsc953cc92018-07-13 21:50:37 -070084 hwpoison_clear(pmem, pmem->phys_addr + offset, cleared);
Dan Williams868f036f2016-12-16 08:10:31 -080085 cleared /= 512;
Dan Williams426824d2018-03-05 16:39:31 -080086 dev_dbg(dev, "%#llx clear %ld sector%s\n",
Dan Williams868f036f2016-12-16 08:10:31 -080087 (unsigned long long) sector, cleared,
88 cleared > 1 ? "s" : "");
Fabian Frederick0a3f27b2016-12-04 10:48:58 -080089 badblocks_clear(&pmem->bb, sector, cleared);
Toshi Kani975750a2017-06-12 16:25:11 -060090 if (pmem->bb_state)
91 sysfs_notify_dirent(pmem->bb_state);
Dan Williams59e64732016-03-08 07:16:07 -080092 }
Toshi Kani3115bb02016-10-13 09:54:21 -060093
Dan Williamsf2b61252017-05-29 23:00:34 -070094 arch_invalidate_pmem(pmem->virt_addr + offset, len);
Dan Williams868f036f2016-12-16 08:10:31 -080095
96 return rc;
Dan Williams59e64732016-03-08 07:16:07 -080097}
98
Vishal Vermabd697a82016-09-30 17:19:30 -060099static void write_pmem(void *pmem_addr, struct page *page,
100 unsigned int off, unsigned int len)
101{
Huang Ying98cc0932017-09-06 16:22:27 -0700102 unsigned int chunk;
103 void *mem;
Vishal Vermabd697a82016-09-30 17:19:30 -0600104
Huang Ying98cc0932017-09-06 16:22:27 -0700105 while (len) {
106 mem = kmap_atomic(page);
Li RongQing9dc64882019-04-04 10:58:01 +0800107 chunk = min_t(unsigned int, len, PAGE_SIZE - off);
Huang Ying98cc0932017-09-06 16:22:27 -0700108 memcpy_flushcache(pmem_addr, mem + off, chunk);
109 kunmap_atomic(mem);
110 len -= chunk;
111 off = 0;
112 page++;
Li RongQing9dc64882019-04-04 10:58:01 +0800113 pmem_addr += chunk;
Huang Ying98cc0932017-09-06 16:22:27 -0700114 }
Vishal Vermabd697a82016-09-30 17:19:30 -0600115}
116
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200117static blk_status_t read_pmem(struct page *page, unsigned int off,
Vishal Vermabd697a82016-09-30 17:19:30 -0600118 void *pmem_addr, unsigned int len)
119{
Huang Ying98cc0932017-09-06 16:22:27 -0700120 unsigned int chunk;
Dan Williams60622d62018-05-03 17:06:21 -0700121 unsigned long rem;
Huang Ying98cc0932017-09-06 16:22:27 -0700122 void *mem;
Vishal Vermabd697a82016-09-30 17:19:30 -0600123
Huang Ying98cc0932017-09-06 16:22:27 -0700124 while (len) {
125 mem = kmap_atomic(page);
Li RongQing9dc64882019-04-04 10:58:01 +0800126 chunk = min_t(unsigned int, len, PAGE_SIZE - off);
Dan Williams60622d62018-05-03 17:06:21 -0700127 rem = memcpy_mcsafe(mem + off, pmem_addr, chunk);
Huang Ying98cc0932017-09-06 16:22:27 -0700128 kunmap_atomic(mem);
Dan Williams60622d62018-05-03 17:06:21 -0700129 if (rem)
Huang Ying98cc0932017-09-06 16:22:27 -0700130 return BLK_STS_IOERR;
131 len -= chunk;
132 off = 0;
133 page++;
Li RongQing9dc64882019-04-04 10:58:01 +0800134 pmem_addr += chunk;
Huang Ying98cc0932017-09-06 16:22:27 -0700135 }
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200136 return BLK_STS_OK;
Vishal Vermabd697a82016-09-30 17:19:30 -0600137}
138
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500139static blk_status_t pmem_do_read(struct pmem_device *pmem,
140 struct page *page, unsigned int page_off,
141 sector_t sector, unsigned int len)
142{
143 blk_status_t rc;
144 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
145 void *pmem_addr = pmem->virt_addr + pmem_off;
146
147 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
148 return BLK_STS_IOERR;
149
150 rc = read_pmem(page, page_off, pmem_addr, len);
151 flush_dcache_page(page);
152 return rc;
153}
154
155static blk_status_t pmem_do_write(struct pmem_device *pmem,
156 struct page *page, unsigned int page_off,
157 sector_t sector, unsigned int len)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200158{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200159 blk_status_t rc = BLK_STS_OK;
Dan Williams59e64732016-03-08 07:16:07 -0800160 bool bad_pmem = false;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400161 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
Dan Williams7a9eb202016-06-03 18:06:47 -0700162 void *pmem_addr = pmem->virt_addr + pmem_off;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200163
Dan Williams59e64732016-03-08 07:16:07 -0800164 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
165 bad_pmem = true;
166
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500167 /*
168 * Note that we write the data both before and after
169 * clearing poison. The write before clear poison
170 * handles situations where the latest written data is
171 * preserved and the clear poison operation simply marks
172 * the address range as valid without changing the data.
173 * In this case application software can assume that an
174 * interrupted write will either return the new good
175 * data or an error.
176 *
177 * However, if pmem_clear_poison() leaves the data in an
178 * indeterminate state we need to perform the write
179 * after clear poison.
180 */
181 flush_dcache_page(page);
182 write_pmem(pmem_addr, page, page_off, len);
183 if (unlikely(bad_pmem)) {
184 rc = pmem_clear_poison(pmem, pmem_off, len);
185 write_pmem(pmem_addr, page, page_off, len);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200186 }
187
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800188 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200189}
190
Jens Axboedece1632015-11-05 10:41:16 -0700191static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200192{
Pankaj Guptac5d43552019-07-05 19:33:22 +0530193 int ret = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200194 blk_status_t rc = 0;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400195 bool do_acct;
196 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -0400197 struct bio_vec bvec;
198 struct bvec_iter iter;
Christoph Hellwig6ec26b82020-05-08 18:15:17 +0200199 struct pmem_device *pmem = bio->bi_disk->private_data;
Dan Williams7e267a82016-06-01 20:48:15 -0700200 struct nd_region *nd_region = to_region(pmem);
201
Ross Zwislerd2d63642018-06-06 10:45:12 -0600202 if (bio->bi_opf & REQ_PREFLUSH)
Pankaj Guptac5d43552019-07-05 19:33:22 +0530203 ret = nvdimm_flush(nd_region, bio);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200204
Dan Williamsf0dc0892015-05-16 12:28:53 -0400205 do_acct = nd_iostat_start(bio, &start);
Dan Williamse10624f2016-01-06 12:03:41 -0800206 bio_for_each_segment(bvec, bio, iter) {
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500207 if (op_is_write(bio_op(bio)))
208 rc = pmem_do_write(pmem, bvec.bv_page, bvec.bv_offset,
209 iter.bi_sector, bvec.bv_len);
210 else
211 rc = pmem_do_read(pmem, bvec.bv_page, bvec.bv_offset,
212 iter.bi_sector, bvec.bv_len);
Dan Williamse10624f2016-01-06 12:03:41 -0800213 if (rc) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200214 bio->bi_status = rc;
Dan Williamse10624f2016-01-06 12:03:41 -0800215 break;
216 }
217 }
Dan Williamsf0dc0892015-05-16 12:28:53 -0400218 if (do_acct)
219 nd_iostat_end(bio, start);
Ross Zwisler61031952015-06-25 03:08:39 -0400220
Jens Axboe1eff9d32016-08-05 15:35:16 -0600221 if (bio->bi_opf & REQ_FUA)
Pankaj Guptac5d43552019-07-05 19:33:22 +0530222 ret = nvdimm_flush(nd_region, bio);
223
224 if (ret)
225 bio->bi_status = errno_to_blk_status(ret);
Ross Zwisler61031952015-06-25 03:08:39 -0400226
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200227 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700228 return BLK_QC_T_NONE;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200229}
230
231static int pmem_rw_page(struct block_device *bdev, sector_t sector,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700232 struct page *page, unsigned int op)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200233{
Christoph Hellwig6ec26b82020-05-08 18:15:17 +0200234 struct pmem_device *pmem = bdev->bd_disk->private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200235 blk_status_t rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200236
Vivek Goyal5d64efe2020-02-28 11:34:51 -0500237 if (op_is_write(op))
238 rc = pmem_do_write(pmem, page, 0, sector,
239 hpage_nr_pages(page) * PAGE_SIZE);
240 else
241 rc = pmem_do_read(pmem, page, 0, sector,
242 hpage_nr_pages(page) * PAGE_SIZE);
Dan Williamse10624f2016-01-06 12:03:41 -0800243 /*
244 * The ->rw_page interface is subtle and tricky. The core
245 * retries on any error, so we can only invoke page_endio() in
246 * the successful completion case. Otherwise, we'll see crashes
247 * caused by double completion.
248 */
249 if (rc == 0)
Tejun Heo3f289dc2018-07-18 04:47:36 -0700250 page_endio(page, op_is_write(op), 0);
Dan Williamse10624f2016-01-06 12:03:41 -0800251
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200252 return blk_status_to_errno(rc);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200253}
254
Dan Williamsf295e532016-06-17 11:08:06 -0700255/* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
Dan Williamsc1d6e822017-01-24 23:02:09 -0800256__weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
257 long nr_pages, void **kaddr, pfn_t *pfn)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200258{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800259 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200260
Dan Williamsc1d6e822017-01-24 23:02:09 -0800261 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
262 PFN_PHYS(nr_pages))))
Dan Williams0a70bd42016-02-24 14:02:11 -0800263 return -EIO;
Huaisheng Ye46a590c2018-07-30 15:15:43 +0800264
265 if (kaddr)
266 *kaddr = pmem->virt_addr + offset;
267 if (pfn)
268 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200269
Dan Williams0a70bd42016-02-24 14:02:11 -0800270 /*
271 * If badblocks are present, limit known good range to the
272 * requested range.
273 */
274 if (unlikely(pmem->bb.count))
Dan Williamsc1d6e822017-01-24 23:02:09 -0800275 return nr_pages;
276 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200277}
278
279static const struct block_device_operations pmem_fops = {
280 .owner = THIS_MODULE,
281 .rw_page = pmem_rw_page,
Dan Williams58138822015-06-23 20:08:34 -0400282 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200283};
284
Vivek Goyalf605a262020-02-28 11:34:52 -0500285static int pmem_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
286 size_t nr_pages)
287{
288 struct pmem_device *pmem = dax_get_private(dax_dev);
289
290 return blk_status_to_errno(pmem_do_write(pmem, ZERO_PAGE(0), 0,
291 PFN_PHYS(pgoff) >> SECTOR_SHIFT,
292 PAGE_SIZE));
293}
294
Dan Williamsc1d6e822017-01-24 23:02:09 -0800295static long pmem_dax_direct_access(struct dax_device *dax_dev,
296 pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
297{
298 struct pmem_device *pmem = dax_get_private(dax_dev);
299
300 return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
301}
302
Dan Williams52f476a2019-05-16 17:05:21 -0700303/*
304 * Use the 'no check' versions of copy_from_iter_flushcache() and
305 * copy_to_iter_mcsafe() to bypass HARDENED_USERCOPY overhead. Bounds
306 * checking, both file offset and device offset, is handled by
307 * dax_iomap_actor()
308 */
Dan Williams0aed55a2017-05-29 12:22:50 -0700309static size_t pmem_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
310 void *addr, size_t bytes, struct iov_iter *i)
311{
Dan Williams52f476a2019-05-16 17:05:21 -0700312 return _copy_from_iter_flushcache(addr, bytes, i);
Dan Williams0aed55a2017-05-29 12:22:50 -0700313}
314
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700315static size_t pmem_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
316 void *addr, size_t bytes, struct iov_iter *i)
317{
Dan Williams52f476a2019-05-16 17:05:21 -0700318 return _copy_to_iter_mcsafe(addr, bytes, i);
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700319}
320
Dan Williamsc1d6e822017-01-24 23:02:09 -0800321static const struct dax_operations pmem_dax_ops = {
322 .direct_access = pmem_dax_direct_access,
Dan Williams7bf7eac2019-05-16 13:26:29 -0700323 .dax_supported = generic_fsdax_supported,
Dan Williams0aed55a2017-05-29 12:22:50 -0700324 .copy_from_iter = pmem_copy_from_iter,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700325 .copy_to_iter = pmem_copy_to_iter,
Vivek Goyalf605a262020-02-28 11:34:52 -0500326 .zero_page_range = pmem_dax_zero_page_range,
Dan Williamsc1d6e822017-01-24 23:02:09 -0800327};
328
Dan Williams6e0c90d2017-06-26 21:28:41 -0700329static const struct attribute_group *pmem_attribute_groups[] = {
330 &dax_attribute_group,
331 NULL,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200332};
333
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200334static void pmem_pagemap_cleanup(struct dev_pagemap *pgmap)
Dan Williams030b99e2016-03-17 20:24:31 -0700335{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200336 struct request_queue *q =
337 container_of(pgmap->ref, struct request_queue, q_usage_counter);
Dan Williams50f44ee2019-06-13 15:56:33 -0700338
Dan Williams030b99e2016-03-17 20:24:31 -0700339 blk_cleanup_queue(q);
340}
341
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200342static void pmem_release_queue(void *pgmap)
Dan Williams50f44ee2019-06-13 15:56:33 -0700343{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200344 pmem_pagemap_cleanup(pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700345}
346
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200347static void pmem_pagemap_kill(struct dev_pagemap *pgmap)
Dan Williams71389702017-04-28 10:23:37 -0700348{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200349 struct request_queue *q =
350 container_of(pgmap->ref, struct request_queue, q_usage_counter);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800351
Linus Torvaldsd3b5d352017-05-01 23:54:56 -0700352 blk_freeze_queue_start(q);
Dan Williams71389702017-04-28 10:23:37 -0700353}
354
Dan Williamsc1d6e822017-01-24 23:02:09 -0800355static void pmem_release_disk(void *__pmem)
Dan Williams030b99e2016-03-17 20:24:31 -0700356{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800357 struct pmem_device *pmem = __pmem;
358
359 kill_dax(pmem->dax_dev);
360 put_dax(pmem->dax_dev);
361 del_gendisk(pmem->disk);
362 put_disk(pmem->disk);
Dan Williams030b99e2016-03-17 20:24:31 -0700363}
364
Christoph Hellwig1e240e82019-06-26 14:27:08 +0200365static const struct dev_pagemap_ops fsdax_pagemap_ops = {
Christoph Hellwig1e240e82019-06-26 14:27:08 +0200366 .kill = pmem_pagemap_kill,
367 .cleanup = pmem_pagemap_cleanup,
368};
369
Dan Williams200c79d2016-03-22 00:22:16 -0700370static int pmem_attach_disk(struct device *dev,
371 struct nd_namespace_common *ndns)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200372{
Dan Williams200c79d2016-03-22 00:22:16 -0700373 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
Dan Williamsf284a4f2016-07-07 19:44:50 -0700374 struct nd_region *nd_region = to_nd_region(dev->parent);
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600375 int nid = dev_to_node(dev), fua;
Dan Williams200c79d2016-03-22 00:22:16 -0700376 struct resource *res = &nsio->res;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100377 struct resource bb_res;
Dan Williams200c79d2016-03-22 00:22:16 -0700378 struct nd_pfn *nd_pfn = NULL;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800379 struct dax_device *dax_dev;
Dan Williams200c79d2016-03-22 00:22:16 -0700380 struct nd_pfn_sb *pfn_sb;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200381 struct pmem_device *pmem;
Dan Williams468ded02016-01-15 16:56:46 -0800382 struct request_queue *q;
Dan Williams6e0c90d2017-06-26 21:28:41 -0700383 struct device *gendev;
Dan Williams200c79d2016-03-22 00:22:16 -0700384 struct gendisk *disk;
385 void *addr;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100386 int rc;
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530387 unsigned long flags = 0UL;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200388
Christoph Hellwig708ab622015-08-10 23:07:08 -0400389 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200390 if (!pmem)
Dan Williams200c79d2016-03-22 00:22:16 -0700391 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200392
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530393 rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
394 if (rc)
395 return rc;
396
Christoph Hellwige8d51342017-12-29 08:54:05 +0100397 /* while nsio_rw_bytes is active, parse a pfn info block if present */
398 if (is_nd_pfn(dev)) {
399 nd_pfn = to_nd_pfn(dev);
400 rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap);
401 if (rc)
402 return rc;
403 }
404
405 /* we're attaching a block device, disable raw namespace access */
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530406 devm_namespace_disable(dev, ndns);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100407
Dan Williams200c79d2016-03-22 00:22:16 -0700408 dev_set_drvdata(dev, pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200409 pmem->phys_addr = res->start;
410 pmem->size = resource_size(res);
Dan Williams0b277962017-06-09 09:46:50 -0700411 fua = nvdimm_has_flush(nd_region);
412 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) || fua < 0) {
Ross Zwisler61031952015-06-25 03:08:39 -0400413 dev_warn(dev, "unable to guarantee persistence of writes\n");
Dan Williams0b277962017-06-09 09:46:50 -0700414 fua = 0;
415 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200416
Dan Williams947df022016-03-21 22:28:40 -0700417 if (!devm_request_mem_region(dev, res->start, resource_size(res),
Dan Williams450c6632016-11-28 11:15:18 -0800418 dev_name(&ndns->dev))) {
Dan Williams947df022016-03-21 22:28:40 -0700419 dev_warn(dev, "could not reserve region %pR\n", res);
Dan Williams200c79d2016-03-22 00:22:16 -0700420 return -EBUSY;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200421 }
422
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100423 q = blk_alloc_queue(pmem_make_request, dev_to_node(dev));
Dan Williams468ded02016-01-15 16:56:46 -0800424 if (!q)
Dan Williams200c79d2016-03-22 00:22:16 -0700425 return -ENOMEM;
Dan Williams468ded02016-01-15 16:56:46 -0800426
Dan Williams34c0fd52016-01-15 16:56:14 -0800427 pmem->pfn_flags = PFN_DEV;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100428 pmem->pgmap.ref = &q->q_usage_counter;
Dan Williams200c79d2016-03-22 00:22:16 -0700429 if (is_nd_pfn(dev)) {
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200430 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
431 pmem->pgmap.ops = &fsdax_pagemap_ops;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100432 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams200c79d2016-03-22 00:22:16 -0700433 pfn_sb = nd_pfn->pfn_sb;
434 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100435 pmem->pfn_pad = resource_size(res) -
436 resource_size(&pmem->pgmap.res);
Dan Williams200c79d2016-03-22 00:22:16 -0700437 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100438 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
439 bb_res.start += pmem->data_offset;
Dan Williams200c79d2016-03-22 00:22:16 -0700440 } else if (pmem_should_map_pages(dev)) {
Christoph Hellwige8d51342017-12-29 08:54:05 +0100441 memcpy(&pmem->pgmap.res, &nsio->res, sizeof(pmem->pgmap.res));
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200442 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
443 pmem->pgmap.ops = &fsdax_pagemap_ops;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100444 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams34c0fd52016-01-15 16:56:14 -0800445 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100446 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
Dan Williams91ed7ac2018-10-04 16:32:08 -0700447 } else {
Dan Williams50f44ee2019-06-13 15:56:33 -0700448 if (devm_add_action_or_reset(dev, pmem_release_queue,
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200449 &pmem->pgmap))
Dan Williams50f44ee2019-06-13 15:56:33 -0700450 return -ENOMEM;
Dan Williams200c79d2016-03-22 00:22:16 -0700451 addr = devm_memremap(dev, pmem->phys_addr,
452 pmem->size, ARCH_MEMREMAP_PMEM);
Dan Williams91ed7ac2018-10-04 16:32:08 -0700453 memcpy(&bb_res, &nsio->res, sizeof(bb_res));
454 }
Dan Williamsb36f4762015-09-15 02:42:20 -0400455
Dan Williams200c79d2016-03-22 00:22:16 -0700456 if (IS_ERR(addr))
457 return PTR_ERR(addr);
Dan Williams7a9eb202016-06-03 18:06:47 -0700458 pmem->virt_addr = addr;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200459
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600460 blk_queue_write_cache(q, true, fua);
Dan Williams5a922892016-03-21 15:43:53 -0700461 blk_queue_physical_block_size(q, PAGE_SIZE);
Dan Williamsf979b132017-06-04 12:12:07 +0900462 blk_queue_logical_block_size(q, pmem_sector_size(ndns));
Dan Williams5a922892016-03-21 15:43:53 -0700463 blk_queue_max_hw_sectors(q, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -0800464 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Ross Zwisler45576412018-06-26 16:30:39 -0600465 if (pmem->pfn_flags & PFN_MAP)
466 blk_queue_flag_set(QUEUE_FLAG_DAX, q);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200467
Dan Williams538ea4a2015-10-05 20:35:56 -0400468 disk = alloc_disk_node(0, nid);
Dan Williams030b99e2016-03-17 20:24:31 -0700469 if (!disk)
470 return -ENOMEM;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800471 pmem->disk = disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200472
Ross Zwisler9e853f22015-04-01 09:12:19 +0200473 disk->fops = &pmem_fops;
Dan Williams5a922892016-03-21 15:43:53 -0700474 disk->queue = q;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200475 disk->flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig6ec26b82020-05-08 18:15:17 +0200476 disk->private_data = pmem;
Minchan Kim23c47d22017-11-15 17:33:00 -0800477 disk->queue->backing_dev_info->capabilities |= BDI_CAP_SYNCHRONOUS_IO;
Vishal Verma5212e112015-06-25 04:20:32 -0400478 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williamscfe30b82016-03-03 09:38:00 -0800479 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
480 / 512);
Dan Williamsb95f5f42016-01-04 23:50:23 -0800481 if (devm_init_badblocks(dev, &pmem->bb))
482 return -ENOMEM;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100483 nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_res);
Dan Williams57f7f312016-01-06 12:03:42 -0800484 disk->bb = &pmem->bb;
Dan Williamsf02716d2016-06-15 14:59:17 -0700485
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530486 if (is_nvdimm_sync(nd_region))
487 flags = DAXDEV_F_SYNC;
488 dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops, flags);
Vivek Goyal4e4ced92020-04-01 12:11:25 -0400489 if (IS_ERR(dax_dev)) {
Dan Williamsc1d6e822017-01-24 23:02:09 -0800490 put_disk(disk);
Vivek Goyal4e4ced92020-04-01 12:11:25 -0400491 return PTR_ERR(dax_dev);
Dan Williamsc1d6e822017-01-24 23:02:09 -0800492 }
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600493 dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
Dan Williamsc1d6e822017-01-24 23:02:09 -0800494 pmem->dax_dev = dax_dev;
Dan Williams6e0c90d2017-06-26 21:28:41 -0700495 gendev = disk_to_dev(disk);
496 gendev->groups = pmem_attribute_groups;
497
Hannes Reineckefef912b2018-09-28 08:17:19 +0200498 device_add_disk(dev, disk, NULL);
Dan Williamsc1d6e822017-01-24 23:02:09 -0800499 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
Dan Williamsf02716d2016-06-15 14:59:17 -0700500 return -ENOMEM;
501
Dan Williams58138822015-06-23 20:08:34 -0400502 revalidate_disk(disk);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200503
Toshi Kani975750a2017-06-12 16:25:11 -0600504 pmem->bb_state = sysfs_get_dirent(disk_to_dev(disk)->kobj.sd,
505 "badblocks");
Dan Williams6aa734a2017-06-30 18:56:03 -0700506 if (!pmem->bb_state)
507 dev_warn(dev, "'badblocks' notification disabled\n");
Toshi Kani975750a2017-06-12 16:25:11 -0600508
Dan Williams8c2f7e82015-06-25 04:20:04 -0400509 return 0;
510}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200511
Dan Williams9f53f9f2015-06-09 15:33:45 -0400512static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200513{
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530514 int ret;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400515 struct nd_namespace_common *ndns;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200516
Dan Williams8c2f7e82015-06-25 04:20:04 -0400517 ndns = nvdimm_namespace_common_probe(dev);
518 if (IS_ERR(ndns))
519 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400520
Dan Williams200c79d2016-03-22 00:22:16 -0700521 if (is_nd_btt(dev))
Christoph Hellwig708ab622015-08-10 23:07:08 -0400522 return nvdimm_namespace_attach_btt(ndns);
523
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400524 if (is_nd_pfn(dev))
Dan Williams200c79d2016-03-22 00:22:16 -0700525 return pmem_attach_disk(dev, ndns);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400526
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530527 ret = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
528 if (ret)
529 return ret;
530
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530531 ret = nd_btt_probe(dev, ndns);
532 if (ret == 0)
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400533 return -ENXIO;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400534
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530535 /*
536 * We have two failure conditions here, there is no
537 * info reserver block or we found a valid info reserve block
538 * but failed to initialize the pfn superblock.
539 *
540 * For the first case consider namespace as a raw pmem namespace
541 * and attach a disk.
542 *
543 * For the latter, consider this a success and advance the namespace
544 * seed.
545 */
546 ret = nd_pfn_probe(dev, ndns);
547 if (ret == 0)
548 return -ENXIO;
549 else if (ret == -EOPNOTSUPP)
550 return ret;
551
552 ret = nd_dax_probe(dev, ndns);
553 if (ret == 0)
554 return -ENXIO;
555 else if (ret == -EOPNOTSUPP)
556 return ret;
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530557
558 /* probe complete, attach handles namespace enabling */
559 devm_namespace_disable(dev, ndns);
560
Dan Williams200c79d2016-03-22 00:22:16 -0700561 return pmem_attach_disk(dev, ndns);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200562}
563
Dan Williams9f53f9f2015-06-09 15:33:45 -0400564static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200565{
Dan Williams6aa734a2017-06-30 18:56:03 -0700566 struct pmem_device *pmem = dev_get_drvdata(dev);
567
Dan Williams8c2f7e82015-06-25 04:20:04 -0400568 if (is_nd_btt(dev))
Dan Williams298f2bc2016-03-15 16:41:04 -0700569 nvdimm_namespace_detach_btt(to_nd_btt(dev));
Dan Williams6aa734a2017-06-30 18:56:03 -0700570 else {
571 /*
Dan Williams87a30e12019-07-17 18:08:26 -0700572 * Note, this assumes nd_device_lock() context to not
573 * race nd_pmem_notify()
Dan Williams6aa734a2017-06-30 18:56:03 -0700574 */
575 sysfs_put(pmem->bb_state);
576 pmem->bb_state = NULL;
577 }
Pankaj Guptac5d43552019-07-05 19:33:22 +0530578 nvdimm_flush(to_nd_region(dev->parent), NULL);
Dan Williams476f8482016-07-09 00:12:52 -0700579
Ross Zwisler9e853f22015-04-01 09:12:19 +0200580 return 0;
581}
582
Dan Williams476f8482016-07-09 00:12:52 -0700583static void nd_pmem_shutdown(struct device *dev)
584{
Pankaj Guptac5d43552019-07-05 19:33:22 +0530585 nvdimm_flush(to_nd_region(dev->parent), NULL);
Dan Williams476f8482016-07-09 00:12:52 -0700586}
587
Dan Williams71999462016-02-18 10:29:49 -0800588static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
589{
Toshi Kanib2518c72017-04-25 17:04:13 -0600590 struct nd_region *nd_region;
Dan Williams298f2bc2016-03-15 16:41:04 -0700591 resource_size_t offset = 0, end_trunc = 0;
592 struct nd_namespace_common *ndns;
593 struct nd_namespace_io *nsio;
594 struct resource res;
Toshi Kanib2518c72017-04-25 17:04:13 -0600595 struct badblocks *bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600596 struct kernfs_node *bb_state;
Dan Williams71999462016-02-18 10:29:49 -0800597
598 if (event != NVDIMM_REVALIDATE_POISON)
599 return;
600
Dan Williams298f2bc2016-03-15 16:41:04 -0700601 if (is_nd_btt(dev)) {
602 struct nd_btt *nd_btt = to_nd_btt(dev);
603
604 ndns = nd_btt->ndns;
Toshi Kanib2518c72017-04-25 17:04:13 -0600605 nd_region = to_nd_region(ndns->dev.parent);
606 nsio = to_nd_namespace_io(&ndns->dev);
607 bb = &nsio->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600608 bb_state = NULL;
Toshi Kanib2518c72017-04-25 17:04:13 -0600609 } else {
610 struct pmem_device *pmem = dev_get_drvdata(dev);
Dan Williamsa3901802016-04-07 20:02:06 -0700611
Toshi Kanib2518c72017-04-25 17:04:13 -0600612 nd_region = to_region(pmem);
613 bb = &pmem->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600614 bb_state = pmem->bb_state;
Dan Williamsa3901802016-04-07 20:02:06 -0700615
Toshi Kanib2518c72017-04-25 17:04:13 -0600616 if (is_nd_pfn(dev)) {
617 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
618 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
619
620 ndns = nd_pfn->ndns;
621 offset = pmem->data_offset +
622 __le32_to_cpu(pfn_sb->start_pad);
623 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
624 } else {
625 ndns = to_ndns(dev);
626 }
627
628 nsio = to_nd_namespace_io(&ndns->dev);
629 }
630
Dan Williams298f2bc2016-03-15 16:41:04 -0700631 res.start = nsio->res.start + offset;
632 res.end = nsio->res.end - end_trunc;
Toshi Kanib2518c72017-04-25 17:04:13 -0600633 nvdimm_badblocks_populate(nd_region, bb, &res);
Toshi Kani975750a2017-06-12 16:25:11 -0600634 if (bb_state)
635 sysfs_notify_dirent(bb_state);
Dan Williams71999462016-02-18 10:29:49 -0800636}
637
Dan Williams9f53f9f2015-06-09 15:33:45 -0400638MODULE_ALIAS("pmem");
639MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400640MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400641static struct nd_device_driver nd_pmem_driver = {
642 .probe = nd_pmem_probe,
643 .remove = nd_pmem_remove,
Dan Williams71999462016-02-18 10:29:49 -0800644 .notify = nd_pmem_notify,
Dan Williams476f8482016-07-09 00:12:52 -0700645 .shutdown = nd_pmem_shutdown,
Dan Williams9f53f9f2015-06-09 15:33:45 -0400646 .drv = {
647 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200648 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400649 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200650};
651
Johannes Thumshirn03e90842018-03-14 19:25:07 +0100652module_nd_driver(nd_pmem_driver);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200653
654MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
655MODULE_LICENSE("GPL v2");