blob: 823b07774244431261c82eab49d5816f39b26d56 [file] [log] [blame]
Ross Zwisler9e853f22015-04-01 09:12:19 +02001/*
2 * Persistent Memory Driver
3 *
Dan Williams9f53f9f2015-06-09 15:33:45 -04004 * Copyright (c) 2014-2015, Intel Corporation.
Ross Zwisler9e853f22015-04-01 09:12:19 +02005 * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
6 * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 */
17
18#include <asm/cacheflush.h>
19#include <linux/blkdev.h>
20#include <linux/hdreg.h>
21#include <linux/init.h>
22#include <linux/platform_device.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
Dan Williamsb95f5f42016-01-04 23:50:23 -080025#include <linux/badblocks.h>
Dan Williams9476df72016-01-15 16:56:19 -080026#include <linux/memremap.h>
Dan Williams32ab0a3f2015-08-01 02:16:37 -040027#include <linux/vmalloc.h>
Dan Williams71389702017-04-28 10:23:37 -070028#include <linux/blk-mq.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080029#include <linux/pfn_t.h>
Ross Zwisler9e853f22015-04-01 09:12:19 +020030#include <linux/slab.h>
Ross Zwisler61031952015-06-25 03:08:39 -040031#include <linux/pmem.h>
Dan Williams0aed55a2017-05-29 12:22:50 -070032#include <linux/uio.h>
Dan Williamsc1d6e822017-01-24 23:02:09 -080033#include <linux/dax.h>
Dan Williams9f53f9f2015-06-09 15:33:45 -040034#include <linux/nd.h>
Dan Williamsf295e532016-06-17 11:08:06 -070035#include "pmem.h"
Dan Williams32ab0a3f2015-08-01 02:16:37 -040036#include "pfn.h"
Dan Williams9f53f9f2015-06-09 15:33:45 -040037#include "nd.h"
Ross Zwisler9e853f22015-04-01 09:12:19 +020038
Dan Williamsf284a4f2016-07-07 19:44:50 -070039static struct device *to_dev(struct pmem_device *pmem)
40{
41 /*
42 * nvdimm bus services need a 'dev' parameter, and we record the device
43 * at init in bb.dev.
44 */
45 return pmem->bb.dev;
46}
47
48static struct nd_region *to_region(struct pmem_device *pmem)
49{
50 return to_nd_region(to_dev(pmem)->parent);
51}
Ross Zwisler9e853f22015-04-01 09:12:19 +020052
Toshi Kani3115bb02016-10-13 09:54:21 -060053static int pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
Dan Williams59e64732016-03-08 07:16:07 -080054 unsigned int len)
55{
Dan Williamsf284a4f2016-07-07 19:44:50 -070056 struct device *dev = to_dev(pmem);
Dan Williams59e64732016-03-08 07:16:07 -080057 sector_t sector;
58 long cleared;
Dan Williams868f036f2016-12-16 08:10:31 -080059 int rc = 0;
Dan Williams59e64732016-03-08 07:16:07 -080060
61 sector = (offset - pmem->data_offset) / 512;
Dan Williams59e64732016-03-08 07:16:07 -080062
Dan Williams868f036f2016-12-16 08:10:31 -080063 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
64 if (cleared < len)
65 rc = -EIO;
Dan Williams59e64732016-03-08 07:16:07 -080066 if (cleared > 0 && cleared / 512) {
Dan Williams868f036f2016-12-16 08:10:31 -080067 cleared /= 512;
68 dev_dbg(dev, "%s: %#llx clear %ld sector%s\n", __func__,
69 (unsigned long long) sector, cleared,
70 cleared > 1 ? "s" : "");
Fabian Frederick0a3f27b2016-12-04 10:48:58 -080071 badblocks_clear(&pmem->bb, sector, cleared);
Dan Williams59e64732016-03-08 07:16:07 -080072 }
Toshi Kani3115bb02016-10-13 09:54:21 -060073
Dan Williams59e64732016-03-08 07:16:07 -080074 invalidate_pmem(pmem->virt_addr + offset, len);
Dan Williams868f036f2016-12-16 08:10:31 -080075
76 return rc;
Dan Williams59e64732016-03-08 07:16:07 -080077}
78
Vishal Vermabd697a82016-09-30 17:19:30 -060079static void write_pmem(void *pmem_addr, struct page *page,
80 unsigned int off, unsigned int len)
81{
82 void *mem = kmap_atomic(page);
83
Dan Williams0aed55a2017-05-29 12:22:50 -070084 memcpy_flushcache(pmem_addr, mem + off, len);
Vishal Vermabd697a82016-09-30 17:19:30 -060085 kunmap_atomic(mem);
86}
87
88static int read_pmem(struct page *page, unsigned int off,
89 void *pmem_addr, unsigned int len)
90{
91 int rc;
92 void *mem = kmap_atomic(page);
93
Dan Williams6abccd12017-01-13 14:14:23 -080094 rc = memcpy_mcsafe(mem + off, pmem_addr, len);
Vishal Vermabd697a82016-09-30 17:19:30 -060095 kunmap_atomic(mem);
Stefan Hajnoczid47d1d22017-01-05 10:05:46 +000096 if (rc)
97 return -EIO;
98 return 0;
Vishal Vermabd697a82016-09-30 17:19:30 -060099}
100
Dan Williamse10624f2016-01-06 12:03:41 -0800101static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
Jens Axboec11f0c02016-08-05 08:11:04 -0600102 unsigned int len, unsigned int off, bool is_write,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200103 sector_t sector)
104{
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800105 int rc = 0;
Dan Williams59e64732016-03-08 07:16:07 -0800106 bool bad_pmem = false;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400107 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
Dan Williams7a9eb202016-06-03 18:06:47 -0700108 void *pmem_addr = pmem->virt_addr + pmem_off;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200109
Dan Williams59e64732016-03-08 07:16:07 -0800110 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
111 bad_pmem = true;
112
Jens Axboec11f0c02016-08-05 08:11:04 -0600113 if (!is_write) {
Dan Williams59e64732016-03-08 07:16:07 -0800114 if (unlikely(bad_pmem))
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800115 rc = -EIO;
116 else {
Vishal Vermabd697a82016-09-30 17:19:30 -0600117 rc = read_pmem(page, off, pmem_addr, len);
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800118 flush_dcache_page(page);
119 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200120 } else {
Dan Williams0a370d262016-04-14 19:40:47 -0700121 /*
122 * Note that we write the data both before and after
123 * clearing poison. The write before clear poison
124 * handles situations where the latest written data is
125 * preserved and the clear poison operation simply marks
126 * the address range as valid without changing the data.
127 * In this case application software can assume that an
128 * interrupted write will either return the new good
129 * data or an error.
130 *
131 * However, if pmem_clear_poison() leaves the data in an
132 * indeterminate state we need to perform the write
133 * after clear poison.
134 */
Ross Zwisler9e853f22015-04-01 09:12:19 +0200135 flush_dcache_page(page);
Vishal Vermabd697a82016-09-30 17:19:30 -0600136 write_pmem(pmem_addr, page, off, len);
Dan Williams59e64732016-03-08 07:16:07 -0800137 if (unlikely(bad_pmem)) {
Toshi Kani3115bb02016-10-13 09:54:21 -0600138 rc = pmem_clear_poison(pmem, pmem_off, len);
Vishal Vermabd697a82016-09-30 17:19:30 -0600139 write_pmem(pmem_addr, page, off, len);
Dan Williams59e64732016-03-08 07:16:07 -0800140 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200141 }
142
Dan Williamsb5ebc8e2016-03-06 15:20:51 -0800143 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200144}
145
Dan Williams7e267a82016-06-01 20:48:15 -0700146/* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
147#ifndef REQ_FLUSH
148#define REQ_FLUSH REQ_PREFLUSH
149#endif
150
Jens Axboedece1632015-11-05 10:41:16 -0700151static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200152{
Dan Williamse10624f2016-01-06 12:03:41 -0800153 int rc = 0;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400154 bool do_acct;
155 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -0400156 struct bio_vec bvec;
157 struct bvec_iter iter;
Dan Williamsbd842b82016-03-18 23:47:43 -0700158 struct pmem_device *pmem = q->queuedata;
Dan Williams7e267a82016-06-01 20:48:15 -0700159 struct nd_region *nd_region = to_region(pmem);
160
Jens Axboe1eff9d32016-08-05 15:35:16 -0600161 if (bio->bi_opf & REQ_FLUSH)
Dan Williams7e267a82016-06-01 20:48:15 -0700162 nvdimm_flush(nd_region);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200163
Dan Williamsf0dc0892015-05-16 12:28:53 -0400164 do_acct = nd_iostat_start(bio, &start);
Dan Williamse10624f2016-01-06 12:03:41 -0800165 bio_for_each_segment(bvec, bio, iter) {
166 rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
Jens Axboec11f0c02016-08-05 08:11:04 -0600167 bvec.bv_offset, op_is_write(bio_op(bio)),
Dan Williamse10624f2016-01-06 12:03:41 -0800168 iter.bi_sector);
169 if (rc) {
170 bio->bi_error = rc;
171 break;
172 }
173 }
Dan Williamsf0dc0892015-05-16 12:28:53 -0400174 if (do_acct)
175 nd_iostat_end(bio, start);
Ross Zwisler61031952015-06-25 03:08:39 -0400176
Jens Axboe1eff9d32016-08-05 15:35:16 -0600177 if (bio->bi_opf & REQ_FUA)
Dan Williams7e267a82016-06-01 20:48:15 -0700178 nvdimm_flush(nd_region);
Ross Zwisler61031952015-06-25 03:08:39 -0400179
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200180 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700181 return BLK_QC_T_NONE;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200182}
183
184static int pmem_rw_page(struct block_device *bdev, sector_t sector,
Jens Axboec11f0c02016-08-05 08:11:04 -0600185 struct page *page, bool is_write)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200186{
Dan Williamsbd842b82016-03-18 23:47:43 -0700187 struct pmem_device *pmem = bdev->bd_queue->queuedata;
Dan Williamse10624f2016-01-06 12:03:41 -0800188 int rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200189
Jens Axboec11f0c02016-08-05 08:11:04 -0600190 rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200191
Dan Williamse10624f2016-01-06 12:03:41 -0800192 /*
193 * The ->rw_page interface is subtle and tricky. The core
194 * retries on any error, so we can only invoke page_endio() in
195 * the successful completion case. Otherwise, we'll see crashes
196 * caused by double completion.
197 */
198 if (rc == 0)
Jens Axboec11f0c02016-08-05 08:11:04 -0600199 page_endio(page, is_write, 0);
Dan Williamse10624f2016-01-06 12:03:41 -0800200
201 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200202}
203
Dan Williamsf295e532016-06-17 11:08:06 -0700204/* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
Dan Williamsc1d6e822017-01-24 23:02:09 -0800205__weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
206 long nr_pages, void **kaddr, pfn_t *pfn)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200207{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800208 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200209
Dan Williamsc1d6e822017-01-24 23:02:09 -0800210 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
211 PFN_PHYS(nr_pages))))
Dan Williams0a70bd42016-02-24 14:02:11 -0800212 return -EIO;
Ross Zwislere2e05392015-08-18 13:55:41 -0600213 *kaddr = pmem->virt_addr + offset;
Dan Williams34c0fd52016-01-15 16:56:14 -0800214 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200215
Dan Williams0a70bd42016-02-24 14:02:11 -0800216 /*
217 * If badblocks are present, limit known good range to the
218 * requested range.
219 */
220 if (unlikely(pmem->bb.count))
Dan Williamsc1d6e822017-01-24 23:02:09 -0800221 return nr_pages;
222 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200223}
224
225static const struct block_device_operations pmem_fops = {
226 .owner = THIS_MODULE,
227 .rw_page = pmem_rw_page,
Dan Williams58138822015-06-23 20:08:34 -0400228 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200229};
230
Dan Williamsc1d6e822017-01-24 23:02:09 -0800231static long pmem_dax_direct_access(struct dax_device *dax_dev,
232 pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
233{
234 struct pmem_device *pmem = dax_get_private(dax_dev);
235
236 return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
237}
238
Dan Williams0aed55a2017-05-29 12:22:50 -0700239static size_t pmem_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
240 void *addr, size_t bytes, struct iov_iter *i)
241{
242 return copy_from_iter_flushcache(addr, bytes, i);
243}
244
Dan Williams3c1cebf2017-05-29 12:58:19 -0700245static void pmem_dax_flush(struct dax_device *dax_dev, pgoff_t pgoff,
246 void *addr, size_t size)
247{
248 wb_cache_pmem(addr, size);
249}
250
Dan Williamsc1d6e822017-01-24 23:02:09 -0800251static const struct dax_operations pmem_dax_ops = {
252 .direct_access = pmem_dax_direct_access,
Dan Williams0aed55a2017-05-29 12:22:50 -0700253 .copy_from_iter = pmem_copy_from_iter,
Dan Williams3c1cebf2017-05-29 12:58:19 -0700254 .flush = pmem_dax_flush,
Dan Williamsc1d6e822017-01-24 23:02:09 -0800255};
256
Dan Williams030b99e2016-03-17 20:24:31 -0700257static void pmem_release_queue(void *q)
258{
259 blk_cleanup_queue(q);
260}
261
Dan Williams71389702017-04-28 10:23:37 -0700262static void pmem_freeze_queue(void *q)
263{
Linus Torvaldsd3b5d352017-05-01 23:54:56 -0700264 blk_freeze_queue_start(q);
Dan Williams71389702017-04-28 10:23:37 -0700265}
266
Dan Williamsc1d6e822017-01-24 23:02:09 -0800267static void pmem_release_disk(void *__pmem)
Dan Williams030b99e2016-03-17 20:24:31 -0700268{
Dan Williamsc1d6e822017-01-24 23:02:09 -0800269 struct pmem_device *pmem = __pmem;
270
271 kill_dax(pmem->dax_dev);
272 put_dax(pmem->dax_dev);
273 del_gendisk(pmem->disk);
274 put_disk(pmem->disk);
Dan Williams030b99e2016-03-17 20:24:31 -0700275}
276
Dan Williams200c79d2016-03-22 00:22:16 -0700277static int pmem_attach_disk(struct device *dev,
278 struct nd_namespace_common *ndns)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200279{
Dan Williams200c79d2016-03-22 00:22:16 -0700280 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
Dan Williamsf284a4f2016-07-07 19:44:50 -0700281 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams200c79d2016-03-22 00:22:16 -0700282 struct vmem_altmap __altmap, *altmap = NULL;
283 struct resource *res = &nsio->res;
284 struct nd_pfn *nd_pfn = NULL;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800285 struct dax_device *dax_dev;
Dan Williams200c79d2016-03-22 00:22:16 -0700286 int nid = dev_to_node(dev);
287 struct nd_pfn_sb *pfn_sb;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200288 struct pmem_device *pmem;
Dan Williams200c79d2016-03-22 00:22:16 -0700289 struct resource pfn_res;
Dan Williams468ded02016-01-15 16:56:46 -0800290 struct request_queue *q;
Dan Williams200c79d2016-03-22 00:22:16 -0700291 struct gendisk *disk;
292 void *addr;
293
294 /* while nsio_rw_bytes is active, parse a pfn info block if present */
295 if (is_nd_pfn(dev)) {
296 nd_pfn = to_nd_pfn(dev);
297 altmap = nvdimm_setup_pfn(nd_pfn, &pfn_res, &__altmap);
298 if (IS_ERR(altmap))
299 return PTR_ERR(altmap);
300 }
301
302 /* we're attaching a block device, disable raw namespace access */
303 devm_nsio_disable(dev, nsio);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200304
Christoph Hellwig708ab622015-08-10 23:07:08 -0400305 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200306 if (!pmem)
Dan Williams200c79d2016-03-22 00:22:16 -0700307 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200308
Dan Williams200c79d2016-03-22 00:22:16 -0700309 dev_set_drvdata(dev, pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200310 pmem->phys_addr = res->start;
311 pmem->size = resource_size(res);
Dan Williams0aed55a2017-05-29 12:22:50 -0700312 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE)
313 || nvdimm_has_flush(nd_region) < 0)
Ross Zwisler61031952015-06-25 03:08:39 -0400314 dev_warn(dev, "unable to guarantee persistence of writes\n");
Ross Zwisler9e853f22015-04-01 09:12:19 +0200315
Dan Williams947df022016-03-21 22:28:40 -0700316 if (!devm_request_mem_region(dev, res->start, resource_size(res),
Dan Williams450c6632016-11-28 11:15:18 -0800317 dev_name(&ndns->dev))) {
Dan Williams947df022016-03-21 22:28:40 -0700318 dev_warn(dev, "could not reserve region %pR\n", res);
Dan Williams200c79d2016-03-22 00:22:16 -0700319 return -EBUSY;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200320 }
321
Dan Williams468ded02016-01-15 16:56:46 -0800322 q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
323 if (!q)
Dan Williams200c79d2016-03-22 00:22:16 -0700324 return -ENOMEM;
Dan Williams468ded02016-01-15 16:56:46 -0800325
Dan Williams71389702017-04-28 10:23:37 -0700326 if (devm_add_action_or_reset(dev, pmem_release_queue, q))
327 return -ENOMEM;
328
Dan Williams34c0fd52016-01-15 16:56:14 -0800329 pmem->pfn_flags = PFN_DEV;
Dan Williams200c79d2016-03-22 00:22:16 -0700330 if (is_nd_pfn(dev)) {
331 addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
332 altmap);
333 pfn_sb = nd_pfn->pfn_sb;
334 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
335 pmem->pfn_pad = resource_size(res) - resource_size(&pfn_res);
336 pmem->pfn_flags |= PFN_MAP;
337 res = &pfn_res; /* for badblocks populate */
338 res->start += pmem->data_offset;
339 } else if (pmem_should_map_pages(dev)) {
340 addr = devm_memremap_pages(dev, &nsio->res,
Dan Williams5c2c2582016-01-15 16:56:49 -0800341 &q->q_usage_counter, NULL);
Dan Williams34c0fd52016-01-15 16:56:14 -0800342 pmem->pfn_flags |= PFN_MAP;
343 } else
Dan Williams200c79d2016-03-22 00:22:16 -0700344 addr = devm_memremap(dev, pmem->phys_addr,
345 pmem->size, ARCH_MEMREMAP_PMEM);
Dan Williamsb36f4762015-09-15 02:42:20 -0400346
Dan Williams030b99e2016-03-17 20:24:31 -0700347 /*
Dan Williams71389702017-04-28 10:23:37 -0700348 * At release time the queue must be frozen before
Dan Williams030b99e2016-03-17 20:24:31 -0700349 * devm_memremap_pages is unwound
350 */
Dan Williams71389702017-04-28 10:23:37 -0700351 if (devm_add_action_or_reset(dev, pmem_freeze_queue, q))
Dan Williams200c79d2016-03-22 00:22:16 -0700352 return -ENOMEM;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400353
Dan Williams200c79d2016-03-22 00:22:16 -0700354 if (IS_ERR(addr))
355 return PTR_ERR(addr);
Dan Williams7a9eb202016-06-03 18:06:47 -0700356 pmem->virt_addr = addr;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200357
Dan Williams7e267a82016-06-01 20:48:15 -0700358 blk_queue_write_cache(q, true, true);
Dan Williams5a922892016-03-21 15:43:53 -0700359 blk_queue_make_request(q, pmem_make_request);
360 blk_queue_physical_block_size(q, PAGE_SIZE);
361 blk_queue_max_hw_sectors(q, UINT_MAX);
362 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
363 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Toshi Kani163d4ba2016-06-23 17:05:50 -0400364 queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
Dan Williams5a922892016-03-21 15:43:53 -0700365 q->queuedata = pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200366
Dan Williams538ea4a2015-10-05 20:35:56 -0400367 disk = alloc_disk_node(0, nid);
Dan Williams030b99e2016-03-17 20:24:31 -0700368 if (!disk)
369 return -ENOMEM;
Dan Williamsc1d6e822017-01-24 23:02:09 -0800370 pmem->disk = disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200371
Ross Zwisler9e853f22015-04-01 09:12:19 +0200372 disk->fops = &pmem_fops;
Dan Williams5a922892016-03-21 15:43:53 -0700373 disk->queue = q;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200374 disk->flags = GENHD_FL_EXT_DEVT;
Vishal Verma5212e112015-06-25 04:20:32 -0400375 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williamscfe30b82016-03-03 09:38:00 -0800376 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
377 / 512);
Dan Williamsb95f5f42016-01-04 23:50:23 -0800378 if (devm_init_badblocks(dev, &pmem->bb))
379 return -ENOMEM;
Dan Williamsf284a4f2016-07-07 19:44:50 -0700380 nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
Dan Williams57f7f312016-01-06 12:03:42 -0800381 disk->bb = &pmem->bb;
Dan Williamsf02716d2016-06-15 14:59:17 -0700382
Dan Williamsc1d6e822017-01-24 23:02:09 -0800383 dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops);
384 if (!dax_dev) {
385 put_disk(disk);
386 return -ENOMEM;
387 }
388 pmem->dax_dev = dax_dev;
389
390 device_add_disk(dev, disk);
391 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
Dan Williamsf02716d2016-06-15 14:59:17 -0700392 return -ENOMEM;
393
Dan Williams58138822015-06-23 20:08:34 -0400394 revalidate_disk(disk);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200395
Dan Williams8c2f7e82015-06-25 04:20:04 -0400396 return 0;
397}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200398
Dan Williams9f53f9f2015-06-09 15:33:45 -0400399static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200400{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400401 struct nd_namespace_common *ndns;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200402
Dan Williams8c2f7e82015-06-25 04:20:04 -0400403 ndns = nvdimm_namespace_common_probe(dev);
404 if (IS_ERR(ndns))
405 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400406
Dan Williams200c79d2016-03-22 00:22:16 -0700407 if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
408 return -ENXIO;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200409
Dan Williams200c79d2016-03-22 00:22:16 -0700410 if (is_nd_btt(dev))
Christoph Hellwig708ab622015-08-10 23:07:08 -0400411 return nvdimm_namespace_attach_btt(ndns);
412
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400413 if (is_nd_pfn(dev))
Dan Williams200c79d2016-03-22 00:22:16 -0700414 return pmem_attach_disk(dev, ndns);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400415
Dan Williams200c79d2016-03-22 00:22:16 -0700416 /* if we find a valid info-block we'll come back as that personality */
Dan Williamsc5ed9262016-05-18 14:50:12 -0700417 if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
418 || nd_dax_probe(dev, ndns) == 0)
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400419 return -ENXIO;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400420
Dan Williams200c79d2016-03-22 00:22:16 -0700421 /* ...otherwise we're just a raw pmem device */
422 return pmem_attach_disk(dev, ndns);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200423}
424
Dan Williams9f53f9f2015-06-09 15:33:45 -0400425static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200426{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400427 if (is_nd_btt(dev))
Dan Williams298f2bc2016-03-15 16:41:04 -0700428 nvdimm_namespace_detach_btt(to_nd_btt(dev));
Dan Williams476f8482016-07-09 00:12:52 -0700429 nvdimm_flush(to_nd_region(dev->parent));
430
Ross Zwisler9e853f22015-04-01 09:12:19 +0200431 return 0;
432}
433
Dan Williams476f8482016-07-09 00:12:52 -0700434static void nd_pmem_shutdown(struct device *dev)
435{
436 nvdimm_flush(to_nd_region(dev->parent));
437}
438
Dan Williams71999462016-02-18 10:29:49 -0800439static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
440{
Toshi Kanib2518c72017-04-25 17:04:13 -0600441 struct nd_region *nd_region;
Dan Williams298f2bc2016-03-15 16:41:04 -0700442 resource_size_t offset = 0, end_trunc = 0;
443 struct nd_namespace_common *ndns;
444 struct nd_namespace_io *nsio;
445 struct resource res;
Toshi Kanib2518c72017-04-25 17:04:13 -0600446 struct badblocks *bb;
Dan Williams71999462016-02-18 10:29:49 -0800447
448 if (event != NVDIMM_REVALIDATE_POISON)
449 return;
450
Dan Williams298f2bc2016-03-15 16:41:04 -0700451 if (is_nd_btt(dev)) {
452 struct nd_btt *nd_btt = to_nd_btt(dev);
453
454 ndns = nd_btt->ndns;
Toshi Kanib2518c72017-04-25 17:04:13 -0600455 nd_region = to_nd_region(ndns->dev.parent);
456 nsio = to_nd_namespace_io(&ndns->dev);
457 bb = &nsio->bb;
458 } else {
459 struct pmem_device *pmem = dev_get_drvdata(dev);
Dan Williamsa3901802016-04-07 20:02:06 -0700460
Toshi Kanib2518c72017-04-25 17:04:13 -0600461 nd_region = to_region(pmem);
462 bb = &pmem->bb;
Dan Williamsa3901802016-04-07 20:02:06 -0700463
Toshi Kanib2518c72017-04-25 17:04:13 -0600464 if (is_nd_pfn(dev)) {
465 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
466 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
467
468 ndns = nd_pfn->ndns;
469 offset = pmem->data_offset +
470 __le32_to_cpu(pfn_sb->start_pad);
471 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
472 } else {
473 ndns = to_ndns(dev);
474 }
475
476 nsio = to_nd_namespace_io(&ndns->dev);
477 }
478
Dan Williams298f2bc2016-03-15 16:41:04 -0700479 res.start = nsio->res.start + offset;
480 res.end = nsio->res.end - end_trunc;
Toshi Kanib2518c72017-04-25 17:04:13 -0600481 nvdimm_badblocks_populate(nd_region, bb, &res);
Dan Williams71999462016-02-18 10:29:49 -0800482}
483
Dan Williams9f53f9f2015-06-09 15:33:45 -0400484MODULE_ALIAS("pmem");
485MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400486MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400487static struct nd_device_driver nd_pmem_driver = {
488 .probe = nd_pmem_probe,
489 .remove = nd_pmem_remove,
Dan Williams71999462016-02-18 10:29:49 -0800490 .notify = nd_pmem_notify,
Dan Williams476f8482016-07-09 00:12:52 -0700491 .shutdown = nd_pmem_shutdown,
Dan Williams9f53f9f2015-06-09 15:33:45 -0400492 .drv = {
493 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200494 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400495 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200496};
497
498static int __init pmem_init(void)
499{
NeilBrown55155292016-03-09 09:21:54 +1100500 return nd_driver_register(&nd_pmem_driver);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200501}
502module_init(pmem_init);
503
504static void pmem_exit(void)
505{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400506 driver_unregister(&nd_pmem_driver.drv);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200507}
508module_exit(pmem_exit);
509
510MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
511MODULE_LICENSE("GPL v2");