blob: 24d7fe7c74ed03e3bc528226e22a04dab7af2b1f [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"
Dave Jiang06e8ccd2018-01-31 12:45:38 -070031#include "nd-core.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 Williams60622d62018-05-03 17:06:21 -0700128 rem = memcpy_mcsafe(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
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200140static blk_status_t pmem_do_bvec(struct pmem_device *pmem, struct page *page,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700141 unsigned int len, unsigned int off, unsigned int op,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200142 sector_t sector)
143{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200144 blk_status_t rc = BLK_STS_OK;
Dan Williams59e64732016-03-08 07:16:07 -0800145 bool bad_pmem = false;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400146 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
Dan Williams7a9eb202016-06-03 18:06:47 -0700147 void *pmem_addr = pmem->virt_addr + pmem_off;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200148
Dan Williams59e64732016-03-08 07:16:07 -0800149 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
150 bad_pmem = true;
151
Tejun Heo3f289dc2018-07-18 04:47:36 -0700152 if (!op_is_write(op)) {
Dan Williams59e64732016-03-08 07:16:07 -0800153 if (unlikely(bad_pmem))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200154 rc = BLK_STS_IOERR;
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800155 else {
Vishal Vermabd697a82016-09-30 17:19:30 -0600156 rc = read_pmem(page, off, pmem_addr, len);
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800157 flush_dcache_page(page);
158 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200159 } else {
Dan Williams0a370d262016-04-14 19:40:47 -0700160 /*
161 * Note that we write the data both before and after
162 * clearing poison. The write before clear poison
163 * handles situations where the latest written data is
164 * preserved and the clear poison operation simply marks
165 * the address range as valid without changing the data.
166 * In this case application software can assume that an
167 * interrupted write will either return the new good
168 * data or an error.
169 *
170 * However, if pmem_clear_poison() leaves the data in an
171 * indeterminate state we need to perform the write
172 * after clear poison.
173 */
Ross Zwisler9e853f22015-04-01 09:12:19 +0200174 flush_dcache_page(page);
Vishal Vermabd697a82016-09-30 17:19:30 -0600175 write_pmem(pmem_addr, page, off, len);
Dan Williams59e64732016-03-08 07:16:07 -0800176 if (unlikely(bad_pmem)) {
Toshi Kani3115bb02016-10-13 09:54:21 -0600177 rc = pmem_clear_poison(pmem, pmem_off, len);
Vishal Vermabd697a82016-09-30 17:19:30 -0600178 write_pmem(pmem_addr, page, off, len);
Dan Williams59e64732016-03-08 07:16:07 -0800179 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200180 }
181
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800182 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200183}
184
Jens Axboedece1632015-11-05 10:41:16 -0700185static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200186{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200187 blk_status_t rc = 0;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400188 bool do_acct;
189 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -0400190 struct bio_vec bvec;
191 struct bvec_iter iter;
Dan Williamsbd842b82016-03-18 23:47:43 -0700192 struct pmem_device *pmem = q->queuedata;
Dan Williams7e267a82016-06-01 20:48:15 -0700193 struct nd_region *nd_region = to_region(pmem);
194
Ross Zwislerd2d63642018-06-06 10:45:12 -0600195 if (bio->bi_opf & REQ_PREFLUSH)
Dan Williams7e267a82016-06-01 20:48:15 -0700196 nvdimm_flush(nd_region);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200197
Dan Williamsf0dc0892015-05-16 12:28:53 -0400198 do_acct = nd_iostat_start(bio, &start);
Dan Williamse10624f2016-01-06 12:03:41 -0800199 bio_for_each_segment(bvec, bio, iter) {
200 rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700201 bvec.bv_offset, bio_op(bio), iter.bi_sector);
Dan Williamse10624f2016-01-06 12:03:41 -0800202 if (rc) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200203 bio->bi_status = rc;
Dan Williamse10624f2016-01-06 12:03:41 -0800204 break;
205 }
206 }
Dan Williamsf0dc0892015-05-16 12:28:53 -0400207 if (do_acct)
208 nd_iostat_end(bio, start);
Ross Zwisler61031952015-06-25 03:08:39 -0400209
Jens Axboe1eff9d32016-08-05 15:35:16 -0600210 if (bio->bi_opf & REQ_FUA)
Dan Williams7e267a82016-06-01 20:48:15 -0700211 nvdimm_flush(nd_region);
Ross Zwisler61031952015-06-25 03:08:39 -0400212
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200213 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700214 return BLK_QC_T_NONE;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200215}
216
217static int pmem_rw_page(struct block_device *bdev, sector_t sector,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700218 struct page *page, unsigned int op)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200219{
Dan Williamsbd842b82016-03-18 23:47:43 -0700220 struct pmem_device *pmem = bdev->bd_queue->queuedata;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200221 blk_status_t rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200222
Huang Ying98cc0932017-09-06 16:22:27 -0700223 rc = pmem_do_bvec(pmem, page, hpage_nr_pages(page) * PAGE_SIZE,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700224 0, op, sector);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200225
Dan Williamse10624f2016-01-06 12:03:41 -0800226 /*
227 * The ->rw_page interface is subtle and tricky. The core
228 * retries on any error, so we can only invoke page_endio() in
229 * the successful completion case. Otherwise, we'll see crashes
230 * caused by double completion.
231 */
232 if (rc == 0)
Tejun Heo3f289dc2018-07-18 04:47:36 -0700233 page_endio(page, op_is_write(op), 0);
Dan Williamse10624f2016-01-06 12:03:41 -0800234
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200235 return blk_status_to_errno(rc);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200236}
237
Dan Williamsf295e532016-06-17 11:08:06 -0700238/* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
Dan Williamsc1d6e822017-01-24 23:02:09 -0800239__weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
240 long nr_pages, void **kaddr, pfn_t *pfn)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200241{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800242 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200243
Dan Williamsc1d6e822017-01-24 23:02:09 -0800244 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
245 PFN_PHYS(nr_pages))))
Dan Williams0a70bd42016-02-24 14:02:11 -0800246 return -EIO;
Huaisheng Ye46a590c2018-07-30 15:15:43 +0800247
248 if (kaddr)
249 *kaddr = pmem->virt_addr + offset;
250 if (pfn)
251 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200252
Dan Williams0a70bd42016-02-24 14:02:11 -0800253 /*
254 * If badblocks are present, limit known good range to the
255 * requested range.
256 */
257 if (unlikely(pmem->bb.count))
Dan Williamsc1d6e822017-01-24 23:02:09 -0800258 return nr_pages;
259 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200260}
261
262static const struct block_device_operations pmem_fops = {
263 .owner = THIS_MODULE,
264 .rw_page = pmem_rw_page,
Dan Williams58138822015-06-23 20:08:34 -0400265 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200266};
267
Dan Williamsc1d6e822017-01-24 23:02:09 -0800268static long pmem_dax_direct_access(struct dax_device *dax_dev,
269 pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
270{
271 struct pmem_device *pmem = dax_get_private(dax_dev);
272
273 return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
274}
275
Dan Williams52f476a2019-05-16 17:05:21 -0700276/*
277 * Use the 'no check' versions of copy_from_iter_flushcache() and
278 * copy_to_iter_mcsafe() to bypass HARDENED_USERCOPY overhead. Bounds
279 * checking, both file offset and device offset, is handled by
280 * dax_iomap_actor()
281 */
Dan Williams0aed55a2017-05-29 12:22:50 -0700282static size_t pmem_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
283 void *addr, size_t bytes, struct iov_iter *i)
284{
Dan Williams52f476a2019-05-16 17:05:21 -0700285 return _copy_from_iter_flushcache(addr, bytes, i);
Dan Williams0aed55a2017-05-29 12:22:50 -0700286}
287
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700288static size_t pmem_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
289 void *addr, size_t bytes, struct iov_iter *i)
290{
Dan Williams52f476a2019-05-16 17:05:21 -0700291 return _copy_to_iter_mcsafe(addr, bytes, i);
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700292}
293
Dan Williamsc1d6e822017-01-24 23:02:09 -0800294static const struct dax_operations pmem_dax_ops = {
295 .direct_access = pmem_dax_direct_access,
Dan Williams7bf7eac2019-05-16 13:26:29 -0700296 .dax_supported = generic_fsdax_supported,
Dan Williams0aed55a2017-05-29 12:22:50 -0700297 .copy_from_iter = pmem_copy_from_iter,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700298 .copy_to_iter = pmem_copy_to_iter,
Dan Williamsc1d6e822017-01-24 23:02:09 -0800299};
300
Dan Williams6e0c90d2017-06-26 21:28:41 -0700301static const struct attribute_group *pmem_attribute_groups[] = {
302 &dax_attribute_group,
303 NULL,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200304};
305
Dan Williams50f44ee2019-06-13 15:56:33 -0700306static void __pmem_release_queue(struct percpu_ref *ref)
Dan Williams030b99e2016-03-17 20:24:31 -0700307{
Dan Williams50f44ee2019-06-13 15:56:33 -0700308 struct request_queue *q;
309
310 q = container_of(ref, typeof(*q), q_usage_counter);
Dan Williams030b99e2016-03-17 20:24:31 -0700311 blk_cleanup_queue(q);
312}
313
Dan Williams50f44ee2019-06-13 15:56:33 -0700314static void pmem_release_queue(void *ref)
315{
316 __pmem_release_queue(ref);
317}
318
Dan Williamsa95c90f2018-12-28 00:34:57 -0800319static void pmem_freeze_queue(struct percpu_ref *ref)
Dan Williams71389702017-04-28 10:23:37 -0700320{
Dan Williamsa95c90f2018-12-28 00:34:57 -0800321 struct request_queue *q;
322
323 q = container_of(ref, typeof(*q), q_usage_counter);
Linus Torvaldsd3b5d352017-05-01 23:54:56 -0700324 blk_freeze_queue_start(q);
Dan Williams71389702017-04-28 10:23:37 -0700325}
326
Dan Williamsc1d6e822017-01-24 23:02:09 -0800327static void pmem_release_disk(void *__pmem)
Dan Williams030b99e2016-03-17 20:24:31 -0700328{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800329 struct pmem_device *pmem = __pmem;
330
331 kill_dax(pmem->dax_dev);
332 put_dax(pmem->dax_dev);
333 del_gendisk(pmem->disk);
334 put_disk(pmem->disk);
Dan Williams030b99e2016-03-17 20:24:31 -0700335}
336
Dan Williamse76384882018-05-16 11:46:08 -0700337static void pmem_release_pgmap_ops(void *__pgmap)
338{
339 dev_pagemap_put_ops();
340}
341
342static void fsdax_pagefree(struct page *page, void *data)
343{
344 wake_up_var(&page->_refcount);
345}
346
347static int setup_pagemap_fsdax(struct device *dev, struct dev_pagemap *pgmap)
348{
349 dev_pagemap_get_ops();
350 if (devm_add_action_or_reset(dev, pmem_release_pgmap_ops, pgmap))
351 return -ENOMEM;
352 pgmap->type = MEMORY_DEVICE_FS_DAX;
353 pgmap->page_free = fsdax_pagefree;
354
355 return 0;
356}
357
Dan Williams200c79d2016-03-22 00:22:16 -0700358static int pmem_attach_disk(struct device *dev,
359 struct nd_namespace_common *ndns)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200360{
Dan Williams200c79d2016-03-22 00:22:16 -0700361 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
Dan Williamsf284a4f2016-07-07 19:44:50 -0700362 struct nd_region *nd_region = to_nd_region(dev->parent);
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600363 int nid = dev_to_node(dev), fua;
Dan Williams200c79d2016-03-22 00:22:16 -0700364 struct resource *res = &nsio->res;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100365 struct resource bb_res;
Dan Williams200c79d2016-03-22 00:22:16 -0700366 struct nd_pfn *nd_pfn = NULL;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800367 struct dax_device *dax_dev;
Dan Williams200c79d2016-03-22 00:22:16 -0700368 struct nd_pfn_sb *pfn_sb;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200369 struct pmem_device *pmem;
Dan Williams468ded02016-01-15 16:56:46 -0800370 struct request_queue *q;
Dan Williams6e0c90d2017-06-26 21:28:41 -0700371 struct device *gendev;
Dan Williams200c79d2016-03-22 00:22:16 -0700372 struct gendisk *disk;
373 void *addr;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100374 int rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200375
Christoph Hellwig708ab622015-08-10 23:07:08 -0400376 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200377 if (!pmem)
Dan Williams200c79d2016-03-22 00:22:16 -0700378 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200379
Christoph Hellwige8d51342017-12-29 08:54:05 +0100380 /* while nsio_rw_bytes is active, parse a pfn info block if present */
381 if (is_nd_pfn(dev)) {
382 nd_pfn = to_nd_pfn(dev);
383 rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap);
384 if (rc)
385 return rc;
386 }
387
388 /* we're attaching a block device, disable raw namespace access */
389 devm_nsio_disable(dev, nsio);
390
Dan Williams200c79d2016-03-22 00:22:16 -0700391 dev_set_drvdata(dev, pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200392 pmem->phys_addr = res->start;
393 pmem->size = resource_size(res);
Dan Williams0b277962017-06-09 09:46:50 -0700394 fua = nvdimm_has_flush(nd_region);
395 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) || fua < 0) {
Ross Zwisler61031952015-06-25 03:08:39 -0400396 dev_warn(dev, "unable to guarantee persistence of writes\n");
Dan Williams0b277962017-06-09 09:46:50 -0700397 fua = 0;
398 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200399
Dan Williams947df022016-03-21 22:28:40 -0700400 if (!devm_request_mem_region(dev, res->start, resource_size(res),
Dan Williams450c6632016-11-28 11:15:18 -0800401 dev_name(&ndns->dev))) {
Dan Williams947df022016-03-21 22:28:40 -0700402 dev_warn(dev, "could not reserve region %pR\n", res);
Dan Williams200c79d2016-03-22 00:22:16 -0700403 return -EBUSY;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200404 }
405
Christoph Hellwig6d469642018-11-14 17:02:18 +0100406 q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
Dan Williams468ded02016-01-15 16:56:46 -0800407 if (!q)
Dan Williams200c79d2016-03-22 00:22:16 -0700408 return -ENOMEM;
Dan Williams468ded02016-01-15 16:56:46 -0800409
Dan Williams34c0fd52016-01-15 16:56:14 -0800410 pmem->pfn_flags = PFN_DEV;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100411 pmem->pgmap.ref = &q->q_usage_counter;
Dan Williamsa95c90f2018-12-28 00:34:57 -0800412 pmem->pgmap.kill = pmem_freeze_queue;
Dan Williams50f44ee2019-06-13 15:56:33 -0700413 pmem->pgmap.cleanup = __pmem_release_queue;
Dan Williams200c79d2016-03-22 00:22:16 -0700414 if (is_nd_pfn(dev)) {
Dan Williamse76384882018-05-16 11:46:08 -0700415 if (setup_pagemap_fsdax(dev, &pmem->pgmap))
416 return -ENOMEM;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100417 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams200c79d2016-03-22 00:22:16 -0700418 pfn_sb = nd_pfn->pfn_sb;
419 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100420 pmem->pfn_pad = resource_size(res) -
421 resource_size(&pmem->pgmap.res);
Dan Williams200c79d2016-03-22 00:22:16 -0700422 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100423 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
424 bb_res.start += pmem->data_offset;
Dan Williams200c79d2016-03-22 00:22:16 -0700425 } else if (pmem_should_map_pages(dev)) {
Christoph Hellwige8d51342017-12-29 08:54:05 +0100426 memcpy(&pmem->pgmap.res, &nsio->res, sizeof(pmem->pgmap.res));
427 pmem->pgmap.altmap_valid = false;
Dan Williamse76384882018-05-16 11:46:08 -0700428 if (setup_pagemap_fsdax(dev, &pmem->pgmap))
429 return -ENOMEM;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100430 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams34c0fd52016-01-15 16:56:14 -0800431 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100432 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
Dan Williams91ed7ac2018-10-04 16:32:08 -0700433 } else {
Dan Williams50f44ee2019-06-13 15:56:33 -0700434 if (devm_add_action_or_reset(dev, pmem_release_queue,
435 &q->q_usage_counter))
436 return -ENOMEM;
Dan Williams200c79d2016-03-22 00:22:16 -0700437 addr = devm_memremap(dev, pmem->phys_addr,
438 pmem->size, ARCH_MEMREMAP_PMEM);
Dan Williams91ed7ac2018-10-04 16:32:08 -0700439 memcpy(&bb_res, &nsio->res, sizeof(bb_res));
440 }
Dan Williamsb36f4762015-09-15 02:42:20 -0400441
Dan Williams200c79d2016-03-22 00:22:16 -0700442 if (IS_ERR(addr))
443 return PTR_ERR(addr);
Dan Williams7a9eb202016-06-03 18:06:47 -0700444 pmem->virt_addr = addr;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200445
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600446 blk_queue_write_cache(q, true, fua);
Dan Williams5a922892016-03-21 15:43:53 -0700447 blk_queue_make_request(q, pmem_make_request);
448 blk_queue_physical_block_size(q, PAGE_SIZE);
Dan Williamsf979b132017-06-04 12:12:07 +0900449 blk_queue_logical_block_size(q, pmem_sector_size(ndns));
Dan Williams5a922892016-03-21 15:43:53 -0700450 blk_queue_max_hw_sectors(q, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -0800451 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Ross Zwisler45576412018-06-26 16:30:39 -0600452 if (pmem->pfn_flags & PFN_MAP)
453 blk_queue_flag_set(QUEUE_FLAG_DAX, q);
Dan Williams5a922892016-03-21 15:43:53 -0700454 q->queuedata = pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200455
Dan Williams538ea4a2015-10-05 20:35:56 -0400456 disk = alloc_disk_node(0, nid);
Dan Williams030b99e2016-03-17 20:24:31 -0700457 if (!disk)
458 return -ENOMEM;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800459 pmem->disk = disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200460
Ross Zwisler9e853f22015-04-01 09:12:19 +0200461 disk->fops = &pmem_fops;
Dan Williams5a922892016-03-21 15:43:53 -0700462 disk->queue = q;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200463 disk->flags = GENHD_FL_EXT_DEVT;
Minchan Kim23c47d22017-11-15 17:33:00 -0800464 disk->queue->backing_dev_info->capabilities |= BDI_CAP_SYNCHRONOUS_IO;
Vishal Verma5212e112015-06-25 04:20:32 -0400465 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williamscfe30b82016-03-03 09:38:00 -0800466 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
467 / 512);
Dan Williamsb95f5f42016-01-04 23:50:23 -0800468 if (devm_init_badblocks(dev, &pmem->bb))
469 return -ENOMEM;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100470 nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_res);
Dan Williams57f7f312016-01-06 12:03:42 -0800471 disk->bb = &pmem->bb;
Dan Williamsf02716d2016-06-15 14:59:17 -0700472
Dan Williamsc1d6e822017-01-24 23:02:09 -0800473 dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops);
474 if (!dax_dev) {
475 put_disk(disk);
476 return -ENOMEM;
477 }
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600478 dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
Dan Williamsc1d6e822017-01-24 23:02:09 -0800479 pmem->dax_dev = dax_dev;
480
Dan Williams6e0c90d2017-06-26 21:28:41 -0700481 gendev = disk_to_dev(disk);
482 gendev->groups = pmem_attribute_groups;
483
Hannes Reineckefef912b2018-09-28 08:17:19 +0200484 device_add_disk(dev, disk, NULL);
Dan Williamsc1d6e822017-01-24 23:02:09 -0800485 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
Dan Williamsf02716d2016-06-15 14:59:17 -0700486 return -ENOMEM;
487
Dan Williams58138822015-06-23 20:08:34 -0400488 revalidate_disk(disk);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200489
Toshi Kani975750a2017-06-12 16:25:11 -0600490 pmem->bb_state = sysfs_get_dirent(disk_to_dev(disk)->kobj.sd,
491 "badblocks");
Dan Williams6aa734a2017-06-30 18:56:03 -0700492 if (!pmem->bb_state)
493 dev_warn(dev, "'badblocks' notification disabled\n");
Toshi Kani975750a2017-06-12 16:25:11 -0600494
Dan Williams8c2f7e82015-06-25 04:20:04 -0400495 return 0;
496}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200497
Dan Williams9f53f9f2015-06-09 15:33:45 -0400498static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200499{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400500 struct nd_namespace_common *ndns;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200501
Dan Williams8c2f7e82015-06-25 04:20:04 -0400502 ndns = nvdimm_namespace_common_probe(dev);
503 if (IS_ERR(ndns))
504 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400505
Dan Williams200c79d2016-03-22 00:22:16 -0700506 if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
507 return -ENXIO;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200508
Dan Williams200c79d2016-03-22 00:22:16 -0700509 if (is_nd_btt(dev))
Christoph Hellwig708ab622015-08-10 23:07:08 -0400510 return nvdimm_namespace_attach_btt(ndns);
511
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400512 if (is_nd_pfn(dev))
Dan Williams200c79d2016-03-22 00:22:16 -0700513 return pmem_attach_disk(dev, ndns);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400514
Dan Williams200c79d2016-03-22 00:22:16 -0700515 /* if we find a valid info-block we'll come back as that personality */
Dan Williamsc5ed9262016-05-18 14:50:12 -0700516 if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
517 || nd_dax_probe(dev, ndns) == 0)
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400518 return -ENXIO;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400519
Dan Williams200c79d2016-03-22 00:22:16 -0700520 /* ...otherwise we're just a raw pmem device */
521 return pmem_attach_disk(dev, ndns);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200522}
523
Dan Williams9f53f9f2015-06-09 15:33:45 -0400524static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200525{
Dan Williams6aa734a2017-06-30 18:56:03 -0700526 struct pmem_device *pmem = dev_get_drvdata(dev);
527
Dan Williams8c2f7e82015-06-25 04:20:04 -0400528 if (is_nd_btt(dev))
Dan Williams298f2bc2016-03-15 16:41:04 -0700529 nvdimm_namespace_detach_btt(to_nd_btt(dev));
Dan Williams6aa734a2017-06-30 18:56:03 -0700530 else {
531 /*
532 * Note, this assumes device_lock() context to not race
533 * nd_pmem_notify()
534 */
535 sysfs_put(pmem->bb_state);
536 pmem->bb_state = NULL;
537 }
Dan Williams476f8482016-07-09 00:12:52 -0700538 nvdimm_flush(to_nd_region(dev->parent));
539
Ross Zwisler9e853f22015-04-01 09:12:19 +0200540 return 0;
541}
542
Dan Williams476f8482016-07-09 00:12:52 -0700543static void nd_pmem_shutdown(struct device *dev)
544{
545 nvdimm_flush(to_nd_region(dev->parent));
546}
547
Dan Williams71999462016-02-18 10:29:49 -0800548static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
549{
Toshi Kanib2518c72017-04-25 17:04:13 -0600550 struct nd_region *nd_region;
Dan Williams298f2bc2016-03-15 16:41:04 -0700551 resource_size_t offset = 0, end_trunc = 0;
552 struct nd_namespace_common *ndns;
553 struct nd_namespace_io *nsio;
554 struct resource res;
Toshi Kanib2518c72017-04-25 17:04:13 -0600555 struct badblocks *bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600556 struct kernfs_node *bb_state;
Dan Williams71999462016-02-18 10:29:49 -0800557
558 if (event != NVDIMM_REVALIDATE_POISON)
559 return;
560
Dan Williams298f2bc2016-03-15 16:41:04 -0700561 if (is_nd_btt(dev)) {
562 struct nd_btt *nd_btt = to_nd_btt(dev);
563
564 ndns = nd_btt->ndns;
Toshi Kanib2518c72017-04-25 17:04:13 -0600565 nd_region = to_nd_region(ndns->dev.parent);
566 nsio = to_nd_namespace_io(&ndns->dev);
567 bb = &nsio->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600568 bb_state = NULL;
Toshi Kanib2518c72017-04-25 17:04:13 -0600569 } else {
570 struct pmem_device *pmem = dev_get_drvdata(dev);
Dan Williamsa3901802016-04-07 20:02:06 -0700571
Toshi Kanib2518c72017-04-25 17:04:13 -0600572 nd_region = to_region(pmem);
573 bb = &pmem->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600574 bb_state = pmem->bb_state;
Dan Williamsa3901802016-04-07 20:02:06 -0700575
Toshi Kanib2518c72017-04-25 17:04:13 -0600576 if (is_nd_pfn(dev)) {
577 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
578 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
579
580 ndns = nd_pfn->ndns;
581 offset = pmem->data_offset +
582 __le32_to_cpu(pfn_sb->start_pad);
583 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
584 } else {
585 ndns = to_ndns(dev);
586 }
587
588 nsio = to_nd_namespace_io(&ndns->dev);
589 }
590
Dan Williams298f2bc2016-03-15 16:41:04 -0700591 res.start = nsio->res.start + offset;
592 res.end = nsio->res.end - end_trunc;
Toshi Kanib2518c72017-04-25 17:04:13 -0600593 nvdimm_badblocks_populate(nd_region, bb, &res);
Toshi Kani975750a2017-06-12 16:25:11 -0600594 if (bb_state)
595 sysfs_notify_dirent(bb_state);
Dan Williams71999462016-02-18 10:29:49 -0800596}
597
Dan Williams9f53f9f2015-06-09 15:33:45 -0400598MODULE_ALIAS("pmem");
599MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400600MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400601static struct nd_device_driver nd_pmem_driver = {
602 .probe = nd_pmem_probe,
603 .remove = nd_pmem_remove,
Dan Williams71999462016-02-18 10:29:49 -0800604 .notify = nd_pmem_notify,
Dan Williams476f8482016-07-09 00:12:52 -0700605 .shutdown = nd_pmem_shutdown,
Dan Williams9f53f9f2015-06-09 15:33:45 -0400606 .drv = {
607 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200608 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400609 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200610};
611
Johannes Thumshirn03e90842018-03-14 19:25:07 +0100612module_nd_driver(nd_pmem_driver);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200613
614MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
615MODULE_LICENSE("GPL v2");