blob: f9f76f6ba07b1ba3283d1d48e6eebcf631e0acc5 [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{
Pankaj Guptac5d43552019-07-05 19:33:22 +0530187 int ret = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200188 blk_status_t rc = 0;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400189 bool do_acct;
190 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -0400191 struct bio_vec bvec;
192 struct bvec_iter iter;
Dan Williamsbd842b82016-03-18 23:47:43 -0700193 struct pmem_device *pmem = q->queuedata;
Dan Williams7e267a82016-06-01 20:48:15 -0700194 struct nd_region *nd_region = to_region(pmem);
195
Ross Zwislerd2d63642018-06-06 10:45:12 -0600196 if (bio->bi_opf & REQ_PREFLUSH)
Pankaj Guptac5d43552019-07-05 19:33:22 +0530197 ret = nvdimm_flush(nd_region, bio);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200198
Dan Williamsf0dc0892015-05-16 12:28:53 -0400199 do_acct = nd_iostat_start(bio, &start);
Dan Williamse10624f2016-01-06 12:03:41 -0800200 bio_for_each_segment(bvec, bio, iter) {
201 rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700202 bvec.bv_offset, bio_op(bio), iter.bi_sector);
Dan Williamse10624f2016-01-06 12:03:41 -0800203 if (rc) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200204 bio->bi_status = rc;
Dan Williamse10624f2016-01-06 12:03:41 -0800205 break;
206 }
207 }
Dan Williamsf0dc0892015-05-16 12:28:53 -0400208 if (do_acct)
209 nd_iostat_end(bio, start);
Ross Zwisler61031952015-06-25 03:08:39 -0400210
Jens Axboe1eff9d32016-08-05 15:35:16 -0600211 if (bio->bi_opf & REQ_FUA)
Pankaj Guptac5d43552019-07-05 19:33:22 +0530212 ret = nvdimm_flush(nd_region, bio);
213
214 if (ret)
215 bio->bi_status = errno_to_blk_status(ret);
Ross Zwisler61031952015-06-25 03:08:39 -0400216
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200217 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700218 return BLK_QC_T_NONE;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200219}
220
221static int pmem_rw_page(struct block_device *bdev, sector_t sector,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700222 struct page *page, unsigned int op)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200223{
Dan Williamsbd842b82016-03-18 23:47:43 -0700224 struct pmem_device *pmem = bdev->bd_queue->queuedata;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200225 blk_status_t rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200226
Huang Ying98cc0932017-09-06 16:22:27 -0700227 rc = pmem_do_bvec(pmem, page, hpage_nr_pages(page) * PAGE_SIZE,
Tejun Heo3f289dc2018-07-18 04:47:36 -0700228 0, op, sector);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200229
Dan Williamse10624f2016-01-06 12:03:41 -0800230 /*
231 * The ->rw_page interface is subtle and tricky. The core
232 * retries on any error, so we can only invoke page_endio() in
233 * the successful completion case. Otherwise, we'll see crashes
234 * caused by double completion.
235 */
236 if (rc == 0)
Tejun Heo3f289dc2018-07-18 04:47:36 -0700237 page_endio(page, op_is_write(op), 0);
Dan Williamse10624f2016-01-06 12:03:41 -0800238
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200239 return blk_status_to_errno(rc);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200240}
241
Dan Williamsf295e532016-06-17 11:08:06 -0700242/* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
Dan Williamsc1d6e822017-01-24 23:02:09 -0800243__weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
244 long nr_pages, void **kaddr, pfn_t *pfn)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200245{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800246 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200247
Dan Williamsc1d6e822017-01-24 23:02:09 -0800248 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
249 PFN_PHYS(nr_pages))))
Dan Williams0a70bd42016-02-24 14:02:11 -0800250 return -EIO;
Huaisheng Ye46a590c2018-07-30 15:15:43 +0800251
252 if (kaddr)
253 *kaddr = pmem->virt_addr + offset;
254 if (pfn)
255 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200256
Dan Williams0a70bd42016-02-24 14:02:11 -0800257 /*
258 * If badblocks are present, limit known good range to the
259 * requested range.
260 */
261 if (unlikely(pmem->bb.count))
Dan Williamsc1d6e822017-01-24 23:02:09 -0800262 return nr_pages;
263 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200264}
265
266static const struct block_device_operations pmem_fops = {
267 .owner = THIS_MODULE,
268 .rw_page = pmem_rw_page,
Dan Williams58138822015-06-23 20:08:34 -0400269 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200270};
271
Dan Williamsc1d6e822017-01-24 23:02:09 -0800272static long pmem_dax_direct_access(struct dax_device *dax_dev,
273 pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
274{
275 struct pmem_device *pmem = dax_get_private(dax_dev);
276
277 return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
278}
279
Dan Williams52f476a2019-05-16 17:05:21 -0700280/*
281 * Use the 'no check' versions of copy_from_iter_flushcache() and
282 * copy_to_iter_mcsafe() to bypass HARDENED_USERCOPY overhead. Bounds
283 * checking, both file offset and device offset, is handled by
284 * dax_iomap_actor()
285 */
Dan Williams0aed55a2017-05-29 12:22:50 -0700286static size_t pmem_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
287 void *addr, size_t bytes, struct iov_iter *i)
288{
Dan Williams52f476a2019-05-16 17:05:21 -0700289 return _copy_from_iter_flushcache(addr, bytes, i);
Dan Williams0aed55a2017-05-29 12:22:50 -0700290}
291
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700292static size_t pmem_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
293 void *addr, size_t bytes, struct iov_iter *i)
294{
Dan Williams52f476a2019-05-16 17:05:21 -0700295 return _copy_to_iter_mcsafe(addr, bytes, i);
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700296}
297
Dan Williamsc1d6e822017-01-24 23:02:09 -0800298static const struct dax_operations pmem_dax_ops = {
299 .direct_access = pmem_dax_direct_access,
Dan Williams7bf7eac2019-05-16 13:26:29 -0700300 .dax_supported = generic_fsdax_supported,
Dan Williams0aed55a2017-05-29 12:22:50 -0700301 .copy_from_iter = pmem_copy_from_iter,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700302 .copy_to_iter = pmem_copy_to_iter,
Dan Williamsc1d6e822017-01-24 23:02:09 -0800303};
304
Dan Williams6e0c90d2017-06-26 21:28:41 -0700305static const struct attribute_group *pmem_attribute_groups[] = {
306 &dax_attribute_group,
307 NULL,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200308};
309
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200310static void pmem_pagemap_cleanup(struct dev_pagemap *pgmap)
Dan Williams030b99e2016-03-17 20:24:31 -0700311{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200312 struct request_queue *q =
313 container_of(pgmap->ref, struct request_queue, q_usage_counter);
Dan Williams50f44ee2019-06-13 15:56:33 -0700314
Dan Williams030b99e2016-03-17 20:24:31 -0700315 blk_cleanup_queue(q);
316}
317
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200318static void pmem_release_queue(void *pgmap)
Dan Williams50f44ee2019-06-13 15:56:33 -0700319{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200320 pmem_pagemap_cleanup(pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700321}
322
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200323static void pmem_pagemap_kill(struct dev_pagemap *pgmap)
Dan Williams71389702017-04-28 10:23:37 -0700324{
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200325 struct request_queue *q =
326 container_of(pgmap->ref, struct request_queue, q_usage_counter);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800327
Linus Torvaldsd3b5d352017-05-01 23:54:56 -0700328 blk_freeze_queue_start(q);
Dan Williams71389702017-04-28 10:23:37 -0700329}
330
Dan Williamsc1d6e822017-01-24 23:02:09 -0800331static void pmem_release_disk(void *__pmem)
Dan Williams030b99e2016-03-17 20:24:31 -0700332{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800333 struct pmem_device *pmem = __pmem;
334
335 kill_dax(pmem->dax_dev);
336 put_dax(pmem->dax_dev);
337 del_gendisk(pmem->disk);
338 put_disk(pmem->disk);
Dan Williams030b99e2016-03-17 20:24:31 -0700339}
340
Christoph Hellwig80a72d02019-06-26 14:27:12 +0200341static void pmem_pagemap_page_free(struct page *page)
Dan Williamse76384882018-05-16 11:46:08 -0700342{
343 wake_up_var(&page->_refcount);
344}
345
Christoph Hellwig1e240e82019-06-26 14:27:08 +0200346static const struct dev_pagemap_ops fsdax_pagemap_ops = {
347 .page_free = pmem_pagemap_page_free,
348 .kill = pmem_pagemap_kill,
349 .cleanup = pmem_pagemap_cleanup,
350};
351
Dan Williams200c79d2016-03-22 00:22:16 -0700352static int pmem_attach_disk(struct device *dev,
353 struct nd_namespace_common *ndns)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200354{
Dan Williams200c79d2016-03-22 00:22:16 -0700355 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
Dan Williamsf284a4f2016-07-07 19:44:50 -0700356 struct nd_region *nd_region = to_nd_region(dev->parent);
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600357 int nid = dev_to_node(dev), fua;
Dan Williams200c79d2016-03-22 00:22:16 -0700358 struct resource *res = &nsio->res;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100359 struct resource bb_res;
Dan Williams200c79d2016-03-22 00:22:16 -0700360 struct nd_pfn *nd_pfn = NULL;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800361 struct dax_device *dax_dev;
Dan Williams200c79d2016-03-22 00:22:16 -0700362 struct nd_pfn_sb *pfn_sb;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200363 struct pmem_device *pmem;
Dan Williams468ded02016-01-15 16:56:46 -0800364 struct request_queue *q;
Dan Williams6e0c90d2017-06-26 21:28:41 -0700365 struct device *gendev;
Dan Williams200c79d2016-03-22 00:22:16 -0700366 struct gendisk *disk;
367 void *addr;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100368 int rc;
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530369 unsigned long flags = 0UL;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200370
Christoph Hellwig708ab622015-08-10 23:07:08 -0400371 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200372 if (!pmem)
Dan Williams200c79d2016-03-22 00:22:16 -0700373 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200374
Christoph Hellwige8d51342017-12-29 08:54:05 +0100375 /* while nsio_rw_bytes is active, parse a pfn info block if present */
376 if (is_nd_pfn(dev)) {
377 nd_pfn = to_nd_pfn(dev);
378 rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap);
379 if (rc)
380 return rc;
381 }
382
383 /* we're attaching a block device, disable raw namespace access */
384 devm_nsio_disable(dev, nsio);
385
Dan Williams200c79d2016-03-22 00:22:16 -0700386 dev_set_drvdata(dev, pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200387 pmem->phys_addr = res->start;
388 pmem->size = resource_size(res);
Dan Williams0b277962017-06-09 09:46:50 -0700389 fua = nvdimm_has_flush(nd_region);
390 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) || fua < 0) {
Ross Zwisler61031952015-06-25 03:08:39 -0400391 dev_warn(dev, "unable to guarantee persistence of writes\n");
Dan Williams0b277962017-06-09 09:46:50 -0700392 fua = 0;
393 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200394
Dan Williams947df022016-03-21 22:28:40 -0700395 if (!devm_request_mem_region(dev, res->start, resource_size(res),
Dan Williams450c6632016-11-28 11:15:18 -0800396 dev_name(&ndns->dev))) {
Dan Williams947df022016-03-21 22:28:40 -0700397 dev_warn(dev, "could not reserve region %pR\n", res);
Dan Williams200c79d2016-03-22 00:22:16 -0700398 return -EBUSY;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200399 }
400
Christoph Hellwig6d469642018-11-14 17:02:18 +0100401 q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
Dan Williams468ded02016-01-15 16:56:46 -0800402 if (!q)
Dan Williams200c79d2016-03-22 00:22:16 -0700403 return -ENOMEM;
Dan Williams468ded02016-01-15 16:56:46 -0800404
Dan Williams34c0fd52016-01-15 16:56:14 -0800405 pmem->pfn_flags = PFN_DEV;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100406 pmem->pgmap.ref = &q->q_usage_counter;
Dan Williams200c79d2016-03-22 00:22:16 -0700407 if (is_nd_pfn(dev)) {
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200408 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
409 pmem->pgmap.ops = &fsdax_pagemap_ops;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100410 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams200c79d2016-03-22 00:22:16 -0700411 pfn_sb = nd_pfn->pfn_sb;
412 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100413 pmem->pfn_pad = resource_size(res) -
414 resource_size(&pmem->pgmap.res);
Dan Williams200c79d2016-03-22 00:22:16 -0700415 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100416 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
417 bb_res.start += pmem->data_offset;
Dan Williams200c79d2016-03-22 00:22:16 -0700418 } else if (pmem_should_map_pages(dev)) {
Christoph Hellwige8d51342017-12-29 08:54:05 +0100419 memcpy(&pmem->pgmap.res, &nsio->res, sizeof(pmem->pgmap.res));
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200420 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
421 pmem->pgmap.ops = &fsdax_pagemap_ops;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100422 addr = devm_memremap_pages(dev, &pmem->pgmap);
Dan Williams34c0fd52016-01-15 16:56:14 -0800423 pmem->pfn_flags |= PFN_MAP;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100424 memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));
Dan Williams91ed7ac2018-10-04 16:32:08 -0700425 } else {
Dan Williams50f44ee2019-06-13 15:56:33 -0700426 if (devm_add_action_or_reset(dev, pmem_release_queue,
Christoph Hellwigd8668bb2019-06-26 14:27:09 +0200427 &pmem->pgmap))
Dan Williams50f44ee2019-06-13 15:56:33 -0700428 return -ENOMEM;
Dan Williams200c79d2016-03-22 00:22:16 -0700429 addr = devm_memremap(dev, pmem->phys_addr,
430 pmem->size, ARCH_MEMREMAP_PMEM);
Dan Williams91ed7ac2018-10-04 16:32:08 -0700431 memcpy(&bb_res, &nsio->res, sizeof(bb_res));
432 }
Dan Williamsb36f4762015-09-15 02:42:20 -0400433
Dan Williams200c79d2016-03-22 00:22:16 -0700434 if (IS_ERR(addr))
435 return PTR_ERR(addr);
Dan Williams7a9eb202016-06-03 18:06:47 -0700436 pmem->virt_addr = addr;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200437
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600438 blk_queue_write_cache(q, true, fua);
Dan Williams5a922892016-03-21 15:43:53 -0700439 blk_queue_make_request(q, pmem_make_request);
440 blk_queue_physical_block_size(q, PAGE_SIZE);
Dan Williamsf979b132017-06-04 12:12:07 +0900441 blk_queue_logical_block_size(q, pmem_sector_size(ndns));
Dan Williams5a922892016-03-21 15:43:53 -0700442 blk_queue_max_hw_sectors(q, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -0800443 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Ross Zwisler45576412018-06-26 16:30:39 -0600444 if (pmem->pfn_flags & PFN_MAP)
445 blk_queue_flag_set(QUEUE_FLAG_DAX, q);
Dan Williams5a922892016-03-21 15:43:53 -0700446 q->queuedata = pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200447
Dan Williams538ea4a2015-10-05 20:35:56 -0400448 disk = alloc_disk_node(0, nid);
Dan Williams030b99e2016-03-17 20:24:31 -0700449 if (!disk)
450 return -ENOMEM;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800451 pmem->disk = disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200452
Ross Zwisler9e853f22015-04-01 09:12:19 +0200453 disk->fops = &pmem_fops;
Dan Williams5a922892016-03-21 15:43:53 -0700454 disk->queue = q;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200455 disk->flags = GENHD_FL_EXT_DEVT;
Minchan Kim23c47d22017-11-15 17:33:00 -0800456 disk->queue->backing_dev_info->capabilities |= BDI_CAP_SYNCHRONOUS_IO;
Vishal Verma5212e112015-06-25 04:20:32 -0400457 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williamscfe30b82016-03-03 09:38:00 -0800458 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
459 / 512);
Dan Williamsb95f5f42016-01-04 23:50:23 -0800460 if (devm_init_badblocks(dev, &pmem->bb))
461 return -ENOMEM;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100462 nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_res);
Dan Williams57f7f312016-01-06 12:03:42 -0800463 disk->bb = &pmem->bb;
Dan Williamsf02716d2016-06-15 14:59:17 -0700464
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530465 if (is_nvdimm_sync(nd_region))
466 flags = DAXDEV_F_SYNC;
467 dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops, flags);
Dan Williamsc1d6e822017-01-24 23:02:09 -0800468 if (!dax_dev) {
469 put_disk(disk);
470 return -ENOMEM;
471 }
Ross Zwislerce7f11a2018-06-06 10:45:13 -0600472 dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
Dan Williamsc1d6e822017-01-24 23:02:09 -0800473 pmem->dax_dev = dax_dev;
Dan Williams6e0c90d2017-06-26 21:28:41 -0700474 gendev = disk_to_dev(disk);
475 gendev->groups = pmem_attribute_groups;
476
Hannes Reineckefef912b2018-09-28 08:17:19 +0200477 device_add_disk(dev, disk, NULL);
Dan Williamsc1d6e822017-01-24 23:02:09 -0800478 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
Dan Williamsf02716d2016-06-15 14:59:17 -0700479 return -ENOMEM;
480
Dan Williams58138822015-06-23 20:08:34 -0400481 revalidate_disk(disk);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200482
Toshi Kani975750a2017-06-12 16:25:11 -0600483 pmem->bb_state = sysfs_get_dirent(disk_to_dev(disk)->kobj.sd,
484 "badblocks");
Dan Williams6aa734a2017-06-30 18:56:03 -0700485 if (!pmem->bb_state)
486 dev_warn(dev, "'badblocks' notification disabled\n");
Toshi Kani975750a2017-06-12 16:25:11 -0600487
Dan Williams8c2f7e82015-06-25 04:20:04 -0400488 return 0;
489}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200490
Dan Williams9f53f9f2015-06-09 15:33:45 -0400491static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200492{
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530493 int ret;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400494 struct nd_namespace_common *ndns;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200495
Dan Williams8c2f7e82015-06-25 04:20:04 -0400496 ndns = nvdimm_namespace_common_probe(dev);
497 if (IS_ERR(ndns))
498 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400499
Dan Williams200c79d2016-03-22 00:22:16 -0700500 if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
501 return -ENXIO;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200502
Dan Williams200c79d2016-03-22 00:22:16 -0700503 if (is_nd_btt(dev))
Christoph Hellwig708ab622015-08-10 23:07:08 -0400504 return nvdimm_namespace_attach_btt(ndns);
505
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400506 if (is_nd_pfn(dev))
Dan Williams200c79d2016-03-22 00:22:16 -0700507 return pmem_attach_disk(dev, ndns);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400508
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530509 ret = nd_btt_probe(dev, ndns);
510 if (ret == 0)
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400511 return -ENXIO;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400512
Aneesh Kumar K.V1c97afa2019-09-05 21:15:58 +0530513 /*
514 * We have two failure conditions here, there is no
515 * info reserver block or we found a valid info reserve block
516 * but failed to initialize the pfn superblock.
517 *
518 * For the first case consider namespace as a raw pmem namespace
519 * and attach a disk.
520 *
521 * For the latter, consider this a success and advance the namespace
522 * seed.
523 */
524 ret = nd_pfn_probe(dev, ndns);
525 if (ret == 0)
526 return -ENXIO;
527 else if (ret == -EOPNOTSUPP)
528 return ret;
529
530 ret = nd_dax_probe(dev, ndns);
531 if (ret == 0)
532 return -ENXIO;
533 else if (ret == -EOPNOTSUPP)
534 return ret;
Dan Williams200c79d2016-03-22 00:22:16 -0700535 return pmem_attach_disk(dev, ndns);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200536}
537
Dan Williams9f53f9f2015-06-09 15:33:45 -0400538static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200539{
Dan Williams6aa734a2017-06-30 18:56:03 -0700540 struct pmem_device *pmem = dev_get_drvdata(dev);
541
Dan Williams8c2f7e82015-06-25 04:20:04 -0400542 if (is_nd_btt(dev))
Dan Williams298f2bc2016-03-15 16:41:04 -0700543 nvdimm_namespace_detach_btt(to_nd_btt(dev));
Dan Williams6aa734a2017-06-30 18:56:03 -0700544 else {
545 /*
Dan Williams87a30e12019-07-17 18:08:26 -0700546 * Note, this assumes nd_device_lock() context to not
547 * race nd_pmem_notify()
Dan Williams6aa734a2017-06-30 18:56:03 -0700548 */
549 sysfs_put(pmem->bb_state);
550 pmem->bb_state = NULL;
551 }
Pankaj Guptac5d43552019-07-05 19:33:22 +0530552 nvdimm_flush(to_nd_region(dev->parent), NULL);
Dan Williams476f8482016-07-09 00:12:52 -0700553
Ross Zwisler9e853f22015-04-01 09:12:19 +0200554 return 0;
555}
556
Dan Williams476f8482016-07-09 00:12:52 -0700557static void nd_pmem_shutdown(struct device *dev)
558{
Pankaj Guptac5d43552019-07-05 19:33:22 +0530559 nvdimm_flush(to_nd_region(dev->parent), NULL);
Dan Williams476f8482016-07-09 00:12:52 -0700560}
561
Dan Williams71999462016-02-18 10:29:49 -0800562static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
563{
Toshi Kanib2518c72017-04-25 17:04:13 -0600564 struct nd_region *nd_region;
Dan Williams298f2bc2016-03-15 16:41:04 -0700565 resource_size_t offset = 0, end_trunc = 0;
566 struct nd_namespace_common *ndns;
567 struct nd_namespace_io *nsio;
568 struct resource res;
Toshi Kanib2518c72017-04-25 17:04:13 -0600569 struct badblocks *bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600570 struct kernfs_node *bb_state;
Dan Williams71999462016-02-18 10:29:49 -0800571
572 if (event != NVDIMM_REVALIDATE_POISON)
573 return;
574
Dan Williams298f2bc2016-03-15 16:41:04 -0700575 if (is_nd_btt(dev)) {
576 struct nd_btt *nd_btt = to_nd_btt(dev);
577
578 ndns = nd_btt->ndns;
Toshi Kanib2518c72017-04-25 17:04:13 -0600579 nd_region = to_nd_region(ndns->dev.parent);
580 nsio = to_nd_namespace_io(&ndns->dev);
581 bb = &nsio->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600582 bb_state = NULL;
Toshi Kanib2518c72017-04-25 17:04:13 -0600583 } else {
584 struct pmem_device *pmem = dev_get_drvdata(dev);
Dan Williamsa3901802016-04-07 20:02:06 -0700585
Toshi Kanib2518c72017-04-25 17:04:13 -0600586 nd_region = to_region(pmem);
587 bb = &pmem->bb;
Toshi Kani975750a2017-06-12 16:25:11 -0600588 bb_state = pmem->bb_state;
Dan Williamsa3901802016-04-07 20:02:06 -0700589
Toshi Kanib2518c72017-04-25 17:04:13 -0600590 if (is_nd_pfn(dev)) {
591 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
592 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
593
594 ndns = nd_pfn->ndns;
595 offset = pmem->data_offset +
596 __le32_to_cpu(pfn_sb->start_pad);
597 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
598 } else {
599 ndns = to_ndns(dev);
600 }
601
602 nsio = to_nd_namespace_io(&ndns->dev);
603 }
604
Dan Williams298f2bc2016-03-15 16:41:04 -0700605 res.start = nsio->res.start + offset;
606 res.end = nsio->res.end - end_trunc;
Toshi Kanib2518c72017-04-25 17:04:13 -0600607 nvdimm_badblocks_populate(nd_region, bb, &res);
Toshi Kani975750a2017-06-12 16:25:11 -0600608 if (bb_state)
609 sysfs_notify_dirent(bb_state);
Dan Williams71999462016-02-18 10:29:49 -0800610}
611
Dan Williams9f53f9f2015-06-09 15:33:45 -0400612MODULE_ALIAS("pmem");
613MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400614MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400615static struct nd_device_driver nd_pmem_driver = {
616 .probe = nd_pmem_probe,
617 .remove = nd_pmem_remove,
Dan Williams71999462016-02-18 10:29:49 -0800618 .notify = nd_pmem_notify,
Dan Williams476f8482016-07-09 00:12:52 -0700619 .shutdown = nd_pmem_shutdown,
Dan Williams9f53f9f2015-06-09 15:33:45 -0400620 .drv = {
621 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200622 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400623 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200624};
625
Johannes Thumshirn03e90842018-03-14 19:25:07 +0100626module_nd_driver(nd_pmem_driver);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200627
628MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
629MODULE_LICENSE("GPL v2");